I forgot to mention that I use Stata 8.2.
--- Friedrich Huebler <[email protected]> wrote:
> Once again, apologies for the incomplete messages. It seems that
> I inadvertently discovered a new keyboard shortcut for sending
> messages in Yahoo Mail.
>
> I work with household survey data and am looking for a way to
> assign the mother's age to her children. The problem is that some
> observations are missing. Take the data below:
>
> hh id sex age mid fid
> 1 1 1 30 . .
> 1 2 2 30 . .
> 1 3 1 5 2 1
> 2 1 1 30 . .
> 2 3 2 5 2 1
>
> There are two households (hh 1 and 2) with three household members
> each (id 1 to 3). mid identifies a child's mother, fid the father.
> In household 2, the mother (id 2) is missing. The usual code to
> assign the mother's age therefore fails:
>
> . bysort hh (id): gen mage=age[mid]
>
> hh id sex age mid fid mage
> 1 1 1 30 . . .
> 1 2 2 30 . . .
> 1 3 1 5 2 1 30
> 2 1 1 30 . . .
> 2 3 2 5 2 1 5
>
> The child in the second household is assigned her own age as the
> mother's age because age[2] points to the wrong observation. My
> solution is a loop over households and household members.
>
> . gen mage2 = .
> . levels hh, local(households)
> . foreach hh of local households {
> levels mid if hh==`hh', local(mothers)
> foreach m of local mothers {
> sum age if hh==`hh' & id==`m', meanonly
> replace mage2 = r(mean) if hh==`hh' & mid==`m'
> }
> }
>
> This yields the correct result but the loop is very time-consuming
> with thousands of observations.
>
> hh id sex age mid fid mage mage2
> 1 1 1 30 . . . .
> 1 2 2 30 . . . .
> 1 3 1 5 2 1 30 30
> 2 1 1 30 . . . .
> 2 3 2 5 2 1 5 .
>
> I would appreciate suggestions for making the code more efficient.
>
> Friedrich Huebler
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
*
* 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/