Hi Shiyu,
You're right, since you need that for each wave using xhhrel will not work. You can use egoalt file and do something like this (using wave 1 as an example):
global in "D:/stata/stata13_se"
cd "D:/number of children"
use "$in/ukhls/a_egoalt", clear
fre a_relationship_dv
//keep only biological, step, adopt, foster parents
keep if inrange(a_relationship_dv,9,12)
fre a_relationship_dv
//count the number of times each pidp appears, which gives you the number of children of all ages (each row is one parent - child relationship)
bysort pidp: gen numchild = _N
fre numchild
//keep only one observation per person
bysort pidp: keep if _n==1
fre numchild
//link to indall
merge 1:1 pidp using "$in/ukhls/a_indall"
//generate your final variable
gen a_numchild=0
replace a_numchild=numchild if numchild!=.
//there are no instances where a_nchild_dv>a_numchild, this is correct since a_nchild_dv includes only children<16 y.o. and excludes foster children
count if a_nchild_dv>a_numchild
//your new variable captures resident children aged >15 y.o.
count if a_nchild_dv<a_numchild
*To run this on all waves, put the code above in a loop, e.g.:
*
foreach w in a b c d e f g h i j k l m {
use "$in/ukhls/`w'_egoalt", clear
fre `w'_relationship_dv
//keep only biological, step, adopt, foster parents
keep if inrange(`w'_relationship_dv,9,12)
fre `w'_relationship_dv
//count the number of times each pidp appears, which gives you the number of children of all ages (each row is one parent - child relationship)
bysort pidp: gen numchild = _N
fre numchild
//keep only one observation per person
bysort pidp: keep if _n==1
fre numchild
//link to indall
merge 1:1 pidp using "$in/ukhls/`w'_indall"
//generate your final variable
gen `w'_numchild=0
replace `w'_numchild=numchild if numchild!=.
//there are no instances where `w'_nchild_dv>`w'_numchild, this is correct since `w'_nchild_dv includes only children<16 y.o.
count if `w'_nchild_dv>`w'_numchild
//your new variable captures resident children aged >15 y.o.
count if `w'_nchild_dv<`w'_numchild
}
What is left are non-resident children and since you need the number of such children it is not possible to get this information for everyone in the sample and for every wave because the relevant question was asked only in wave 1: a_nrelsw11. The other thing you need to consider is how respondents understood the wording: "How many son(s) or daughter(s) do you have, excluding those living here at the moment?", it doesn't specify if this was about adopted, foster, and step children as well. This will be easier if you need a binary has children vs. no children, as it was asked in waves 1, 3, 5, 7, 9, 11, 13.
I hope this helps.
Best wishes,
Piotr Marzec
UKHLS User Support