Announcement

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

  • Help adding a dummy variable


    I am working with longitudinal household data and am (very) stuck at creating a dummy variable that takes the value 1 if a woman is having a child in the following year (so first baby next year is the variable I want to create). Here is a description of the data, where HID is the household ID, PID is the personal ID, and mother ID is the PID of the mother of the person. Any help would be greatly appreciated.
    year HID PID Sex Age MotherID First baby next year
    2019 115 1151 F 30 . 1
    2020 115 1151 F 31 . 0
    2021 115 1151 F 32 . 0
    2020 115 1152 M 1 1151 .
    2021 115 1152 M 2 1151 .
    2021 115 1153 F 1 1151 .







  • #2
    Try the following:

    [CODE]
    replace MotherID=PID // if MotherID==PID, cannot be child
    sort MotherID year Age
    gen baby_next_year=age[_n+1]==1 if PID==MotherID & MotherID==MotherID[_n+1] & PID!=PID[_n+1]
    /CODE]

    Comment


    • #3
      [QUOTE=Daniel Shin;n1701689]Try the following:

      I did this:
      MotherID=PID
      sort MotherID year Age
      gen baby_next_year=age[_n+1]==1 if PID==MotherID & MotherID==MotherID[_n+1] & PID!=PID[_n+1]

      but it didn't work: motherID becomes PID and the baby_next_year remains empty.

      Comment

      Working...
      X