Dear all,
I'm having a problem coding the first occurrence of certain value equal to 1. I have a panel data with 10 years of observation (at most 10 years, some firms have less than 10 years of observation), the relevant variables are firm, year and value. The value variable can take values 0, 1, or 2. I want to code the first year a firm with value 1 as 1, say the new variable is called "occur1". Here is an example of data
I would like the bold ones to code 1 for occur1.
After reading https://www.stata.com/support/faqs/d...t-occurrences/ I tried
however it only works when firms with value of 0 and 1 but not when there is value 2.
Any suggestions on how to correct this code? Thanks a lot!
Lucy
I'm having a problem coding the first occurrence of certain value equal to 1. I have a panel data with 10 years of observation (at most 10 years, some firms have less than 10 years of observation), the relevant variables are firm, year and value. The value variable can take values 0, 1, or 2. I want to code the first year a firm with value 1 as 1, say the new variable is called "occur1". Here is an example of data
Code:
year firm value 2010 2092 2 2011 2092 2 2012 2092 2 2013 2092 1 2014 2092 1 2015 2092 1 2016 2092 1 2017 2092 2 2018 2092 2 2019 2092 2 2010 2103 0 2011 2103 1 2012 2103 1 2013 2103 1 2014 2103 1 2015 2103 1 2016 2103 1 2017 2103 1 2018 2103 0 2019 2103 0 2010 2106 2 2011 2106 2 2012 2106 2 2013 2106 2 2014 2106 2 2015 2106 1 2016 2106 0 2017 2106 0 2018 2106 0 2019 2106 0 2010 2456 0 2011 2456 0 2012 2456 0 2013 2456 2 2014 2456 0 2015 2456 2 2016 2456 2 2017 2456 2 2018 2456 2 2019 2456 2
After reading https://www.stata.com/support/faqs/d...t-occurrences/ I tried
Code:
by firm (year), sort: gen byte occur1 = sum(value) == 1 & (sum(value[_n-1]) == 0 | sum(value[_n-1]) == 2)
Any suggestions on how to correct this code? Thanks a lot!
Lucy
Comment