Announcement

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

  • drop obeservations according to some rules

    Hello!

    I have a set of firms for the sample period 2010-2019 (some firms may have less than 10 years' records), there is a variable "first", for each firm-year observation. It is an indicator variable that =1 if it is the first time the firm meet some condition. A firm can only be "first" once throughout the sample period, and it is possible that some firms would never be "first" in the sample period. I want to delete all the observations after the year the firm coded 1 for "first", how can I achieve this?

    Here is some data:
    Code:
    year    firm    first
    2010    0035    0
    2011    0035    0
    2012    0035    0
    2013    0035    0
    2014    0035    1
    2015    0035    0
    2016    0035    0
    2017    0035    0
    2018    0035    0
    2019    0035    0
    2013    0042    1
    2014    0042    0
    2015    0042    0
    2016    0042    0
    2017    0042    0
    2018    0042    0
    2019    0042    0
    2010    0050    0
    2011    0050    0
    2012    0050    0
    2013    0050    0
    2014    0050    0
    2015    0050    0
    2016    0050    0
    2017    0050    0
    2018    0050    0
    2019    0050    0
    And after deletion I would like the data looks like this:
    Code:
    year    firm    first
    2010    0035    0
    2011    0035    0
    2012    0035    0
    2013    0035    0
    2014    0035    1
    2013    0042    1
    2010    0050    0
    2011    0050    0
    2012    0050    0
    2013    0050    0
    2014    0050    0
    2015    0050    0
    2016    0050    0
    2017    0050    0
    2018    0050    0
    2019    0050    0
    Thanks a lot for any kind help!

  • #2
    I think I solved it after reading https://www.statalist.org/forums/for...ce-of-an-event (sorry I searched but didn't see this post before I ask this question)
    Code:
    by firm (year), sort: keep if sum(first) <= 1 & sum(first[_n-1]) == 0
    Although I tried and think it works, can anyone confirm me this is correct or not? Thanks!

    Comment


    • #3
      I agree that what you have is correct, and also note that an alternative, at least for your example data, is
      Code:
      by firm (year), sort: keep if sum(first) == 0 | first == 1

      Comment


      • #4
        Thanks William, your suggested code is neater

        Comment

        Working...
        X