Hi all, I need help with creating a datetime variable. I've already been through the stata help and the manuals but the problem remains.
I have 2 variables in my dataset, one containing the date (string) and a second one containing the time (double)
I have run the following command to merge both into a datetime variable:
generate double first_dose_day = clock( tre01b , "YMD")
format %tc first_dose_day
format tre01c %tcHH:MM
gen double dt_dose1=first_dose_day + tre01c
format dt_dose1 %tcNN/DD/CCYY_HH:MM
But there seems to be a problem as the new variable dt_dose1 gives me this:
. list dt_dose1 in 3/10
dt_dose1
3. 07/16/1962 13:15
4. 07/17/1962 16:45
5. 07/17/1962 16:40
6. 07/18/1962 07:50
7. 07/18/1962 14:05
8. 07/19/1962 10:10
9. 07/24/1962 09:40
10. 07/26/1962 14:00
I can't identify what the problem is and have tried several different ways like changing the tre01c variable to string and then using clock, etc.
I am aware that is a very basic question, but somehow I can't seem to find a solution! I would appreciate any help you can provide.
Thanks
Niki
I have 2 variables in my dataset, one containing the date (string) and a second one containing the time (double)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str10 tre01b double tre01c "2022-07-15" -1.8933219e+12 "2022-07-16" -1.8933093e+12 "2022-07-16" -1.8933096e+12 "2022-07-17" -1.8933414e+12 "2022-07-17" -1.8933189e+12 "2022-07-18" -1.893333e+12 "2022-07-23" -1.8933348e+12 "2022-07-25" -1.8933192e+12 end format %tc tre01c
generate double first_dose_day = clock( tre01b , "YMD")
format %tc first_dose_day
format tre01c %tcHH:MM
gen double dt_dose1=first_dose_day + tre01c
format dt_dose1 %tcNN/DD/CCYY_HH:MM
But there seems to be a problem as the new variable dt_dose1 gives me this:
. list dt_dose1 in 3/10
dt_dose1
3. 07/16/1962 13:15
4. 07/17/1962 16:45
5. 07/17/1962 16:40
6. 07/18/1962 07:50
7. 07/18/1962 14:05
8. 07/19/1962 10:10
9. 07/24/1962 09:40
10. 07/26/1962 14:00
I can't identify what the problem is and have tried several different ways like changing the tre01c variable to string and then using clock, etc.
I am aware that is a very basic question, but somehow I can't seem to find a solution! I would appreciate any help you can provide.
Thanks
Niki

Comment