- * Example generated by -dataex-. To install: ssc install dataex
- clear
- input id year str1 x
- 1 2011 "a"
- 1 2011 "b"
- 1 2012 "a"
- 1 2012 "d"
- 1 2013 "a"
- 1 2013 "d"
- 1 2013 "c"
- 2 2011 "c"
- 2 2011 "d"
- 2 2012 "c"
- 2 2012 "a"
- 2 2013 "c"
- 2 2013 "b"
- end
- bysort id x (year) : gen first = year[1]
- by id x: gen last = year[_N]
- sort id year x
- bysort id year : egen new = total(year == first)
- by id year : egen retiring = total(year == last)
- by id : gen retired = retiring[_n-1] if year > year[_n-1]
- by id year : replace retired = retired[1]
- list, sepby(id year)
- +---------------------------------------------------------+
- | id year x first last new retiring retired |
- |---------------------------------------------------------|
- 1. | 1 2011 a 2011 2013 2 1 . |
- 2. | 1 2011 b 2011 2011 2 1 . |
- |---------------------------------------------------------|
- 3. | 1 2012 a 2011 2013 1 0 1 |
- 4. | 1 2012 d 2012 2013 1 0 1 |
- |---------------------------------------------------------|
- 5. | 1 2013 a 2011 2013 1 3 0 |
- 6. | 1 2013 c 2013 2013 1 3 0 |
- 7. | 1 2013 d 2012 2013 1 3 0 |
- |---------------------------------------------------------|
- 8. | 2 2011 c 2011 2013 2 1 . |
- 9. | 2 2011 d 2011 2011 2 1 . |
- |---------------------------------------------------------|
- 10. | 2 2012 a 2012 2012 1 1 1 |
- 11. | 2 2012 c 2011 2013 1 1 1 |
- |---------------------------------------------------------|
- 12. | 2 2013 b 2013 2013 1 2 1 |
- 13. | 2 2013 c 2011 2013 1 2 1 |
- +---------------------------------------------------------+


雷达卡






京公网安备 11010802022788号







