Announcement

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

  • Households- Generate variables within groups if condition is met

    Hi Everyone,

    I hope these lines find you well.

    I am trying to create a variable called Household structure.

    I have the following variables
    1. Name, Familyname1, Familyname2 (it's a Hispanic household where kids' Familyname1 and Familyname2 are from Familyname 1 of the father and familyname1 of the mother).
    2. I have status in the household (this shows the household head (most often the husband or wife), wife of the household head, Son, Daughter, Aunt etc).
    3. There is also another variable for Indiv (this appoints 1 for the household head and consequent numbers for other members of the family; so you can imagine 1 for the household head (often the husband), 2 for the wife, 3 for the first child etc)
    4. Household ID (In this, each household have a unified identity)
    To create the Household structure
    I want to assign the following;

    1 for a household with a single individual

    2 for a household with no father or mother. Just siblings

    3 for a household with Father, Mother and child(ren)

    3 for a household with just couples (no child yet)

    4 for a household with a father, mother, child(ren) and any other relative (like uncle, grandfather etc).

    Can you please guide me on how to command this?

    Thanks so much in advance



  • #2
    Femi:
    as usual (and recommended by the FAQ), an excerpt/example of your dataset shared via -dataex- would help enormously. Thanks.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlo,

      Thanks for your response.

      Below I have the dataset example, I have made discrete the real name for data privacy
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int HHID byte Indiv str21 Name str24 familyname1 str21    familyname2    str20    Relationship    float    gender
      1 1 "Name"        "Surname1"     "Surname2"      "Head" 1
      2 4 "Name"        "Surname1"     "Surname2" "Daughter"        0
      2 2 "Name"        "Surname1"     "Surname2"     "Wife"      0
      2 1 "Name"        "Surname1"     "Surname2"        "Head"      1
      2 3 "Name"        "Surname1"     "Surname2"        "Daughter"        0
      3 1 "Name"        "Surname1"     "Surname2"      "Head"      1
      3 2 "Name"        "Surname1"     "Surname2"      "Son"        1
      4 5 "Name"        "Surname1"     "Surname2"        "Daughter"        0
      4 2 "Name"        "Surname1"     "Surname2"     "Wife"      0
      4 3 "Name"        "Surname1"     "Surname2"      "Son"        1
      4 1 "Name"        "Surname1"     "Surname2"     "Head"      1
      4 4 "Name"        "Surname1"     "Surname2"       "Daughter"        0
      5 5 "Name"        "Surname1"     "Surname2"    "Grandmother"        0
      5 3 "Name"        "Surname1"     "Surname2"    "Son"        1
      5 1 "Name"        "Surname1"     "Surname2"       "Head"      1
      5 2 "Name"        "Surname1"     "Surname2"       "Wife"      0
      5 4 "Name"        "Surname1"     "Surname2"       "Daughter"      0
      
      end
      I am glad to provide additional information if need be.

      Thanks

      Comment


      • #4
        See https://www.stata.com/support/faqs/d...ble-recording/. It would help if you created indicators at the household level for the family members, e.g.,

        Code:
        rename gender male
        bys HHID: egen father= max((Relationship =="Head" & male) | (Relationship =="Husband"))
        bys HHID: egen mother=  max((Relationship =="Head" & !male) | (Relationship =="Wife"))
        bys HHID: egen child= max((Relationship =="Daughter") | (Relationship =="Son"))
        1 for a household with a single individual
        Code:
        bys HHID: g wanted1=_N==1
        2 for a household with no father or mother. Just siblings
        Code:
        bys HHID: egen wanted2=max(!father & !mother & child)
        3 for a household with just couples (no child yet)
        Code:
        bys HHID: egen wanted3=max(!child & father & mother)
        and so on.
        Last edited by Andrew Musau; 01 Aug 2022, 08:49.

        Comment


        • #5
          Hi Andrew,

          Thanks for your response.

          After generating the father, mother, and child variables, I went further to generate for grandparents and uncles.

          However, generating the variable for the household structure seems unclear;
          The code below works pretty well for the single individual household

          bys HHID: g wanted1=_N==1

          The consequent code to create; "2 for a household with no father or mother. Just siblings", "3 for a household with just couples (no child yet)" 3 for a household with father, mother, and children, etc. did not work well.

          Do you think it would be doable to generate just one column for the interested variable "Household_structure"?


          Regards
          Last edited by Femi Oladunni; 02 Aug 2022, 08:12.

          Comment


          • #6
            You need to show examples of

            did not work well
            using a data example. Some of the conditions may be ambiguous, e.g., below

            3 for a household with just couples (no child yet)
            a couple is a pair, so single-person households will be excluded. If you mean


            a household with no children
            then you just take the negation of the indicator child. Same with

            2 for a household with no father or mother. Just siblings
            could mean

            i. no father, no mother, children
            ii. no father, mother and children
            iii. no mother, father and children
            iv. just no father and mother
            v. either no father or no mother

            So you know what your conditions mean and should be able to adjust the code creating the indicators. Finally, for the final line of the code, you can use gen instead of egen, but the latter will still yield the correct results.


            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input int HHID byte Indiv str21 Name str24 familyname1 str21 familyname2 str20 Relationship float gender
            1 1 "Name" "Surname1" "Surname2" "Head"        1
            2 4 "Name" "Surname1" "Surname2" "Daughter"    0
            2 2 "Name" "Surname1" "Surname2" "Wife"        0
            2 1 "Name" "Surname1" "Surname2" "Head"        1
            2 3 "Name" "Surname1" "Surname2" "Daughter"    0
            3 1 "Name" "Surname1" "Surname2" "Head"        1
            3 2 "Name" "Surname1" "Surname2" "Son"         1
            4 5 "Name" "Surname1" "Surname2" "Daughter"    0
            4 2 "Name" "Surname1" "Surname2" "Wife"        0
            4 3 "Name" "Surname1" "Surname2" "Son"         1
            4 1 "Name" "Surname1" "Surname2" "Head"        1
            4 4 "Name" "Surname1" "Surname2" "Daughter"    0
            5 5 "Name" "Surname1" "Surname2" "Grandmother" 0
            5 3 "Name" "Surname1" "Surname2" "Son"         1
            5 1 "Name" "Surname1" "Surname2" "Head"        1
            5 2 "Name" "Surname1" "Surname2" "Wife"        0
            5 4 "Name" "Surname1" "Surname2" "Daughter"    0
            end
            
            rename gender male
            bys HHID: egen father= max((Relationship =="Head" & male) | (Relationship =="Husband"))
            bys HHID: egen mother=  max((Relationship =="Head" & !male) | (Relationship =="Wife"))
            bys HHID: egen child= max((Relationship =="Daughter") | (Relationship =="Son"))
            
            *2 for a household with no father or mother. Just siblings
            *NO PARENTS
            g wanted1= !father & !mother
            
            *NO PARENTS, CHILDREN PRESENT
            g wanted2= !father & !mother & child
            
            *ONE PARENT, CHILDREN PRESENT
            g wanted3= (father & child & !mother) | (mother& child& !father)
            Res.:

            Code:
            . l, sepby(HHID)
            
                 +------------------------------------------------------------------------------------------------------------------------+
                 | HHID   Indiv   Name   family~1   family~2   Relations~p   male   father   mother   child   wanted1   wanted2   wanted3 |
                 |------------------------------------------------------------------------------------------------------------------------|
              1. |    1       1   Name   Surname1   Surname2          Head      1        1        0       0         0         0         0 |
                 |------------------------------------------------------------------------------------------------------------------------|
              2. |    2       4   Name   Surname1   Surname2      Daughter      0        1        1       1         0         0         0 |
              3. |    2       2   Name   Surname1   Surname2          Wife      0        1        1       1         0         0         0 |
              4. |    2       1   Name   Surname1   Surname2          Head      1        1        1       1         0         0         0 |
              5. |    2       3   Name   Surname1   Surname2      Daughter      0        1        1       1         0         0         0 |
                 |------------------------------------------------------------------------------------------------------------------------|
              6. |    3       1   Name   Surname1   Surname2          Head      1        1        0       1         0         0         1 |
              7. |    3       2   Name   Surname1   Surname2           Son      1        1        0       1         0         0         1 |
                 |------------------------------------------------------------------------------------------------------------------------|
              8. |    4       5   Name   Surname1   Surname2      Daughter      0        1        1       1         0         0         0 |
              9. |    4       2   Name   Surname1   Surname2          Wife      0        1        1       1         0         0         0 |
             10. |    4       3   Name   Surname1   Surname2           Son      1        1        1       1         0         0         0 |
             11. |    4       1   Name   Surname1   Surname2          Head      1        1        1       1         0         0         0 |
             12. |    4       4   Name   Surname1   Surname2      Daughter      0        1        1       1         0         0         0 |
                 |------------------------------------------------------------------------------------------------------------------------|
             13. |    5       5   Name   Surname1   Surname2   Grandmother      0        1        1       1         0         0         0 |
             14. |    5       3   Name   Surname1   Surname2           Son      1        1        1       1         0         0         0 |
             15. |    5       1   Name   Surname1   Surname2          Head      1        1        1       1         0         0         0 |
             16. |    5       2   Name   Surname1   Surname2          Wife      0        1        1       1         0         0         0 |
             17. |    5       4   Name   Surname1   Surname2      Daughter      0        1        1       1         0         0         0 |
                 +------------------------------------------------------------------------------------------------------------------------+
            
            .
            Last edited by Andrew Musau; 02 Aug 2022, 08:24.

            Comment


            • #7
              Thanks a bunch, Andrew!

              Comment

              Working...
              X