Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Extensions to: Creating variables recording properties of the other members of a group


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Extensions to: Creating variables recording properties of the other members of a group
Date   Thu, 29 Aug 2002 15:39:37 +0100

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 
n.j.cox@durham.ac.uk 
*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index