Announcement

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

  • restrict observation households to age of mothers...

    Hello everyone.
    I want to restrict and keep observation of a households data census that
    Only households with mothers and daughters under the age of 55 and over 20 remain


    data example:
    in relation 1= house head
    2=Spouse
    3= son or daughter
    4= father or mother of house head

    in sex 1= male 2=female
    person relation sex age house_id
    01 1 1 43 222
    02 2 2 37 222
    03 3 1 13 222
    01 1 1 49 102
    02 2 2 49 102
    03 3 2 25 102
    04 3 1 22 102
    01 1 1 35 404
    02 2 2 33 404
    03 3 2 09 404
    04 3 1 05 404
    05 3 2 01 404
    01 1 1 72 342
    02 2 2 22 342
    03 3 2 01 342
    01 1 1 78 345
    02 2 2 59 345
    03 3 2 28 345
    01 1 1 36 402
    02 2 2 26 402
    03 3 2 05 402
    01 1 1 61 441
    02 2 2 57 441
    03 3 1 17 441
    01 1 1 27 522
    03 3 1 19 83
    01 1 1 35 483
    02 2 2 30 483
    03 3 1 07 483
    04 3 1 04 483
    01 1 1 30 523
    02 2 2 29 523
    01 1 1 57 524
    02 2 2 52 524
    03 3 1 25 524

    Thank you
    Last edited by Hreza Ata; 11 Apr 2022, 17:42.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str2 person byte(relation sex) str2 age int house_id
    "01" 1 1 "43" 222
    "02" 2 2 "37" 222
    "03" 3 1 "13" 222
    "01" 1 1 "49" 102
    "02" 2 2 "49" 102
    "03" 3 2 "25" 102
    "04" 3 1 "22" 102
    "01" 1 1 "35" 404
    "02" 2 2 "33" 404
    "03" 3 2 "09" 404
    "04" 3 1 "05" 404
    "05" 3 2 "01" 404
    "01" 1 1 "72" 342
    "02" 2 2 "22" 342
    "03" 3 2 "01" 342
    "01" 1 1 "78" 345
    "02" 2 2 "59" 345
    "03" 3 2 "28" 345
    "01" 1 1 "36" 402
    "02" 2 2 "26" 402
    "03" 3 2 "05" 402
    "01" 1 1 "61" 441
    "02" 2 2 "57" 441
    "03" 3 1 "17" 441
    "01" 1 1 "27" 522
    "03" 3 1 "19"  83
    "01" 1 1 "35" 483
    "02" 2 2 "30" 483
    "03" 3 1 "07" 483
    "04" 3 1 "04" 483
    "01" 1 1 "30" 523
    "02" 2 2 "29" 523
    "01" 1 1 "57" 524
    "02" 2 2 "52" 524
    "03" 3 1 "25" 524
    end
    label values relation relation
    label def relation 1 "Head", modify
    label def relation 2 "Spouse", modify
    label def relation 3 "Son/Daughter", modify
    label values sex sex
    label def sex 1 "Male", modify
    label def sex 2 "Female", modify
    
    destring age, replace
    by house_id, sort: egen byte has_mother_20_55 = max(relation == 4 & sex == 2 & inrange(age, 21, 54))
    by house_id: egen byte has_daughter_20_55 = max(relation == 3 & sex == 2 & inrange(age, 21, 54))
    keep if has_mother_20_55 & has_daughter_20_55
    The only real obstacle here is that age is, for some odd reason, a string variable. Converting it to numeric is a prerequisite to solving the problem.

    Note: In your example data, there are no households with a mother in the specified age range, and only a few with a daughter either. There are no households with both.

    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
    Last edited by Clyde Schechter; 11 Apr 2022, 19:03.

    Comment


    • #3
      Thank you so much.
      It's just an issue. That I should also support households who do not have daughters. For example, a households may include a father, mother and son with the desired condition but no daughter. This household should also be considered.


      Originally posted by Clyde Schechter View Post
      [code]

      Note: In your example data, there are no households with a mother in the specified age range, and only a few with a daughter either. There are no households with both.

      .
      When the head of the household is a man and his wife is a woman, there is a mother. It is possible that the head of the household is a woman who is a mother. The number 4 is for the case where it is possible for the presence of a grandparent in the family.

      Comment


      • #4
        When the head of the household is a man and his wife is a woman, there is a mother. It is possible that the head of the household is a woman who is a mother. The number 4 is for the case where it is possible for the presence of a grandparent in the family.
        Oh, I see! Well that changes it a little bit:

        Code:
        destring age, replace
        by house_id, sort: egen byte has_mother_20_55 = max(inlist(relation, 1, 2) & sex == 2 & inrange(age, 21, 54))
        by house_id: egen byte has_daughter_20_55 = max(relation == 3 & sex == 2 & inrange(age, 21, 54))
        keep if has_mother_20_55 & has_daughter_20_55

        Comment

        Working...
        X