Reputation: 3
I am trying to combine a date and time variable into a date/time variable in Stata.
Suppose date = 23jun2015
and time = 31dec1899 11:23:00
and suppose that I want to create a new variable datetime= 23jun2015 11:23:00
.
The format of date
is %tdNN/DD/CCYY
and the format of time
is %tcHH:MM:SS
I have tried the following code
gen double datetime = date*24*60*60*1000 + time
format datetime %tcNN/DD/CCYY_HH:MM:SS
However, somehow I get the following result (the year is wrong):
23jun1955 11:23:00
Any ideas how I can change this to get the correct year?
Thanks!
Upvotes: 0
Views: 4202
Reputation:
The following should point you in the right direction.
clear
set obs 1
generate date = daily("23jun2015","DMY")
generate double time = clock("31dec1899 11:23:00","DMY hms")
format date %tdNN/DD/CCYY
format time %tcHH:MM:SS
generate double datetime = dhms(date,hh(time),mm(time),ss(time))
format datetime %tcNN/DD/CCYY_HH:MM:SS
list
If you have not already done so, you will want to review help datetime
, which is without a doubt the most visited documentation on my system, with the second-most-visited being Chapter 24 (Working with dates and times) of the Stata User's Guide PDF available from the PDF Documentation item on Stata's Help menu. Before working with dates and times, any Stata user should read the very detailed Chapter 24 thoroughly. After that, the help datetime documentation will usually be enough to point the way. Some people may be able to remember everything without have to continually refer to the documentation, but I for one am not such a person.
Upvotes: 1