Search code examples
loopsstatapanel

Keep observations that appear consecutively 5 years before and after the event


I have an unbalanced panel dataset in Stata and am conducting an event study. The variable window counts years before or after the event happens for each firm. I want to keep only those firms that are observed for the window [-5, 5]. In the example dataset below, that means that id=1 should be kept in the data and id=2 should be dropped because two years before the event the id was not observed, that is window = -2 is absent.

How to write a loop that keeps units that are observed only for -5 to 5 consecutive years before and after the event, so to make the data balanced in Stata? Example


Solution

  • The criterion is that observations are present for all the years in each window -5(1)5, which is that the result of

      egen count = total(inrange(window, -5, 5)), by(id) 
    

    is always 11, so that you perhaps proceed with

      keep if count == 11 
      keep if inrange(window, -5, 5) 
    

    No loops needed.