Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Keep observations in panel data


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Keep observations in panel data
Date   Tue, 18 Jan 2011 19:16:23 +0000

Here is another way to do it using -tsspell- from SSC:

. tsset id year
. tsspell, fcond(subsidy & !subsidy[_n+1] & !subsidy[_n+2])

. l

     +--------------------------------------------+
     | id   year   subsidy   _spell   _seq   _end |
     |--------------------------------------------|
  1. |  1   2000         0        0      0      0 |
  2. |  1   2001         0        0      0      0 |
  3. |  1   2002         1        1      1      0 |
  4. |  1   2003         0        1      2      0 |
  5. |  1   2004         0        1      3      0 |
     |--------------------------------------------|
  6. |  1   2005         1        1      4      1 |
  7. |  2   2000         0        0      0      0 |
  8. |  2   2001         0        0      0      0 |
  9. |  2   2002         1        0      0      0 |
 10. |  2   2003         1        0      0      0 |
     |--------------------------------------------|
 11. |  2   2004         0        0      0      0 |
 12. |  2   2005         1        0      0      0 |
 13. |  3   2000         1        0      0      0 |
 14. |  3   2001         1        0      0      0 |
 15. |  3   2002         1        0      0      0 |
     +--------------------------------------------+

. drop if _spell == 0 | _seq >= 4

. l

     +--------------------------------------------+
     | id   year   subsidy   _spell   _seq   _end |
     |--------------------------------------------|
  1. |  1   2002         1        1      1      0 |
  2. |  1   2003         0        1      2      0 |
  3. |  1   2004         0        1      3      0 |
     +--------------------------------------------+

Nick 
n.j.cox@durham.ac.uk 

Nick Cox

Here is another way to tackle this that hinges on being able to pack
the history into a string variable:

 gen history = ""
 bysort id (year) : replace history = history[_n-1] + string(subsidy)
 by id : replace history = history[_N]
 by id : gen start = strpos(history, "100") == _n
 by id : keep if start == 1 | start[_n-1] == 1 | start[_n-2] == 1


On Tue, Jan 18, 2011 at 3:29 PM, Nick Cox <njcoxstata@gmail.com> wrote:

> bysort id (year) : keep if
> (subsidy == 1 & subsidy[_n+1] == 0 & subsidy[_n+2] == 0) |
> (subsidy == 0 & subsidy[_n-1] == 1 & subsidy[_n+1] == 0) |
> (subsidy == 0 & subsidy[_n-2] == 1 & subsidy[_n-1] == 0)
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index