Announcement

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

  • creating a variable with repeated value within a group


    Dear Statalist
    I am working with a household data (contains both the household information and members of the household). i defined a variable member10_17 that stores the number of people in particular household for example household 108 within the age bracket(10 - 17), i also want the values returned to repeat across the household. for example, household no 93 will return 0 because there is no member within the age limit so 0 should be repeated four times because there are four members in the household. in my do file i wrote this code:
    Code:
    by province district community hhno: egen member10_17 = count(a4age) if (a4age>=10) & (a4age<=17)
    but unsurprisingly only record that satisfies
    Code:
     (a4age>=10) & (a4age<=17)
    contains the value. Now how can i get the value of member10_17 repeated in all the records of the household.
    please i will appreciate any attempt to help me

    Below is the snapshot of the dataset

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(hhno a4age) byte(b5labour c4grade)
     93  1  .  .
     93  4  .  1
     93 20  9  .
     93 40 14  .
     96 45  9  .
     96 27  3  .
     96 67 14  .
     96 20  8 12
     96 55  9  .
    108 10  8  5
    108  6  8  .
    108 20  8  .
    108 60 13  .
    120  4  .  1
    120 40 13  .
    120  1  .  .
    120 27  9  .
    122 25  9  .
    131  5  8  1
    131 30  9  .
    131  7  8  3
    end
    Regard

  • #2

    Code:
     
     by province district community hhno: egen member10_17 = total(inrange(a4age, 10, 17))

    Comment


    • #3
      Dear Nick
      The code did the magic, it has been a life saver. Thank you
      Regard

      Comment


      • #4
        I am face with another challenge. below is the snap shot of my dataset:
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(a2sex a3relationship) int a4age float(hhage hhsex)
        2 3 2 0 0
        2 3 4 0 0
        1 1 42 42 1
        2 2 27 0 0
        1 3 1 0 0
        2 1 76 76 2
        2 3 32 0 0
        1 4 6 0 0
        2 3 10 0 0
        2 2 49 0 0
        1 3 17 0 0
        1 1 67 67 1
        2 3 14 0 0
        2 3 12 0 0
        2 3 18 0 0
        2 3 8 0 0
        2 3 10 0 0
        2 2 35 0 0
        1 3 4 0 0
        2 3 1 0 0
        1 6 12 0 0
        end
        label values a2sex sex
        label def sex 1 "Male", modify
        label def sex 2 "Female", modify
        label values a3relationship relation
        label def relation 1 "Head", modify
        label def relation 2 "Spouse (Wife/Husband)", modify
        label def relation 3 "Child(Son/Daughter)", modify
        label def relation 4 "Grandchild", modify
        label def relation 6 "Son/Daughter in-law)", modify

        the variables hhage and hhsex store the age and sex of the head of the household respectively. it shows zero if the person is not the head of the household but I want the value stored for the head of the household to repeat across all the members in the household. for example if the head of the household is 60 years old and the sex is 1 then all the members of the household will have that same value under the variables hhage and hhsex.
        as always anticipating kind respond.
        Cheers
        Lau

        Comment


        • #5
          I don't see that you can do this without a household identifier. With one, it's easy. See also Section 7 in https://www.stata-journal.com/sjpdf....iclenum=dm0055

          Comment


          • #6
            Dear Nick
            Forgive me it was a mistake. in trying to generate the dataset I omitted the household ID below is the data with id included

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input byte(id a2sex a3relationship a4age hhage hhsex)
            1 2 3  2  0 0
            1 2 3  4  0 0
            1 1 1 42 42 1
            1 2 2 27  0 0
            1 1 3  1  0 0
            2 2 1 76 76 2
            2 2 3 32  0 0
            2 1 4  6  0 0
            2 2 3 10  0 0
            2 2 2 49  0 0
            2 1 3 17  0 0
            3 1 1 67 67 1
            3 2 3 14  0 0
            3 2 3 12  0 0
            3 2 3 18  0 0
            3 2 3  8  0 0
            3 2 3 10  0 0
            4 2 1 45 35 1
            4 1 3  4  0 0
            4 2 3  1  0 0
            4 2 6 15  0 0
            end
            label values a2sex sex
            label def sex 1 "Male", modify
            label def sex 2 "Female", modify
            label values a3relationship relation
            label def relation 1 "Head", modify
            label def relation 2 "Spouse (Wife/Husband)", modify
            label def relation 3 "Child (Son/Daughter)", modify
            label def relation 4 "Grandchild", modify
            label def relation 6 "Son/Daughter in-Law", modify
            thanks for the link as I await your respond on this I will study the Journal shared in the link

            Regards

            Comment


            • #7
              Mutanen:
              try:
              Code:
              . bysort id (a3relationship): replace hhsex=hhsex[1]
              
              . bysort id (a3relationship): replace hhage=hhage[1]
              Kind regards,
              Carlo
              (StataNow 18.5)

              Comment


              • #8
                Carlo's solution is good if there is always in each household just one person who is head of household. What if there were none or more than one?

                Code:
                bysort id: egen head_check = total(a3relationship == 1)
                by id: egen head_age = total((a3relationship == 1) * a4age) if head_check == 1
                Last edited by Nick Cox; 11 Jun 2018, 01:24.

                Comment


                • #9
                  Nick:
                  admittedly, I did not consider those scenarios.
                  Kind regards,
                  Carlo
                  (StataNow 18.5)

                  Comment


                  • #10
                    Thank you for the responses. it great to have the two solutions because as Carlo pointed out I did not consider those scenarios I am using CAPI that does not allowed more that one head for the household as well as no head. both solutions worked.

                    Warm Regards

                    Comment

                    Working...
                    X