Announcement

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

  • Identify panel

    Hi,

    Hope you are doing great. I am working with panel data, I have several periods where the people I follow change. The only condition is that I follow each individual for 5 consecutive periods. I want to create a sub-panel where I keep all the observations of individuals that appear in my data for a particular period, year 2015. ID is the variable I use to uniquely identify each individual.
    ID Year
    1 2001
    1 2002
    1 2003
    1 2004
    1 2005
    2 2002
    2 2003
    2 2004
    2 2005
    2 2006
    . .
    . .
    . .
    10 2012
    10 2013
    10 2014
    10 2015
    10 2016
    Any ideas on how can I do that?

    Thanks,

    Tessa

  • #2
    I'm not sure I have properly understood what you want. The following code will identify all id's that have at least 5 consecutive years of data. It will then keep the year 2015 observations of all those id's (even if the 2015 observation is not among 5 consecutive years). This may be an overly literal reading of what you intended. If it isn't what you want, please post back with a clearer explanation.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id int year
     1 2001
     1 2002
     1 2003
     1 2004
     1 2005
     2 2002
     2 2003
     2 2004
     2 2005
     2 2006
    10 2012
    10 2013
    10 2014
    10 2015
    10 2016
    end
    
    
    by id (year), sort: gen run = sum(year != year[_n-1] + 1)
    by id run (year), sort: gen duration = year[_N] - year[1] + 1
    by id (duration run year), sort: gen byte has_5_consecutive_years ///
        = duration[_N] >= 5
        
    keep if has_5_consecutive_years & year == 2015
    Note: In the example data, every id has at least five consecutive years of data, so the code has not been tested for the case of id's that do not meet this criterion. If you find the code does not perform as needed in this regard, please post back with new example data that includes both id's that do and id's that don't meet the criterion.

    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Hi, thanks. Sorry I didn't explain myself clearly. What I want is to keep all the observations of the individuals (IDs) I have information for in 2015. So if I have information por individual A in 2012, 2013, 2014, and 2015, I wan to keep all the observations of that person. However, if I have information for individual B in 2012, 2013, and 2014, I want to delete that individual from my dataset. Also, I can follow individuals for 5 consecutive periods maximum but there are some individual I only follow for 2 or 3 periods.

      Comment


      • #4
        To be honest, I'm still not entirely clear on what you want. My current interpretation is that you want to keep all observations for those IDs that have an observation in 2015 that is part of a group of 5 or more consecutive years. If that's what you want:
        Code:
        by id (year), sort: gen run = sum(year != year[_n-1] + 1)
        by id run (year), sort: gen duration = year[_N] - year[1] + 1
        by id: egen keeper = max((year == 2015) & (duration >= 5))
        keep if keeper

        Comment

        Working...
        X