Announcement

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

  • drop/keep with multiple conditional statement

    Hi everyone,
    A sample of my data looks as follows (my current data). I am trying to keep those observations whose marital status is married in at least one year. In other words, I wanna keep him/her in the data even if his/her marital status changes afterwards. Likewise, if a person's marital status is unmarried, I wanna drop him/her out. Thanks.
    Nader



    *My current data
    Code:
    clear all
    input id year str16 (marital)
    1 2000 "married"
    1 2002 "married"
    1 2004 "married"
    2 2000 "never married"
    2 2002 "never married"
    3 2000 "never married"
    3 2002 "married"
    3 2004 "divorced"
    4 2002 "never married"
    4 2004 "married"
    5 2002 "divorced"
    5 2004 "married"
    end
    list
    *My goal
    Code:
    clear all
    input id year str16 (marital)
    1 2000 "married"
    1 2002 "married"
    1 2004 "married"
    3 2002 "married"
    3 2004 "divorced"
    4 2004 "married"
    5 2004 "married"
    end
    list

  • #2
    Code:
    by id, sort: egen ever_married = max(marital == "married")
    keep if ever_married

    Comment


    • #3
      This is great! Thank. Is there any way to drop observations (not cases) if the corresponding marital status until it becomes married. For example, if an observation is unmarried, for example in 2000, but becomes married in 2002 and once again becomes divorced in 2004 then I wanna drop out 2000 but keep 2002 and afterwards as his/her status is married in 2002 but not before that.
      Last edited by Nader Mehri; 26 Dec 2019, 22:56.

      Comment


      • #4
        Well, your question strikes me as unclear. Look at id 5, who is "divorced" in 2002 and "married" in 2004. In order to be divorced in 2002, he or she must have been married in some previous year., though that is not recorded in the data. So how do you want to handle this? Is id 5's 2002 observation kept or dropped?

        Comment

        Working...
        X