Dear All, I was asked the following question with data:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long stkcd int year byte new
2 2015 0
2 2016 0
2 2017 1
2 2018 0
2 2019 0
4 2015 0
4 2016 0
4 2017 0
5 2015 0
5 2016 1
5 2017 0
5 2018 0
6 2015 0
6 2016 0
6 2017 0
6 2018 0
6 2019 0
7 2015 0
7 2016 1
8 2015 0
8 2016 0
8 2017 0
8 2018 0
8 2019 0
9 2015 0
9 2016 0
9 2017 0
9 2018 0
10 2015 0
10 2016 0
10 2017 0
10 2018 1
end
For each `stkcd', if the values of `new' are all 0s, keep that stkcd, e.g., keeing stkcd=4. For each `stkcd',, if any value of `new' equals 1, drop the observations before the year with new=1, e.g., drop year=2015, and 2016 for stkcd=2. I have the following approach (ssc install rangestat)
Code:
rangestat (max) new, interval(year 1 .) by(stkcd)
drop if new_max == 1
drop new_max
I wonder if there is more concise way to do this. Thanks.