Announcement

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

  • create variables for number of hh member by categories

    Hi Stata expert.
    I'd like to regress an employment model. I have variables and datasets as shown below. hhid14 is the household's ID, and pidlink is the individual id within the household.
    I want to create new dummy variables containing the number of household members by age categories: 0-9; 10-14; 15-24; 25-45; 46-65; and >=66 years. In addition, I also want to make the variables for the number of household members who are: females aged above15 years; the number of members with the highest education level for no schooling, junior high, senior high, and postgraduates (post14); and finally the number of household members aged 15-21 years who are still in school. All of these variables are calculated excluding the corresponding respondent. Can anyone help me with the Stata syntax for this? Thank you in advance.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 hhid14 str10 pidlink double(sex ageyr no_school14 junior14 senior14 post14 attn_sch14 work15)
    "0012451" "001240005" 0 37 1 0 0 0 3 1
    "0012451" "001245101" 1 50 0 0 0 0 3 1
    "0012451" "001245103" 1 21 0 0 0 1 1 1
    "0012451" "001245104" 0 18 0 1 0 0 1 1
    "0012451" "001245105" 0 13 0 0 0 0 1 1
    "0012451" "001245106" 0 11 0 0 0 0 1 1
    "0012451" "001245107" 0  7 1 0 0 0 1 3
    "0012452" "001240009" 0 24 0 1 0 0 3 1
    "0012452" "001245201" 1 30 0 1 0 0 3 1
    "0012452" "001245203" 1  8 0 0 0 0 1 3
    end
    label values sex sex
    label def sex 0 "0.F", modify
    label def sex 1 "1.M", modify

  • #2
    Code:
    label values sex sex
    label def sex 0 "0.F", modify
    label def sex 1 "1.M", modify
    
    label define agegroup   1   "0-9"   ///
                            2   "10-14" ///
                            3   "15-24" ///
                            4   "25-45" ///
                            5   "46-65" ///
                            6   ">=66"
    gen byte agegroup:agegroup =    1   if inrange(ageyr, 0, 9)
    replace agegroup =  2   if inrange(ageyr, 10, 14)
    replace agegroup =  3   if inrange(ageyr, 15, 24)
    replace agegroup =  4   if inrange(ageyr, 24, 45)
    replace agegroup =  5   if inrange(ageyr, 46, 65)
    replace agegroup =  6   if inrange(ageyr, 66, .)
    
    by hhid14 pidlink agegroup, sort: gen hh_agegroup_count = _N
    by hhid14: egen hh_females_gt_15 = total(sex == 0 & inrange(ageyr, 15, .))
    foreach v of varlist no_school14-post14 {
        by hhid14: egen hh_`v' = total(`v' == 1)
    }
    by hhid14: egen hh_15_21_in_school ///
        = total(inrange(ageyr, 15, 21) & attn_sch14 == 1)

    Comment


    • #3
      Dear Clyde,
      thank you very much for the help. It worked perfectly.

      Comment

      Working...
      X