Announcement

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

  • Long dataset: remove observations based on values of other observations of the same group (company)

    Dear all,

    I'm trying to figure out how to delete observations based on the value of a previous observation.
    Namely, I have companies (BvdIdNumber) for different years (t) with an outcome dummy variable firm survival (1 = survived).

    I am trying to delete the observations for the subsequent years after the first observation of a company has a 0 for the variable firm survival.

    Below an example of the dataset.
    For the first company (AT9010101806) I would only like to keep observations for t0-t8, as t8 is the first time a 0 appears for firm survival and I do not want to keep a failed company in my dataset after the first year of failure.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str16 BvdIdNumber byte t float(Gender Age Nationality FirmSurvival)
    "AT9010101806"  0 0  .01408922       .5 1
    "AT9010101806"  1 0  .01381379       .5 1
    "AT9010101806"  2 0  .01354892       .5 1
    "AT9010101806"  3 0  .01329402       .5 1
    "AT9010101806"  4 0  .01304853       .5 1
    "AT9010101806"  5 0 .012811944       .5 1
    "AT9010101806"  6 0 .012583784       .5 1
    "AT9010101806"  7 0 .012363608       .5 1
    "AT9010101806"  8 0 .012151004       .5 0
    "AT9010101806"  9 0 .011945589       .5 0
    "AT9010101806" 10 0 .011747003       .5 0
    "AT9010131067"  0 0          0        0 1
    "AT9010131067"  1 0          0        0 1
    "AT9010131067"  2 0          0        0 1
    "AT9010131067"  3 0          0        0 1
    "AT9010131067"  4 0          0        0 1
    "AT9010131067"  5 0 .037223335        0 1
    "AT9010131067"  6 0  .03663288        0 1
    "AT9010131067"  7 0  .03606087        0 1
    "AT9010131067"  8 0 .035506442        0 1
    "AT9010131067"  9 0 .034968805        0 1
    "AT9010131067" 10 0 .034447208        0 1
    "AT9010132938"  0 0   .1627238        0 1
    "AT9010132938"  1 0   .1598938        0 1
    "AT9010132938"  2 0  .15716057        0 1
    "AT9010132938"  3 0   .1545192        0 1
    "AT9010132938"  4 0  .15196514        0 1
    "AT9010132938"  5 0  .16941477 .4444444 0
    "AT9010132938"  6 0   .1665105 .4444444 0
    "AT9010132938"  7 0  .16370414 .4444444 0
    "AT9010132938"  8 0   .1609908 .4444444 0
    "AT9010132938"  9 0  .15836594 .4444444 0
    "AT9010132938" 10 0   .1558253 .4444444 0
    
    end
    Thank you in advance for your help and time.

    Best regards,
    Laura

  • #2
    Code:
    bysort BvdIdNumber (t): keep if FirmSurvival[_n-1]

    Comment


    • #3
      Oyvind Snilsberg, thank you very much, this worked perfectly!

      Comment

      Working...
      X