I have Household data as follows. I want to create two variables- the proportion of girls below 12 and boys below 12 in the Household. When I use bysort and egen, I get the proportion as per household only for the selected female/male below 12; the rest is missing. But I want the proportions to be repeated for all the members of the same household and the households that don't have to be zero.
DATA:
My code and output I get:
What I want as an output:
Can you please suggest the correct code?
DATA:
HHID | memsex | age |
1 | F | 11 |
1 | M | 8 |
1 | M | 37 |
1 | F | 34 |
2 | F | 5 |
2 | F | 6 |
2 | M | 11 |
2 | M | 40 |
2 | F | 38 |
3 | M | 13 |
3 | F | 40 |
Code:
bysort HHID memsex: egen girlsbelow12 = count(memsex) if age<12 & memsex==2 bysort HHID memsex: egen boysbelow12 = count(memsex) if age<12 & memsex==1
HHID | memsex | age | girlsbelow12 | boysbelow12 |
1 | F | 11 | 1 | . |
1 | M | 8 | . | 1 |
1 | M | 37 | . | . |
1 | F | 34 | . | . |
2 | F | 5 | 2 | . |
2 | F | 6 | 2 | . |
2 | M | 11 | . | 1 |
2 | M | 40 | . | . |
2 | F | 38 | . | . |
3 | M | 13 | . | . |
3 | F | 40 | . | . |
HHID | memsex | age | girlsbelow12 | boysbelow12 |
1 | F | 11 | 1 | 1 |
1 | M | 8 | 1 | 1 |
1 | M | 37 | 1 | 1 |
1 | F | 34 | 1 | 1 |
2 | F | 5 | 2 | 1 |
2 | F | 6 | 2 | 1 |
2 | M | 11 | 2 | 1 |
2 | M | 40 | 2 | 1 |
2 | F | 38 | 2 | 1 |
3 | M | 13 | 0 | 0 |
3 | F | 40 | 0 | 0 |
Comment