Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Survey data: how to assign mother's age to child?


From   [email protected]
To   [email protected]
Subject   Re: st: Survey data: how to assign mother's age to child?
Date   Thu, 08 Jun 2006 13:32:59 -0500

Would the following process work:  Create a data set of mothers and 
their ages, merge it with the original data set by household, and then 
assign mage to the child's age if there is no mother present.


use hh,clear
l, noobs
tempfile tmp1
sort hh id
save `tmp1', replace
keep if id == 2 & sex == 2
keep hh age
sort hh 
rename age mage
merge hh using `tmp1'
drop _me
sort hh id
by hh: gen byte mom= 1 if id[2] == 2
replace mage = . if id <= 2
replace mage = age if mom == . & id >2
l, noobs



For example:


. use hh,clear

. l, noobs

  +---------------------------------+
  | 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 |
  +---------------------------------+

. tempfile tmp1

. sort hh id

. save `tmp1', replace
(note: file C:\DOCUME~1\SCOTT~1.MER\LOCALS~1\Temp\ST_0f00000o.tmp not 
found)
file C:\DOCUME~1\SCOTT~1.MER\LOCALS~1\Temp\ST_0f00000o.tmp saved

. keep if id == 2 & sex == 2
(4 observations deleted)

. keep hh age

. sort hh 

. rename age mage

. merge hh using `tmp1'
variable hh does not uniquely identify observations in C:\DOCUME~1
\SCOTT~1.MER\LOCALS~1\Temp\ST_0f00000o.tmp

. drop _me

. sort hh id

. by hh: gen byte mom= 1 if id[2] == 2
(2 missing values generated)

. replace mage = . if id <= 2
(2 real changes made, 2 to missing)

. replace mage = age if mom == . & id >2
(1 real change made)

. l, noobs

  +----------------------------------------------+
  | hh   mage   id   sex   age   mid   fid   mom |
  |----------------------------------------------|
  |  1      .    1     1    30     .     .     1 |
  |  1      .    2     2    30     .     .     1 |
  |  1     30    3     1     5     2     1     1 |
  |  2      .    1     1    30     .     .     . |
  |  2      5    3     2     5     2     1     . |
  +----------------------------------------------+

. 


Scott


----- Original Message -----
From: Friedrich Huebler <[email protected]>
Date: Thursday, June 8, 2006 12:48 pm
Subject: st: Survey data: how to assign mother's age to child?
To: [email protected]

> One 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/
> 
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index