I am trying to assign a range of dates to the month that the most days occur in. The ranges are random (but are usually about 1 month).
This is not the cleanest way to read in some random dates, but it works.
I would like a new variable/column called "month" that returns the month that the most days between first_date and second_date occur in. The answers should be numeric (1 represents january, 2 represents february and so on). The "month" variable/column for the 6 ranges in the dataex example should be 7, 8, 9, 10, 11, 12.
Thanks in advance.
This is not the cleanest way to read in some random dates, but it works.
Code:
clear input obs str8 firstdate str8 seconddate 1 "20180712" "20180803" 2 "20180804" "20180903" 3 "20180904" "20181005" 4 "20181006" "20181028" 5 "20181029" "20181128" 6 "20181129" "20190103" end destring firstdate seconddate, replace gen year = floor(firstdate/10000) gen month = floor(mod(firstdate, 10000)/100) gen day = mod(firstdate, 100) gen year2 = floor(seconddate/10000) gen month2 = floor(mod(seconddate, 10000)/100) gen day2 = mod(seconddate, 100) gen first_date = mdy(month, day, year) gen second_date = mdy(month2, day2, year2) drop year* month* day* firstdate seconddate format first_date second_date %tdCY-N-D
Thanks in advance.
Comment