Announcement

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

  • Generate a new variable

    I am trying to generate a variable for the age of the oldest male in the household.
    So, I am using the variables from the household roster in the NIDS dataset.
    I have identified variables such as a household residence or living in the household, gender (male) and year of birth (age).
    I want to generate or create a variable for the age oldest male in the household.
    Then, I want to place him (male) side by side with the female that is receiving benefit within the same household.

    Please see what I have done in my dofile;

    ******** generating age of the oldest male ********// generating male age

    *1* living in the residence according to household roster
    codebook r_pres
    numlabel, add
    tab r_pres
    gen hhres=.
    replace hhres=1 if r_pres==1
    *replace hhres=0 if r_pres==2
    *drop if r_pres==3
    * // Just rename
    *ren r_pres hhres

    *2* year of birth to generate age in the household roster
    codebook r_dob_y
    tab r_dob_y
    drop if r_dob_y==9999
    ren r_dob_y ageh
    gen yr=2017
    gen agehh=yr-ageh
    tab agehh

    *3* gender / sex in the household roster
    br r_gen
    *ren r_gen // male = 1, female = 2 //
    gen sexM=.
    replace sexM=1 if r_gen==1
    replace sexM=0 if r_gen==2
    tab sexM


    *4* females receiving the benefit
    OAPr


    */ age of the oldest male living in the household (AOmhh) ********//

    egen AOmhh= max(agehh) if sexM==1 & hhres==1, by (pid)


    Please, how do I generate a variable age of the oldest male in the household?

    Thanks in anticipation for your help.


  • #2
    Welcome to the Statalist. Your questions is solely about Stata, and not related to Mata so it is not posted in the correct forum. If you want faster responses, you can post your questions in the General Stata forum. That said below is the answer to your question:

    If you don't have ereplace, you can first get ereplace and do the following:
    Code:
    ssc install ereplace
    //hhvar is for your household indicator variable whatever it is
    bys hhvar: egen AOmhh = max(agehh) if sexM==1
    bys hhvar: ereplace AOmhh = max(AOmhh)

    Comment


    • #3
      Thank you very, the code really helps. It is much appreciated.

      Comment


      • #4
        You don't need ereplace for this at all.

        Code:
         
         bysort hhvar: egen AOmhh = max(cond(sexM == 1, agehh, .))
        See Sections 9 and 10 in https://www.stata-journal.com/articl...article=dm0055

        Comment

        Working...
        X