Hello,
I have a date variable that contains hms and am trying to use it to identify the season in which the dates are. Here are some sample data.
If I split the date from the time, I am able to identify the seasons with the following code.
That gives me what I want but I can't figure out why the following doesn't produce the same result.
From what I understand, the dofc function should change the date information of date2 into the date type rather than the date-clock type and then the year function should extract the year just like it does in the first set of code. I haven't been able to figure it out what I'm missing from the resources I've been able to get my hands on. Since I can get what I need from the first set of code, I'm hoping to better understand the date-time functions, which I've always struggled with.
Thanks,
Lance
I have a date variable that contains hms and am trying to use it to identify the season in which the dates are. Here are some sample data.
Code:
clear input str18 startdate "27jun2017 17:41:17" "14jun2017 17:31:28" "11mar2019 20:13:10" "06sep2017 14:26:16" "16jun2017 16:00:39" "03may2019 10:00:07" "20jun2017 11:01:14" "03may2018 09:50:13" "14nov2018 11:46:36" "30may2019 11:34:01" "16jun2017 09:57:07" "23sep2017 17:49:21" "07sep2017 19:10:58" "25jan2018 09:29:11" "27jun2017 20:33:07" end
Code:
split startdate gen date1 = date(startdate1,"DMY") format date1 %td gen season1 = . replace season1 = 1 if inrange(date1,mdy(3,21,year(date1)),mdy(6,20,year(date1))) replace season1 = 2 if inrange(date1,mdy(6,21,year(date1)),mdy(9,21,year(date1))) replace season1 = 3 if inrange(date1,mdy(9,22,year(date1)),mdy(12,20,year(date1))) replace season1 = 4 if date1 >= mdy(12,21,year(date1)) | date1 <= mdy(3,20,year(date1))
Code:
gen date2 = clock(startdate1,"DMY hms") format date2 %tc gen season2 = . replace season2 = 1 if inrange(date2,mdy(3,21,year(dofc(date2))),mdy(6,20,year(dofc(date2)))) replace season2 = 2 if inrange(date2,mdy(6,21,year(dofc(date2))),mdy(9,21,year(dofc(date2)))) replace season2 = 3 if inrange(date2,mdy(9,22,year(dofc(date2))),mdy(12,20,year(dofc(date2)))) replace season2 = 4 if date2 >= mdy(12,21,year(dofc(date2))) | date2 <= mdy(3,20,year(dofc(date2)))
Thanks,
Lance
Comment