I am using Stata 16 and seem to have problem extracting year, month and day from a date-time column. Below is an example of my dataset:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str26 created double date1 float(year_first_detected day_first_detected month_first_detected) double date2 float(year_last_detected day_last_detected month_last_detected) double date3 float(year_created day_created month_created) "2020-11-15 06:45:31.901427" 1.8823932e+12 2019 25 8 1.908054e+12 2020 17 6 . . . . "2020-11-15 06:45:31.901427" 1.8823932e+12 2019 25 8 1.9180188e+12 2020 11 10 . . . . "2020-11-15 06:45:31.901427" 1.8823932e+12 2019 25 8 1.9180188e+12 2020 11 10 . . . . "2020-11-15 06:45:31.901427" 1.8823932e+12 2019 25 8 1.9180188e+12 2020 11 10 . . . . "2020-11-15 06:45:31.901427" 1.8837756e+12 2019 10 9 1.9180188e+12 2020 11 10 . . . . "2020-11-15 06:45:31.901427" 1.8837756e+12 2019 10 9 1.9180188e+12 2020 11 10 . . . . "2020-11-15 06:45:31.901427" 1.8837756e+12 2019 10 9 1.908054e+12 2020 17 6 . . . . "2020-11-15 06:45:31.901427" 1.8837756e+12 2019 10 9 1.908054e+12 2020 17 6 . . . . "2020-11-15 06:45:31.901427" 1.8837756e+12 2019 10 9 1.908054e+12 2020 17 6 . . . . "2020-11-15 06:45:31.901427" 1.8837756e+12 2019 10 9 1.908054e+12 2020 17 6 . . . . "2020-11-15 06:45:31.901427" 1.8837756e+12 2019 10 9 1.908054e+12 2020 17 6 . . . . "2020-11-15 06:45:31.901427" 1.9140444e+12 2020 26 8 1.9180188e+12 2020 11 10 . . . . end format %tc date1 format %tc date2 format %tc date3
For the variables date1, date2 and date3, I used the following codes:
Code:
gen double trial = clock(first_detected,"YMDhms") format %tc trial rename trial date1 gen year = year(dofc(date1)) gen day = day(dofc(date1)) gen month = month(dofc(date1)) gen double date2 =clock(last_detected, "YMDhms") format %tc date2 gen year_last_detected = year(dofc(date2)) gen day_last_detected = day(dofc(date2)) gen month_last_detected = month(dofc(date2)) **This code does not work gen double date3 = clock(created, "YMDhms") format %tc date3 gen year_created = year(dofc(date3)) gen day_created = day(dofc(date3)) gen month_created = month(dofc(date3))
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str23(first_detected last_detected) str26 created "2019-08-25 23:00:00" "2020-06-17 23:00:00" "2020-11-15 06:45:31.901427" "2019-08-25 23:00:00" "2020-10-11 07:00:00" "2020-11-15 06:45:31.901427" "2019-08-25 23:00:00" "2020-10-11 07:00:00" "2020-11-15 06:45:31.901427" "2019-08-25 23:00:00" "2020-10-11 07:00:00" "2020-11-15 06:45:31.901427" "2019-09-10 23:00:00" "2020-10-11 07:00:00" "2020-11-15 06:45:31.901427" "2019-09-10 23:00:00" "2020-10-11 07:00:00" "2020-11-15 06:45:31.901427" "2019-09-10 23:00:00" "2020-06-17 23:00:00" "2020-11-15 06:45:31.901427" "2019-09-10 23:00:00" "2020-06-17 23:00:00" "2020-11-15 06:45:31.901427" end
Any help in this area would be appreciated. Thanks!
0 Response to Dealing with date and time
Post a Comment