I didn't really look at your solution (sorry) but try:
clear
input 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
end
*would like to get [both] parents' years of schooling
*for a member in a group (for example children or
*grandchildren, etc..).
*[apparently grandchildren follow their parents in
* order of matv within ID]
la def rel 1 "household head", modify
la def rel 2 "spouse of HH head", modify
la def rel 3 "child of HH head", modify
la def rel 4 "parent of HH head", modify
la def rel 5 "grandparent of HH head", modify
la def rel 6 "grandchild of HH head", modify
la def rel 7 "other relation", modify
la val rel rel
tempfile tmp
save `tmp'
*can't do anything with rel==7
drop if rel==7
gen byte gnr=5 if rel==6
replace gnr=4 if rel==3
replace gnr=3 if inlist(rel,1,2)
replace gnr=2 if rel==4
replace gnr=1 if rel==5
bys ID (matv): g subf=rel==3 & rel[_n-1]!=3
by ID: g sf=sum(subf)
replace sf=0 if gnr<4
* apparently grandpars not assoc w/parents
drop if rel==5
qui tab matv
loc maxi=r(r)
g fe=.
g me=.
g obs=_n
forv i=1/`maxi' {
loc c "gnr[_n-`i']==gnr-1 & (sf==sf[_n-`i'] | rel!=6)"
bys ID (sf gnr): replace fe=y[_n-`i'] if sex[_n-`i']==1 & `c'
bys ID (sf gnr): replace me=y[_n-`i'] if sex[_n-`i']==0 & `c'
}
la var fe "Father's educ"
la var me "Mother's educ"
joinby ID matv using `tmp', unm(both) update replace
sort ID matv
li ID rel06 yrs sex sf fe me, noo sepby(ID)
On 10/25/07, Nguyen Cong Minh <[email protected]> wrote:
> 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 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/