Announcement

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

  • how to identify the parent sex within the same household

    I would like to identify the parent sex of each person within the same household in stata, but not sure how to do:

    original dataset:

    SUID ADDID PNUM PNPT sex
    1 11 101 . 1
    1 11 102 . 2
    1 11 103 101 1
    1 11 104 101 1

    expected output:

    SUID ADDID PNUM PNPT sex parent_sex
    1 11 101 . 1 .
    1 11 102 . 2 .
    1 11 103 101 1 1
    1 11 104 101 1 1

    Thank you for your time to look into this question!

  • #2
    rangestat from SSC can help here. rangestat is a good search word for this forum.

    I am guessing that SUID identifies households. If it's really ADDID, or SUID ADDID jointly, change the by() call accordingly.

    Code:
    clear 
    input SUID ADDID PNUM PNPT sex
    1 11 101 . 1
    1 11 102 . 2
    1 11 103 101 1
    1 11 104 101 1
    end 
    
    gen PNPT2 = cond(PNPT == ., 0, PNPT )
    rangestat parent_sex=sex, int(PNUM PNPT2 PNPT2) by(SUID)
    
    list 
    
         +-----------------------------------------------------+
         | SUID   ADDID   PNUM   PNPT   sex   PNPT2   parent~x |
         |-----------------------------------------------------|
      1. |    1      11    101      .     1       0          . |
      2. |    1      11    102      .     2       0          . |
      3. |    1      11    103    101     1     101          1 |
      4. |    1      11    104    101     1     101          1 |
         +-----------------------------------------------------+

    Comment


    • #3
      I agree with Nick that -rangestat- is the best way to solve this particular problem. But there is another approach that can handle more complicated situations that sometimes arise with problems like this one (but don't arise in this particular instance). So it might be worth taking a look at:
      Code:
      frame put SUID ADDID PNUM sex, into(working)
      frlink m:1 SUID ADDID PNPT, frame(working SUID ADDID PNUM)
      frget parent_sex = sex, from(working)
      Note: Contra #2, this code assumes that the combination of SUID ADDID and PNUM is required to identify distinct persons. It is agnostic as to whether ADDID combines with SUID to define families or combines with PNUM to define individual people. If ADDID is not actually needed, then, just remove it from the code everywhere it appears.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        rangestat from SSC can help here. rangestat is a good search word for this forum.

        I am guessing that SUID identifies households. If it's really ADDID, or SUID ADDID jointly, change the by() call accordingly.

        Code:
        clear
        input SUID ADDID PNUM PNPT sex
        1 11 101 . 1
        1 11 102 . 2
        1 11 103 101 1
        1 11 104 101 1
        end
        
        gen PNPT2 = cond(PNPT == ., 0, PNPT )
        rangestat parent_sex=sex, int(PNUM PNPT2 PNPT2) by(SUID)
        
        list
        
        +-----------------------------------------------------+
        | SUID ADDID PNUM PNPT sex PNPT2 parent~x |
        |-----------------------------------------------------|
        1. | 1 11 101 . 1 0 . |
        2. | 1 11 102 . 2 0 . |
        3. | 1 11 103 101 1 101 1 |
        4. | 1 11 104 101 1 101 1 |
        +-----------------------------------------------------+
        Thank you Nick! It works.

        Comment

        Working...
        X