Hi,
I tried to do one problem (Create variables summarizing for each individual
properties of the other members of a group).
I followed some guide in the Stata list, but I still get some wrong results.
I found some hints on the Stata' website
(http://www.stata.com/support/faqs/data/members.html) and a thread (looping
over observations -mata-? in 2005).
For a simple family (parents, children), my solution is fine. However for a
extended family (with several nuclear family inside), my solution doesn't
work well. (please see the results).
Suppose I have the following data, where rel variable is the relationship
within the family, male if sex=1, and yrschl06 is years of schooling, matv
is a count variable within a family, married06=1 if married.
The rel06 variable is defined as follows:
rel =1, then that person is household head
rel =2, then --> spouse of HH head
rel =3, then --> children of HH head
rel =4, then --> parents of HH head
rel =5, then --> grandparents of HH head
rel =6, then --> grand children of HH head
rel =7, then --> other relations
Data:
ID matv rel06 yrschl06 sex06 married06
1 1 1 11 1 1
1 2 2 12 0 1
1 3 3 21 1 0
1 4 3 23 1 1
1 5 3 7 0 1
1 6 6 5 0 0
1 7 3 12 1 1
1 8 6 3 1 0
2 1 1 32 0 1
2 2 2 21 1 1
2 3 3 13 1 1
2 4 3 16 0 1
2 5 6 6 0 0
2 6 6 6 0 0
2 7 7 7 0 1
3 1 1 21 1 1
3 2 3 13 0 0
3 3 3 6 1 0
3 4 4 6 0 1
3 5 5 6 1 1
4 1 1 23 1 1
4 2 3 21 0 1
4 3 3 15 1 1
4 4 6 16 1 0
4 5 6 6 1 0
4 6 3 17 1 1
4 7 6 6 0 0
I would like to get parents' years of schooling for a member in a group (for
example children or grand children, etc..). My solution is:
------
gen fa = (rel06==1 & sex06==1)
gen ma = (rel06==1 & sex06==0)
replace ma=1 if rel06==2 & sex06==0
replace fa=1 if rel06==2 & sex06==1
gen notfa = 1-fa
gen notmo = 1-ma
bysort ID (notfa): gen fatherid = yrschl06[1] if notfa[1]==0
replace fatherid = . if rel06!=3
bysort ID (notmo): gen motherid = yrschl06[1] if notmo[1]==0
replace motherid = . if rel06!=3
gen bo = (rel06==3 & sex06==1 & married06==1)
gen me = (rel06==3 & sex06==0 & married06==1)
gen notbo = 1- bo
gen notme = 1- me
bysort ID (notbo): gen boid = yrschl06[1] if notbo[1]==0
replace boid = . if rel06!=6
bysort ID (notme): gen meid = yrschl06[1] if notme[1]==0
replace meid = . if rel06!=6
gen bo2 = (rel06==4 & sex06==1)
gen me2 = (rel06==4 & sex06==0)
gen notbo2 = 1- bo2
gen notme2 = 1- me2
bysort ID (notbo2): gen boid2 = yrschl06[1] if notbo2[1]==0
replace boid2 = . if rel06!=1
bysort ID (notme2): gen meid2 = yrschl06[1] if notme2[1]==0
replace meid2 = . if rel06!=1
egen Fatheryr = rowmax( fatherid boid boid2)
egen Motheryr = rowmax( motherid meid meid2)
drop fa ma notfa notmo fatherid motherid bo me notbo notme boid meid bo2 me2
notbo2 notme2 boid2 meid2
sort ID matv
list ID matv rel06 sex06 yrschl06 Fatheryr Motheryr
------
The results I got is:
ID matv rel06 sex06 marri~06 yrschl06
Fatheryr Motheryr
1. 1 1 1 1 1 11
. .
2. 1 2 2 0 1 12
. .
3. 1 3 3 1 0 21
11 12
4. 1 4 3 1 1 23
11 12
5. 1 5 3 0 1 7
11 12
6. 1 6 6 0 0 5
23 7 <-- for id==1, matv==6, luckly Fatheryr=23
7. 1 7 3 1 1 12
11 12
8. 1 8 6 1 0 3
23 7 <-- wrong here, it should be Fatheryr=12, Motheryr=. for
id==1, matv==8
9. 2 1 1 0 1 32
. .
10. 2 2 2 1 1 21
. .
11. 2 3 3 1 1 13
21 32
12. 2 4 3 0 1 16
21 32
13. 2 5 6 0 0 6
13 16
14. 2 6 6 0 0 6
13 16
15. 2 7 7 0 1 7
. .
16. 3 1 1 1 1 21
. 6
17. 3 2 3 0 0 13
21 .
18. 3 3 3 1 0 6
21 .
19. 3 4 4 0 1 6
. .
20. 3 5 5 1 1 6
. .
21. 4 1 1 1 1 23
. .
22. 4 2 3 0 1 21
23 .
23. 4 3 3 1 1 15
23 .
24. 4 4 6 1 0 16
15 21
25. 4 5 6 1 0 6
15 21
26. 4 6 3 1 1 17
23 .
27. 4 7 6 0 0 6
15 21 <-- wrong here, it should be Fatheryr=17, Motheryr=. for
id==1, matv==8
Anyone can help me fix this problem or have a better solution? I appreciate
your help very much.
Thank you and best,
Minh Nguyen
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/