Announcement

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

  • Calculate shares by variable and a condition

    Hi Stata Users,

    I have household data and would like to calculate age specific enrollment rates i.e. share of children in at a specific age in a household that are attending school. Below is an example of data with the original (hhid -attend) and desired (age10 - age15) variables

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(hhid pid age attend) float age10 byte age11 float(age12 age13) byte age14 float age15
    1 1 10 1    1 0  1  0 0  0
    1 2 11 0    1 0  1  0 0  0
    1 3 12 1    1 0  1  0 0  0
    2 1 10 1   .5 0 .5  0 0  0
    2 2 10 0   .5 0 .5  0 0  0
    2 3 12 0   .5 0 .5  0 0  0
    2 4 12 1   .5 0 .5  0 0  0
    3 1 10 0    0 1  0  0 0  0
    3 2 11 1    0 1  0  0 0  0
    3 3 12 0    0 1  0  0 0  0
    3 4 14 0    0 1  0  0 0  0
    4 1 15 1    0 0  0  0 0 .5
    4 2 15 0    0 0  0  0 0 .5
    5 1 10 1 .333 0  0 .5 0  0
    5 2 10 1 .333 0  0 .5 0  0
    5 3 10 0 .333 0  0 .5 0  0
    5 4 13 1 .333 0  0 .5 0  0
    5 5 13 0 .333 0  0 .5 0  0
    5 6 15 0 .333 0  0 .5 0  0
    end
    Thanks in advance!

  • #2
    I don't follow what you seek, but this may help.


    Code:
    egen pr_attend = mean(attend), by(age)
    
    .
    . tabdisp age, c(pr_attend)
    
    ----------------------
    age | pr_attend
    ----------+-----------
    10 | .5714286
    11 | .5
    12 | .5
    13 | .5
    14 | 0
    15 | .3333333
    ----------------------

    Comment


    • #3
      Thanks Nick Cox for your response.

      Let me try to be clearer. For household 2, there are 2 children aged 10 (1 attending school and the other not) hence the value 0.5 in column age10. This applies to those aged 12 in the same household.
      For household 5, there are 3 children aged 10, 2 of which are attending school hence the value 0.667 (This was wrongly presented in my original post. Sincere apologies for this!!) 1 of the 2 children aged 13 are attending school hence the value 0.5 in the column age13 and finally none of those aged 15 in the house is attending hence 0 in age15.

      Comment


      • #4
        So it seems to me that you just want to add hhid to the by() option in #2.

        Comment


        • #5
          Thanks Nick!!

          Comment

          Working...
          X