I have a dataset with student test scores and test dates for the years 2008-2019. I have all student test scores but I don't have their test dates for the years 2008 to 2014. The data looks something like this:
student id | test_score | test_date | year | school |
1 | 90 | . | 2010 | 100 |
1 | 98 | . | 2011 | 100 |
1 | 95 | apr 30 2015 | 2015 | 100 |
1 | 94 | may 1 2016 | 2016 | 100 |
2 | 88 | . | 2013 | 200 |
2 | 88 | . | 2014 | 200 |
2 | 89 | may 3 2015 | 2015 | 200 |
2 | 90 | apr 22 2017 | 2017 | 200 |
3 | 70 | . | 2011 | 100 |
3 | 75 | apr 30 2015 | 2015 | 100 |
I am imputing these missing test dates by taking the average test date of the non-missing test dates by school but some of these average test dates end up being on a Saturday or Sunday. I know that most students take their tests on a Monday. I would like to round the imputed test dates to the nearest Monday. Does anyone know how to do that? Here is what my code looks like so far:
gen day = day(test_date)
gen month = month(test_date)
egen day_mean = mean(day), by(school)
egen month_mean = mean(month), by(school)
gen date_imp = mdy(month_mean, day_mean, year)
***insert code to round to nearest monday here!!!
replace test_date = date_imp if test_date == .
0 Response to How to round date variables to the nearest Monday?
Post a Comment