Dear all,
I have a string variable that I am trying to convert into a datetime variable with format %tC+CCYY-NN-DD_HH:MM. The string variable which is called "start" uses year-Julian days for the date and Universal Time Coordinated (UTC) for the time. An example observation is "2008001 1645" where the first 4 characters indicate the year, characters 5 to 7 indicate the Julian day, character 8 is a space, characters 9 to 12 indicate the UTC. To convert from the string variable to a Stata datetime variable in the %tC+CCYY-NN-DD_HH:MM format, I tried the following code:
After running this code every datetime observation was 1960-01-01 00:00. I hope to hear your thoughts if this code can be modified so that "2008001 1645" from the original string variable would be converted to a datetime variable formatted as 2008-01-01 16:45. Thank you.
Below is generated data for this question:
I have a string variable that I am trying to convert into a datetime variable with format %tC+CCYY-NN-DD_HH:MM. The string variable which is called "start" uses year-Julian days for the date and Universal Time Coordinated (UTC) for the time. An example observation is "2008001 1645" where the first 4 characters indicate the year, characters 5 to 7 indicate the Julian day, character 8 is a space, characters 9 to 12 indicate the UTC. To convert from the string variable to a Stata datetime variable in the %tC+CCYY-NN-DD_HH:MM format, I tried the following code:
Code:
* convert year, Julian day and UTC substrings to numeric variables gen start_year_num = real(substr(start, 1, 4)) gen start_day_num = real(substr(start, 5, 3)) gen start_utc_num = real(substr(start, 9, 4)) * convert the year, Julian day, and UTC hour-minute to Stata date and time variables gen double start_datetime = mdy(1, 1, start_year_num) + start_day_num - 1 + start_utc_num / 2400 * format the new datetime variable format start_datetime %tC+CCYY-NN-DD_HH:MM
Below is generated data for this question:
Code:
clear input str12 start "2008186 1915" "2008197 1230" "2008202 0015" "2008204 2145" "2008214 2245" "2008215 2015" "2008215 2015" "2008215 2015" "2008272 2345" "2008276 1915" end
Comment