Announcement

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

  • Create a new variable by group

    I´m trying to create a new variable. I defined from folioviv, foliohog and parentesco each household, identifyng if the person in the household is the father (parentesco=101), mother (parentesco=201) and children (parentesco=301).
    So basically, each household has contained all members, but i need create a new variable complete=1 if the household has father, mother and children, incomplete otherwise, but when I use
    by household: gen complete=1 if parentesco==101 & parentesco==201 & parentesco==301 it does not generate any value because the conditions are excludible by row but n by group.
    Does anybody know how to create ir? I'm going to let some sample from my base. And thanks so much


    folioviv foliohog parentesco household Complete
    1 1 101 1
    1 1 201 1
    1 1 301 1
    1 2 101 2
    1 2 201 2
    2 1 201 3
    2 1 301 3
    3 1 101 4
    3 1 201 4
    3 1 301 4

  • #2
    Code:
    egen byte has_father = max(parentesco == 101), by(household)
    egen byte has_mother = max(parentesco == 201), by(household)
    egen byte has_child = max(parentesco == 301), by(household)
    
    gen byte complete = has_father & has_mother & has_child
    drop has_father has_mother has_child

    Comment


    • #3
      Thank u so much, it was really usefull

      Comment

      Working...
      X