Hello,
I have 5 million + rows with year variables that include 15 years of data and month variables with 12 months in each year. I want to generate and assign 4-5 correct weeks codes for each month in all 15 years. From data ex a sample sample:
I want to assign each row 4 or 5 new variables that correspond to the 4 or 5 weeks within each historical year and month.
For now, I don't know what day the first Monday fell in any of the months, so I had to arbitrarily assign the first day of the month as the start of the first week.
What I did is:
g day = 1
g week1 = mdy(month , day , year)
format %9.0f week1
replace week1 = (week1-2182)/7
drop day year month
* creating 4-5 week codes within each month - 4 weeks in a month with some having 5 weeks = dropped extra 5th week later
forval i =2/5{
g week`i' = round(week1 + `i' - 1, 1)
}
This way is wrong because if the start if the first Monday occurred on for ex. March 3, I would still have that the full week occurred March 1-7, throwing off all the weeks that follow.
Is there any way to turn months into the correct weeks?
Please help!
Thank you!
I have 5 million + rows with year variables that include 15 years of data and month variables with 12 months in each year. I want to generate and assign 4-5 correct weeks codes for each month in all 15 years. From data ex a sample sample:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str6 ind float(year month) "M00647" 2007 7 "M00647" 2007 8 "M00647" 2007 9 "M00647" 2007 10 "M00647" 2007 11 "M00647" 2007 12 "M00647" 2008 1 "M00647" 2008 2 "M00647" 2008 3 "M00647" 2008 4 "M00647" 2008 5 "M00647" 2008 6 "M00887" 2007 7 "M00887" 2007 8 "M00887" 2007 9 "M00887" 2007 10 "M00887" 2007 11 "M00887" 2007 12 "M00887" 2008 1 "M00887" 2008 2 "M00887" 2008 3 "M00887" 2008 4 "M00887" 2008 5 "M00888" 2008 6 "M00888" 2008 7 "M00888" 2008 8 "M00888" 2008 9 "M00888" 2008 10 "M00888" 2008 11 "M00888" 2008 12 end
For now, I don't know what day the first Monday fell in any of the months, so I had to arbitrarily assign the first day of the month as the start of the first week.
What I did is:
g day = 1
g week1 = mdy(month , day , year)
format %9.0f week1
replace week1 = (week1-2182)/7
drop day year month
* creating 4-5 week codes within each month - 4 weeks in a month with some having 5 weeks = dropped extra 5th week later
forval i =2/5{
g week`i' = round(week1 + `i' - 1, 1)
}
This way is wrong because if the start if the first Monday occurred on for ex. March 3, I would still have that the full week occurred March 1-7, throwing off all the weeks that follow.
Is there any way to turn months into the correct weeks?
Please help!
Thank you!
Comment