Hello,
I'm working with some panel data that looks similar to this:
I'd like to create a new variable that counts the number of rows in which X == 1 sequentially within states, chronologically by year. That would look like this:
But I can't figure out how to achieve this. I've tried
and that ends up counting sequentially when X == 1 within each state, but not chronologically by year.
How would I go about creating this new variable?
Thanks for your help.
I'm working with some panel data that looks similar to this:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str63 state float(year X) "NY" 2000 0 "NY" 2001 1 "NY" 2002 1 "NY" 2003 0 "NY" 2004 1 "NY" 2005 1 "NC" 2000 1 "NC" 2001 1 "NC" 2002 1 "NC" 2003 1 "NC" 2004 1 "NC" 2005 0 end
I'd like to create a new variable that counts the number of rows in which X == 1 sequentially within states, chronologically by year. That would look like this:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str63 state float(year X count) "NY" 2000 0 0 "NY" 2001 1 1 "NY" 2002 1 2 "NY" 2003 0 0 "NY" 2004 1 1 "NY" 2005 1 2 "NC" 2000 1 1 "NC" 2001 1 2 "NC" 2002 1 3 "NC" 2003 1 4 "NC" 2004 1 5 "NC" 2005 0 0 end
But I can't figure out how to achieve this. I've tried
Code:
bysort state X: gen count = _n
How would I go about creating this new variable?
Thanks for your help.

Comment