Announcement

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

  • generate retirement age

    Hey all,

    I am using panel data and have a full population dataset. I can observe an individuel over time, their age and their retirement status (dummy variable). I would like to generate a variable over their retirement age by ID.

    I tried the following code:

    by ID_nr: gen retirement_age = age if retirement == 1
    by ID_nr: replace retirement_age = min(retirement_age)

    However, it does unfortunately not work.

    Can anyone help?

    Thank you in advance!

  • #2
    Code:
    gen wanted = age if retirement
    bys id (wanted): replace wanted = wanted[1]

    Comment


    • #3
      Thanks! It works

      Comment


      • #4
        To show why the code in post #1 almost worked
        Code:
        by ID_nr: gen ra = age if retirement == 1 
        by ID_nr: egen retirement_age = min(ra)
        drop ra
        The min() function on the generate command finds the minimum of its arguments in a single observation; the egen min() function finds the minimum of its single argument n across observations.

        This can be shortened to

        Code:
        by ID_nr: egen retirement_age = min( cond(retirement==1, age, .) )
        which finds the minimum value of the expression that evaluates to age if the individual is retired and a missing value otherwise.

        Comment

        Working...
        X