I may have found a use for m:m merging--I'm wondering if this is correct/if there is a simpler way of doing this.
I have data on firms that made multiple different payments each year over a number of years, sometimes making no payments in a given year. There's about 800k observations, I'm using Stata 13.
My data looks something like:
Firm Year Payment
1 2013 100
1 2013 20
1 2012 100
2 2013 10
2 2012 50
3 2012 50
4 2009 70
4 2010 80
...
I'm trying to generate variables that are yearly totals for each firm & that have that value for every observation for the firm, provided that it made a payment in that year.
Firm Year Payment 2013total 2012total ...
1 2013 100 120 100
1 2013 20 120 100
1 2012 100 120 100
2 2013 10 10 50
2 2012 50 10 50
3 2012 50 . 50
4 2009 70
4 2010 80 . .
...
Here's what I came up with:
preserve
keep if year==2013
bysort Firm: egen total2013=total(payment)
save "x.dta"
restore
merge m:m firm using "x.dta"
save
// rinse and repeat
...
This seems to do the trick. Is this (finally) a necessary use of m:m? & Is there a simpler way to do this? This feels like the long way 'round.
Thanks!
Comment