My code for Guillermo Cruces' problem can be simplified
or improved further from this
> Here is the code in one chunk
>
> bysort hhid fatherm : gen fkids = _N if fatherm < .
> bysort hhid motherm : gen mkids = _N if motherm < .
> gen ownkids = 0
> su member, meanonly
>
> forval i = 1 / `r(max)' { /* mailer bug protection */
> gen ischild = (fatherm == `i') | (motherm == `i')
> gen isnotmember = member != `i'
> #delimit ;
> bysort hhid (ischild isnotmember) :
> replace ownkids =
> cond(motherm[_N] == `i', mkids[_N], fkids[_N])
> if _n == 1 & ischild[_N] ;
> #delimit cr
> drop ischild isnotmember
> }
to this:
bysort hhid fatherm : gen fkids = _N
bysort hhid motherm : gen mkids = _N
gen byte ownkids = 0
gen byte ischild = .
su member, meanonly
forval i = 1 / `r(max)' { /* mailer bug protection */
replace ischild = (fatherm == `i') | (motherm == `i')
#delimit ;
bysort hhid (ischild) :
replace ownkids =
cond(motherm[_N] == `i', mkids[_N], fkids[_N])
if member == `i' & ischild[_N] ;
#delimit cr
}
Nick
[email protected]
*
* 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/