Hello, I have at hand a dataset that resembles the toy example I'm reporting below. I have data about car accidents in a number of counties that are reported monthly as a "moving sum" -- so to speak -- of the past 12 months, and this data is available for the past 5 years. My aim is to retrieve -- if possible -- the count of accidents for a given county in a given month and year; intuitively I should try some kind of recursive pairwise difference between subsequent annual counts, but I have no idea how to get there.
start_date and end_date refer to the first and last month the sum of the accidents (tot_accidents) refers to. As you can see, the count of the first observation is the sum of accidents in County 1 from January 2019 to December 2019; the second observation is the sum of accidents in County 1 from February 2019 to January 2020, and so on. My aim is to retrieve in some way the monthly figure for each month and county, therefore my target dataset should look like this:
Can anyone help me out?
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str6 start_date str7 end_date byte county_id int tot_accidents "2019m1" "2019m12" 1 635 "2019m2" "2020m1" 1 674 "2019m3" "2020m2" 1 592 "2019m4" "2020m3" 1 598 "2019m5" "2020m4" 1 618 "2019m1" "2019m12" 2 376 "2019m2" "2020m1" 2 389 "2019m3" "2020m2" 2 342 "2019m4" "2020m3" 2 318 "2019m5" "2020m4" 2 365 end
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str6 month byte county_id str1 monthly_accidents "2019m1" 1 "?" "2019m2" 1 "?" "2019m3" 1 "?" "2019m4" 1 "?" "2019m5" 1 "?" "2019m1" 2 "?" "2019m2" 2 "?" "2019m3" 2 "?" "2019m4" 2 "?" "2019m5" 2 "?" end
Comment