Home > Net >  Converting a date variable from double to long
Converting a date variable from double to long

Time:06-01

I have a date variable in Stata which is stored as a double which looks like this:

Date
07dec2012
08jan2018
    .
    . 
    .

The display format is: %td. I would like to convert this variable into a long format so that I can use it for a fixed effects analysis. Is there an easy way of doing so?

CodePudding user response:

long is a variable or storage type, not a (display) format.

Daily dates that arise in most practice can be stored as int variables, so compress will achieve that.

. clear

. set obs 1
Number of observations (_N) was 0, now 1.

. gen double date = mdy(1, 8, 2018)

. compress
  variable date was double now int

I wasn't aware that being double would be a problem for what you want to do, and I can't see why long should be preferred over int.

EDIT The reported range of dates can be tested too

. clear

. set obs 2
Number of observations (_N) was 0, now 2.

. gen double Date = cond(_n==1, -19903, 22280)



. format Date %td

. l

      ----------- 
     |      Date |
     |-----------|
  1. | 05jul1905 |
  2. | 31dec2020 |
      ----------- 

. compress Date
  variable Date was double now int
  (12 bytes saved)
  • Related