Dear statalist,
I have a dataset from 2009 to 2019, the main variables are Year, Symbol, and status. Status takes three values, =1 for mandatory, =2 for voluntary, =3 for non. I want to create an indicator variable, voluntary_past_any, equals one if a firm has been in status 2 (voluntary) in any one or more of the past years. Say now is 2012, firm A was in status 1 in 2009, status 2 in 2010-2011, and status 3 in 2012. So firm A should be coded 1 for voluntary_past_any in 2012, as it used to be in status 2 in at least one of the years prior to the current year (i.e., 2012). If now is 2011, firm A should also be coded 1 for voluntary_past_any, but not so if now is 2010. I wonder how to write code to generate this variable.
Btw, in my dataset, it is quite often that firms switch from one status to another over the sample period, even though the firm may not be in status 2 in the current year, as long as it has ever been in status 2 for at least once prior to the current year, voluntary_past_any should be coded 1 for the current year.
I tried:
but it doesn't work as I want.
Thanks a lot in advance for any kind help.
I have a dataset from 2009 to 2019, the main variables are Year, Symbol, and status. Status takes three values, =1 for mandatory, =2 for voluntary, =3 for non. I want to create an indicator variable, voluntary_past_any, equals one if a firm has been in status 2 (voluntary) in any one or more of the past years. Say now is 2012, firm A was in status 1 in 2009, status 2 in 2010-2011, and status 3 in 2012. So firm A should be coded 1 for voluntary_past_any in 2012, as it used to be in status 2 in at least one of the years prior to the current year (i.e., 2012). If now is 2011, firm A should also be coded 1 for voluntary_past_any, but not so if now is 2010. I wonder how to write code to generate this variable.
Btw, in my dataset, it is quite often that firms switch from one status to another over the sample period, even though the firm may not be in status 2 in the current year, as long as it has ever been in status 2 for at least once prior to the current year, voluntary_past_any should be coded 1 for the current year.
I tried:
Code:
by Symbol (Year), sort: gen byte voluntary_past_any = (Year[_n-1] > Year-10) if _n > 1 & status == 2
Thanks a lot in advance for any kind help.
Comment