楼主: 日复一日12
18623 24

[数据管理求助] stata 如何留下至少有连续5年的观测? [推广有奖]

11
黃河泉 在职认证  发表于 2017-3-4 12:14:47
但若是
  1. clear
  2. input stkcd year x
  3. 1 2000 .
  4. 1 2001 1
  5. 1 2002 2
  6. 1 2003 .
  7. 1 2004 3
  8. 1 2005 4
  9. 1 2006 .
  10. 1 2007 5
  11. 1 2008 6
  12. 2 2000 .
  13. 2 2001 1
  14. 2 2002 2
  15. 2 2003 3
  16. 2 2004 3
  17. 2 2005 4
  18. 2 2006 .
  19. 2 2007 5
  20. 2 2008 6
  21. 3 2000 .
  22. 3 2001 1
  23. 3 2002 2
  24. 3 2003 .
  25. 3 2004 3
  26. 3 2005 4
  27. 3 2006 4
  28. 3 2007 5
  29. 3 2008 .
  30. end

  31. *drop if x == .

  32. tsset stkcd year
  33. xtpattern, gen(pp)                 
  34. gen p5 = strpos(pp, "11111")
  35. drop if p5 == 0
复制代码

上述之方法就不可行(需先 drop x==.)!
已有 1 人评分论坛币 收起 理由
happy_287422301 + 40 鼓励积极发帖讨论

总评分: 论坛币 + 40   查看全部评分

12
黃河泉 在职认证  发表于 2017-3-4 15:43:50
Clyde 提供下列作法:
  1. * Example generated by -dataex-. To install: ssc install dataex
  2. clear
  3. input float(stkcd year x)
  4. 1 2000 .
  5. 1 2001 1
  6. 1 2002 2
  7. 1 2003 .
  8. 1 2004 3
  9. 1 2005 4
  10. 1 2006 .
  11. 1 2007 5
  12. 1 2008 6
  13. 2 2000 .
  14. 2 2001 1
  15. 2 2002 2
  16. 2 2003 3
  17. 2 2004 3
  18. 2 2005 4
  19. 2 2006 .
  20. 2 2007 5
  21. 2 2008 6
  22. 3 2000 .
  23. 3 2001 1
  24. 3 2002 2
  25. 3 2003 .
  26. 3 2004 3
  27. 3 2005 4
  28. 3 2006 4
  29. 3 2007 5
  30. 3 2008 .
  31. end

  32. by stkcd (year), sort: ///
  33.     gen run = sum(missing(x) != missing(x[_n-1]) | year > year[_n-1]+1)
  34. by stkcd run (year), sort: gen run_length = _N
  35. by stkcd: egen longest_non_missing_run_length = max(cond(!missing(x), run_length, .))
  36. keep if longest_non_missing_run_length >= 5
复制代码

In Stata terminology, in your example, all of your stkcd's in the example have at least 5 consecutive years of observations. I assume what you mean is 5 consecutive years of observations with non-missing values of x. I notice that in your example data there are no gaps in the year variable, but the code above does not rely on that--it will work correctly if there are some gaps.

The logic is that a spell of consecutive non-missing observations begins, with the observations are sorted by year within stkcd, when x is not missing but x[_n-1] is. Here we actually start be counting runs of consecutive missing or consecutive non-missing observations. Then each run has a length. Then we calculate the length of the longest run of non-missing values. And we keep those where that longest run's length is at least 5.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
小七六六 + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

13
绷场面4 发表于 2018-9-28 09:20:39
bysort var: drop if _N<5

14
黃河泉 在职认证  发表于 2018-9-28 09:48:07
绷场面4 发表于 2018-9-28 09:20
bysort var: drop if _N
楼主是要"有连续5年的公司"。

15
李锐昌 发表于 2021-3-8 17:15:58
黃河泉 发表于 2017-3-4 12:14
但若是
上述之方法就不可行(需先 drop x==.)!
黄老师您好,如果一家公司只保留连续5年以上的观测值,对于不连续或连续年份少于5年的观测值需要删除,该如何操作呢?
例如公司A有2007、 2009、 2011、 2013、 2014、 2015、 2016、 2017、 2018的观测值,只保留2013-2018这些连续5年以上的样本,而2007、2009、2011需要删除。

16
黃河泉 在职认证  发表于 2021-3-9 07:37:07
李锐昌 发表于 2021-3-8 17:15
黄老师您好,如果一家公司只保留连续5年以上的观测值,对于不连续或连续年份少于5年的观测值需要删除,该 ...
你若要问程序,请用 (ssc install) dataex 附上相关`代表性’资料 (请尽量不要用截图或其他格式)。请参考说明 https://bbs.pinggu.org/thread-5048204-1-1.htmlhttps://zhuanlan.zhihu.com/p/29911577

17
bet132186 发表于 2021-3-16 20:10:56
李锐昌 发表于 2021-3-8 17:15
黄老师您好,如果一家公司只保留连续5年以上的观测值,对于不连续或连续年份少于5年的观测值需要删除,该 ...
巧了老弟,想出办法来了没有

18
蛮月69 发表于 2022-8-17 19:00:47
bet132186 发表于 2021-3-16 20:10
巧了老弟,想出办法来了没有
你好,请问对于这个问题您解决了吗?

19
蛮月69 发表于 2022-8-17 19:01:32
李锐昌 发表于 2021-3-8 17:15
黄老师您好,如果一家公司只保留连续5年以上的观测值,对于不连续或连续年份少于5年的观测值需要删除,该 ...
你好,请问这个问题您解决了吗?

20
我是工作狂2009 发表于 2022-11-25 11:03:13
李锐昌 发表于 2021-3-8 17:15
黄老师您好,如果一家公司只保留连续5年以上的观测值,对于不连续或连续年份少于5年的观测值需要删除,该 ...
有方法了吗!求助

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-25 04:41