Announcement

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

  • Generate an age variable in panel data.

    Hello,

    I am trying to generate an "age when first married" variable for each woman in my data. I have panel data with three observations for each woman. Using the agefirst variable, I want to take the first non-missing value and record that as their age when first married in a new variable called agemar. The agemar variable would take the same value for each unique woman for each of the three waves.

    I have managed to create this variable, but it seems to be taking the smallest value of agefirst and recording that instead of the first non-missing. I am not sure how to amend it to get the first non-missing instead of the minimum (or smallest age).

    Code:
    sort ID wave
    bysort ID: egen agemar = min(cond(agefirst != ., agefirst, .))


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str23 ID byte(wave marstat agefirst) float agemar
    "0601001005"  6 1  .  .
    "0601001005" 12 1  .  .
    "0601001005" 18 2  .  .
    "0601001006"  6 1  .  .
    "0601001006" 12 1  .  .
    "0601001006" 18 1  .  .
    "0601002402"  6 4 36 36
    "0601002402" 12 4 43 36
    "0601002402" 18 4  . 36
    "0601002405"  6 1  .  .
    "0601002405" 12 1  .  .
    "0601002405" 18 2  .  .
    "0601002505"  6 1  .  .
    "0601002505" 12 1  .  .
    "0601002505" 18 2  .  .
    "0601003805"  6 1  .  .
    "0601003805" 12 1  .  .
    "0601003805" 18 2  .  .
    "0601004004"  6 4 18 18
    "0601004004" 12 4 18 18
    "0601004004" 18 4 18 18
    "0601004006"  6 1  .  .
    "0601004006" 12 1  .  .
    "0601004006" 18 1  .  .
    "0601004603"  6 4 23 23
    "0601004603" 12 4 23 23
    "0601004603" 18 4 23 23
    "0601004604"  6 1  .  .
    "0601004604" 12 1  .  .
    "0601004604" 18 1  .  .
    "0601005902"  6 4 24 24
    "0601005902" 12 4 24 24
    "0601005902" 18 4 24 24
    "0601005903"  6 1  .  .
    "0601005903" 12 1  .  .
    "0601005903" 18 1  .  .
    "0601006004"  6 1  .  .
    "0601006004" 12 1  .  .
    "0601006004" 18 1  .  .
    "0601006704"  6 1  .  .
    "0601006704" 12 1  .  .
    "0601006704" 18 2  .  .
    "0601009005"  6 1  .  .
    "0601009005" 12 1  .  .
    "0601009005" 18 2  .  .
    "0601009205"  6 1  .  .
    "0601009205" 12 1  .  .
    "0601009205" 18 2  .  .
    "0601011602"  6 6 23 23
    "0601011602" 12 6 26 23
    "0601011602" 18 6 29 23
    "0601012406"  6 4 22 22
    "0601012406" 12 4 24 22
    "0601012406" 18 4 22 22
    "0601012407"  6 1  .  .
    "0601012407" 12 1  .  .
    "0601012407" 18 1  .  .
    "0601012705"  6 1  .  .
    "0601012705" 12 1  .  .
    "0601012705" 18 1  .  .
    "0601013205"  6 1  .  .
    "0601013205" 12 1  .  .
    "0601013205" 18 2  .  .
    "0601013703"  6 1  .  .
    "0601013703" 12 1  .  .
    "0601013703" 18 2  .  .
    "0601013704"  6 1  .  .
    "0601013704" 12 1  .  .
    "0601013704" 18 1  .  .
    "0601013705"  6 4 21 21
    "0601013705" 12 6 21 21
    "0601013705" 18 6  . 21
    "0601014605"  6 1  .  .
    "0601014605" 12 1  .  .
    "0601014605" 18 2  .  .
    "0601018905"  6 1  .  .
    "0601018905" 12 1  .  .
    "0601018905" 18 2  .  .
    "0601019206"  6 4 18 18
    "0601019206" 12 4 18 18
    "0601019206" 18 4 18 18
    "0601019209"  6 4 17 17
    "0601019209" 12 4 20 17
    "0601019209" 18 4 27 17
    "0601019210"  6 1  .  .
    "0601019210" 12 1  .  .
    "0601019210" 18 1  .  .
    "0601019307"  6 4 19 19
    "0601019307" 12 4 19 19
    "0601019307" 18 4  . 19
    "0601019309"  6 1  .  .
    "0601019309" 12 1  .  .
    "0601019309" 18 1  .  .
    "0601019310"  6 1  .  .
    "0601019310" 12 1  .  .
    "0601019310" 18 1  .  .
    "0601019706"  6 1  .  .
    "0601019706" 12 1  .  .
    "0601019706" 18 2  .  .
    "0601020203"  6 4 20 17
    end
    label values wave year
    label def year 6 "2006", modify
    label def year 12 "2012", modify
    label def year 18 "2018", modify
    label values marstat Lmarital
    label def Lmarital 1 "less than minimum age", modify
    label def Lmarital 2 "never married", modify
    label def Lmarital 4 "married", modify
    label def Lmarital 6 "widowed(er)", modify

  • #2
    Indeed. You asked for min() which means smallest.

    There is an FAQ https://www.stata.com/support/faqs/d...t-occurrences/ on first occurrences.

    That aside, I don't understand why the minimum is not what you seek here.

    Comment

    Working...
    X