Dear All,
I am using Stata/SE 13.1 and having trouble when formatting a date.
I have dates extracted from excel, using the input excel function of stata
codebook date
type: numeric daily date (int)
rage [20451,20818]
or equivlently 01jan2016,30dec2016] units: days
list date in 1/5
1. 01jan2016
2. 01jan2016
3. 06jan2016
4. 13jan2016
5. 22jan2016
Now I want to format it in a monthly date, showing "Apr 2009".
To be absolutely sure I do not make any mistake while typing my command, I use the Variables manager to format my date:
format %tmMon_CCYY date
Now I want to see if it work:
codebook date
I obtain:
type: numeric monthly date
range: [20454, 20818]
or equivalently [3664m7, 3694m11] units: months
list date in 1/5
1. Jul 3664
2. Jul 3664
3. Dec 3664
4. Jul 3665
What happened? What did I do wrong?
I do experience the same trouble when trying to format my date in year.
Strangely, if I extract the day, the month and the year it works well:
gen datem=month(date)
list date datem in 1/5
1. 01jan2016 1
2. 01jan2016 1
3. 06jan2016 1
4. 13jan2016 1
5. 22jan2016 1
gen datey=year(date)
list date datey in 1/5
1. 01jan2016 2016
2. 01jan2016 2016
3. 06jan2016 2016
4. 13jan2016 2016
5. 22jan2016 2016
gen dateday=day(date)
list date dated in 1/5
1. 01jan2016 1
2. 01jan2016 1
3. 06jan2016 6
4. 13jan2016 13
5. 22jan2016 22
I have tried to recreate my date using the mdy()
gen datenew=mdy(datemonth,dateday,dateyear)
list date datenew in 1/5
1. 01jan2016 20454
2. 01jan2016 20454
3. 06jan2016 20459
4. 13jan2016 20466
5. 22jan2016 20475
Then I formatted it in daily and it worked
format datenew %td
list date datenew in 1/5
1. 01jan2016 01jan2016
2. 01jan2016 01jan2016
3. 06jan2016 06jan2016
4. 13jan2016 13jan2016
5. 22jan2016 22jan2016
But when trying to get the monthly format, it is going wrong again
format datenew %tm
list date datenew in 1/5
1. 01jan2016 3664m7
2. 01jan2016 3664m7
3. 06jan2016 3664m12
4. 13jan2016 3665m7
5. 22jan2016 3666m4
I have already spent 2 hours trying to understand this issue and fixing it, reading other posts from the statalist, at statblog, at the help files in stata, the PDF manual, and other online resources. I have been using stata for many years for data analysis in epidemiology and I have never been stuck like today...
Thanks for your help
Sophie
I am using Stata/SE 13.1 and having trouble when formatting a date.
I have dates extracted from excel, using the input excel function of stata
codebook date
type: numeric daily date (int)
rage [20451,20818]
or equivlently 01jan2016,30dec2016] units: days
list date in 1/5
1. 01jan2016
2. 01jan2016
3. 06jan2016
4. 13jan2016
5. 22jan2016
Now I want to format it in a monthly date, showing "Apr 2009".
To be absolutely sure I do not make any mistake while typing my command, I use the Variables manager to format my date:
format %tmMon_CCYY date
Now I want to see if it work:
codebook date
I obtain:
type: numeric monthly date
range: [20454, 20818]
or equivalently [3664m7, 3694m11] units: months
list date in 1/5
1. Jul 3664
2. Jul 3664
3. Dec 3664
4. Jul 3665
What happened? What did I do wrong?
I do experience the same trouble when trying to format my date in year.
Strangely, if I extract the day, the month and the year it works well:
gen datem=month(date)
list date datem in 1/5
1. 01jan2016 1
2. 01jan2016 1
3. 06jan2016 1
4. 13jan2016 1
5. 22jan2016 1
gen datey=year(date)
list date datey in 1/5
1. 01jan2016 2016
2. 01jan2016 2016
3. 06jan2016 2016
4. 13jan2016 2016
5. 22jan2016 2016
gen dateday=day(date)
list date dated in 1/5
1. 01jan2016 1
2. 01jan2016 1
3. 06jan2016 6
4. 13jan2016 13
5. 22jan2016 22
I have tried to recreate my date using the mdy()
gen datenew=mdy(datemonth,dateday,dateyear)
list date datenew in 1/5
1. 01jan2016 20454
2. 01jan2016 20454
3. 06jan2016 20459
4. 13jan2016 20466
5. 22jan2016 20475
Then I formatted it in daily and it worked
format datenew %td
list date datenew in 1/5
1. 01jan2016 01jan2016
2. 01jan2016 01jan2016
3. 06jan2016 06jan2016
4. 13jan2016 13jan2016
5. 22jan2016 22jan2016
But when trying to get the monthly format, it is going wrong again
format datenew %tm
list date datenew in 1/5
1. 01jan2016 3664m7
2. 01jan2016 3664m7
3. 06jan2016 3664m12
4. 13jan2016 3665m7
5. 22jan2016 3666m4
I have already spent 2 hours trying to understand this issue and fixing it, reading other posts from the statalist, at statblog, at the help files in stata, the PDF manual, and other online resources. I have been using stata for many years for data analysis in epidemiology and I have never been stuck like today...
Thanks for your help
Sophie

Comment