连续四年保持厂商

Keep firms with four consecutive years

我使用的 ExecuComp/Compustat 数据看起来像这样:

  gvkey|Year |Ticker  |
-------|-----|--------|
 001111| 2006| abc    |
 001111| 2007| abc    |
 001111| 2008| abc    |
 001111| 2009| abc    |
 001112| 2006| www    |
 001112| 2009| www    |
 001113| 2008| ttt    |
 001114| 2010| vvv    |

我怎样才能只保留有任何 4 连续观察年的公司?

我试图阅读 FAQ 和相关的论坛问题,但我似乎无法理解它。

以下对我有用:

clear

input gvkey Year str3 Ticker
001111 2006 abc     
001111 2007 abc     
001111 2008 abc     
001111 2009 abc     
001112 2006 www     
001112 2009 www     
001113 2008 ttt     
001114 2010 vvv  
end

tsset gvkey Year

tsspell, f(L.Year == .) 

egen tag = max(_seq), by(gvkey _spell)

keep if tag == 4

list

     +----------------------------------------------------+
     | gvkey   Year   Ticker   _spell   _seq   _end   tag |
     |----------------------------------------------------|
  1. |  1111   2006      abc        1      1      0     4 |
  2. |  1111   2007      abc        1      2      0     4 |
  3. |  1111   2008      abc        1      3      0     4 |
  4. |  1111   2009      abc        1      4      1     4 |
     +----------------------------------------------------+