Announcement

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

  • Looking for help with dropping observations in panel data

    I am working with panel data by year. I would like to drop all observations (years) within a panel after an indicator variable has a value of 1. Can anyone suggest code for this? To illustrate: I have observations for each country over a certain time period. At some point in the time period for some of the countries, an indicator variable turns to a value of 1. I want to drop all the years for each country after the indicator variable turns to 1 (so that the country 'exits' the data), only for those countries where the indicator goes to 1 (all other country-observations should stay in the data).

  • #2
    I hope the followings would work. In future, please do a favor by following the FAQ (http://www.statalist.org/forums/help) and use -dataex- to provide some sample data so that it will not be necessary to waste extra time to create the data.

    Code:
    clear
    input str3 country year indicator
    a 2020 .
    a 2021 1
    a 2022 .
    b 2016 .
    b 2017 .
    b 2018 1
    b 2019 .
    b 2020 .
    c 2018 .
    c 2019 .
    c 2020 1
    c 2021 .
    end
    
    gen todrop = indicator
    bysort country (year): replace todrop = todrop[_n-1] if todrop == .
    drop if todrop == 1

    Comment


    • #3
      Good suggestions! The code didn't quite work, because it coded the todrop variable to equal one only during the year that the indicator variable is 1. I wanted the todrop variable to be coded 1 in the years after the indicator variable equaled 1. I did this instead, which worked.

      gen todrop = 0

      bysort country (year): replace todrop = 1 if indicator[_n-1]==1

      bysort country (year): replace todrop = 1 if todrop[_n-1]==1

      drop if todrop==1

      Comment

      Working...
      X