Announcement

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

  • Identify observations in >1 variable

    Hello,

    I wonder if anyone could help with the following:

    I have 2 variables with unique identification (ID) numbers with over 30 years of follow-up time. One is ID numbers for children and the second is the ID numbers of their mothers.
    Example:
    ChildID:-------------MotherID:
    10---------------------------20
    11---------------------------21
    12---------------------------10
    13---------------------------22
    I want to identify children who are now also mothers in the dataset. For example Child ID10 above is now also a mother. Is there a way I can create a variable to identify which children subsequently became mothers based on their ID numbers?

    Thank you,

    Gillian

  • #2
    There are a number of ways of doing this, but I personally like this simple and elegant method from some posts by Romalpa Akzo

    Code:
    input float(ChildID MotherID)
    10 20 
    11 21
    12 10
    13 22
    end
    
    expand 2, gen(x)
    replace ChildID=MotherID if x
    bys ChildID: egen tag= max(ChildID[_n]==ChildID[_n+1])
    keep if x==0
    drop x
    l
    Result:

    Code:
    . l
    
         +--------------------------+
         | ChildID   MotherID   tag |
         |--------------------------|
      1. |      10         20     1 |
      2. |      11         21     0 |
      3. |      12         10     0 |
      4. |      13         22     0 |
         +--------------------------+

    Comment

    Working...
    X