Announcement

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

  • Create new variable in panel data set

    Dear all,

    I am working with a panel data set, and would like to create a new variable which measures the 'age as first-time parent'. I generated a new variable:

    PHP Code:
    gen age_firstborn AGE if year == year_firstborn 
    However I get missing values on all other years for each id.

    How can I fill the age in for each id for all years?

    Best,
    Kamilla


  • #2
    Stata's logic is that you didn't say what you wanted for the other observations. The easiest way forward is likely to be something like

    Code:
    bysort id (age_firstborn) : replace age_firstborn = firstborn[1]
    There are one-line solutions, such as

    Code:
    egen age_firstborn = min(cond(year == year_firstborn, AGE, .)), by(id)
    on which see Section 9 of https://www.stata-journal.com/articl...article=dm0055 The other sections of the paper may well be useful.

    Comment


    • #3
      Thank you, Nick! This was exactly what I needed

      Comment


      • #4
        Hi Nick
        I took a look at the paper to figure out my next problem, but I got stock on the way..
        What I want to do is to create a new wage variable equal to the wage if wage>1. For the missing values I want to insert the mean of the wage in the last three years.

        I generated the following variable

        Code:
        gen new_wage = wage if wage >1
        For the missing values I want to generate a mean, and have used the following code, but it is not working

        Code:
        egen mean_wage = mean(wage) if inrange(year, year-3,year-1)
        Do you have any suggestions on how I could solve this problem?

        Comment


        • #5
          #2 should be


          Code:
           bysort id (age_firstborn) : replace age_firstborn = age_firstborn[1]

          #4 That sounds quite a different problem. I think I need to see a data example to understand what you want.

          Comment

          Working...
          X