I would like to sum the values of one variable, where values of another variable satisfy some condition. Here is some example data (based on this Stata help page)
Within each family ID, for each person I would like to see the sum of income of other family members, but only of those family members that are up to 4 years older than each person.
So for example, family 1 has a 14 yr old, and there is 1 other family member within the age range of being 4 years older, and the sum of that 1 person's income is 300, so I'd like the value 300 placed with the 14 yr old. The other two members of family 1 do not have anyone within a +4 year age range, so no value should be generated for the two other members.
Family 2 has a 10 yr old, and there are 2 family members who are within +4 years age range, and the sum of income for those two members is 325, so the value 325 should be put with the 10 yr old record.
Family 2 has a 12 yr old, with 1 other family member who is within +4 years age range, and the sum of income for that one person is 175, so the value 175 should be put with the 12 yr old record.
So on and so forth.
The attempts I've made so far are
and
but these do not work.
Thanks for reading.
Code:
clear input famid person female age income 1 3 1 14 250 1 2 1 16 300 1 1 1 36 42325 2 5 0 10 100 2 4 1 12 150 2 3 0 14 175 2 2 1 42 48942 2 1 0 45 47621 3 6 1 3 25 3 5 1 7 50 3 4 1 9 75 3 3 0 11 110 3 2 1 36 41984 3 1 0 39 42329 end
Within each family ID, for each person I would like to see the sum of income of other family members, but only of those family members that are up to 4 years older than each person.
So for example, family 1 has a 14 yr old, and there is 1 other family member within the age range of being 4 years older, and the sum of that 1 person's income is 300, so I'd like the value 300 placed with the 14 yr old. The other two members of family 1 do not have anyone within a +4 year age range, so no value should be generated for the two other members.
Family 2 has a 10 yr old, and there are 2 family members who are within +4 years age range, and the sum of income for those two members is 325, so the value 325 should be put with the 10 yr old record.
Family 2 has a 12 yr old, with 1 other family member who is within +4 years age range, and the sum of income for that one person is 175, so the value 175 should be put with the 12 yr old record.
So on and so forth.
The attempts I've made so far are
Code:
bysort famid: egen newvar = total(income) if inrange(age, age, age+4)
Code:
bysort famid: egen newvar = total(income) if inrange(age[_n], age[_n], age[_n]+4)
Thanks for reading.
Comment