Announcement

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

  • How to Correctly Delete the Data after the First Occurrence of "1" in a Variable within ID with Stata?

    The dataset is listed below, and I want to delete the data after the first occurrence of 1 in the variable "employment" within ID. Also, I want to create a new variable "time", which should be the total number of "1" or "0" after the deletion within ID. Can someone help with Stata code?
    Thank you!
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte (id employment)
    1 1
    1 1
    1 1
    1 0
    1 0
    1 0
    1 0
    1 0
    1 1
    2 0
    2 0
    2 0
    2 0
    2 0
    3 1
    3 1
    3 1
    3 1
    3 1
    3 1
    3 1
    4 1
    4 0
    4 0
    4 0
    4 1
    4 1
    end
    Last edited by smith Jason; 29 Mar 2022, 19:07.

  • #2
    I got the results after consulting with my friends. Anyway, thank you folks!
    bys id: gen time=_n
    bys id (time): keep if sum(employment) <= 1 & sum(employment[_n-1]) == 0
    bys id: replace time = time[_N]
    Last edited by smith Jason; 29 Mar 2022, 21:11.

    Comment


    • #3
      Thank you for following-up with your solution so others can learn from it, too.

      Comment

      Working...
      X