售后年份

时间:2019-03-09 08:32:51

标签: stata

我有以下数据:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(cc year sale)
4534288 2016 1
4534288 2017 1
4534288 2018 1
4534289 2016 0
4534289 2017 1
4534289 2018 1
4534290 2016 1
4534291 2017 0
4534291 2018 1
4534292 2016 0
4534292 2017 0
4534292 2018 1
end

我想知道自出售以来已经过去了多少年。这是 由虚拟变量sale显示,当销售发生时1则为0。否则

1 个答案:

答案 0 :(得分:2)

以下对我有用:

clear
input float(cc year sale)
4534288 2016 1
4534288 2017 1
4534288 2018 1
4534289 2016 0
4534289 2017 1
4534289 2018 1
4534290 2016 1
4534291 2017 0
4534291 2018 1
4534292 2016 0
4534292 2017 0
4534292 2018 1
end

bysort cc (year): egen years = seq() if sale

list, sepby(cc)

     +-------------------------------+
     |      cc   year   sale   years |
     |-------------------------------|
  1. | 4534288   2016      1       1 |
  2. | 4534288   2017      1       2 |
  3. | 4534288   2018      1       3 |
     |-------------------------------|
  4. | 4534289   2016      0       . |
  5. | 4534289   2017      1       1 |
  6. | 4534289   2018      1       2 |
     |-------------------------------|
  7. | 4534290   2016      1       1 |
     |-------------------------------|
  8. | 4534291   2017      0       . |
  9. | 4534291   2018      1       1 |
     |-------------------------------|
 10. | 4534292   2016      0       . |
 11. | 4534292   2017      0       . |
 12. | 4534292   2018      1       1 |
     +-------------------------------+