Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • consecutive observations form specific position in each panel

    Hi, all
    Have a problem when I processing panel data.
    the basic situation is there are more 1,000 firms, have observations from 1998-2007(not necessarily have all of the 10 years for every firm),
    and there would be a firm specific event and some year during its own time horizon.
    what I want to do is to keep all those firms who have five consecutive observations, one year before the event occur, one year of the event, and three after the event occur.

    for example, firm "101113645" 's event year is "2002", so if it has observations like 2001, 2002,2003,2004,2005, I will keep it as my sample, other wise,like you can see in the sample data
    below, event year for this firm is 2002, but the year right before 2002 is 1998, not 2001, I will delete it.


    how could I achieve this? spent a long time on this but still can't fix it.

    I need help!!!


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float year str9 firmid float event_year
    1998 "101101628" 2003
    1999 "101101628" 2003
    2000 "101101628" 2003
    2001 "101101628" 2003
    2002 "101101628" 2003
    2003 "101101628" 2003
    2004 "101101628" 2003
    2005 "101101628" 2003
    2006 "101101628" 2003
    2007 "101101628" 2003
    1998 "101105573" 1999
    1999 "101105573" 1999
    2000 "101105573" 1999
    2001 "101105573" 1999
    2002 "101105573" 1999
    1999 "101113645" 2002
    2002 "101113645" 2002
    2003 "101113645" 2002
    2004 "101113645" 2002
    2005 "101113645" 2002
    end

  • #2
    Code:
    clear
    input float year str9 firmid float event_year
    1998 "101101628" 2003
    1999 "101101628" 2003
    2000 "101101628" 2003
    2001 "101101628" 2003
    2002 "101101628" 2003
    2003 "101101628" 2003
    2004 "101101628" 2003
    2005 "101101628" 2003
    2006 "101101628" 2003
    2007 "101101628" 2003
    1998 "101105573" 1999
    1999 "101105573" 1999
    2000 "101105573" 1999
    2001 "101105573" 1999
    2002 "101105573" 1999
    1999 "101113645" 2002
    2002 "101113645" 2002
    2003 "101113645" 2002
    2004 "101113645" 2002
    2005 "101113645" 2002
    end
    
    egen nyears = total(inrange(year, event_year-1, event_year+3)), by(firmid)
    tabdisp firmid, c(nyears)
    
    ----------------------
       firmid |     nyears
    ----------+-----------
    101101628 |          5
    101105573 |          5
    101113645 |          4
    ----------------------
    
    drop if nyears < 5

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      clear
      input float year str9 firmid float event_year
      1998 "101101628" 2003
      1999 "101101628" 2003
      2000 "101101628" 2003
      2001 "101101628" 2003
      2002 "101101628" 2003
      2003 "101101628" 2003
      2004 "101101628" 2003
      2005 "101101628" 2003
      2006 "101101628" 2003
      2007 "101101628" 2003
      1998 "101105573" 1999
      1999 "101105573" 1999
      2000 "101105573" 1999
      2001 "101105573" 1999
      2002 "101105573" 1999
      1999 "101113645" 2002
      2002 "101113645" 2002
      2003 "101113645" 2002
      2004 "101113645" 2002
      2005 "101113645" 2002
      end
      
      egen nyears = total(inrange(year, event_year-1, event_year+3)), by(firmid)
      tabdisp firmid, c(nyears)
      
      ----------------------
      firmid | nyears
      ----------+-----------
      101101628 | 5
      101105573 | 5
      101113645 | 4
      ----------------------
      
      drop if nyears < 5
      Thanks Nick, you are so great, problem perfectly solved.

      Comment

      Working...
      X