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   "Marcela Perticara" <mperticara@uahurtado.cl>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Extensions to: Creating variables recording properties of the other members of a group
Date   Wed, 28 Aug 2002 19:08:56 -0500

Hello Guillermo,

This code might help. I only tested with the two cases you sent, it might
need some adjutment.
Here I am creating only one vble for the number of kids, but you can easily
rename
the variable to be father-mother specific and you will get two vbles after
you merge
everything.
If your dataset is too big you might want to read the data twice instead of
preserving/restoring.

/*Make datasets for fathers and mothers with their number
of kids*/

use hm.dta, clear /*your dataset*/
foreach var of varlist fatherm motherm {
 preserve
 qui keep if `var'!=.
 sort hhid `var'
 by hhid `var': gen sibl=_N /*here you can make the name father-mother
specific*/
 by hhid `var': keep if _n==1
 keep hhid `var' sibl
 ren `var' member
 sort hhid member
 save `var'.dta, replace
 restore
}

use hm.dta
sort hhid member
merge hhid member using fatherm
drop _merge
sort hhid member
merge hhid member using motherm, update
drop _merge


I didnīt put too much effort in writting it short, you might be able to
shorten it.

Hope this helps!

Marcela


--------------------------------------------
Universidad Alberto Hurtado
Erasmo Escala 1835
Santiago, Chile
Phono: 671-7130 anexo 267
----- Original Message -----
From: <gcruces@worldbank.org>
To: <statalist@hsphsun2.harvard.edu>
Sent: Wednesday, August 28, 2002 4:19 PM
Subject: st: Extensions to: Creating variables recording properties of the
other members of a group


> When working with twin household/individual datasets, this is one of the
most
> useful FAQs:
> http://www.stata.com/support/faqs/data/members.html
> However, there are a few issues I couldn't solve with the information
included
> there, or not efficiently at least. I would like to solve my problem and,
if
> worthwhile,  write an extension of the FAQ. The problem refers to the fact
that
> sometimes you may need to record for one individual the properties not of
the
> whole group, but of another member of the group in particular.
> In my example, I have a household survey where I don't have direct
information
> about the number of kids of each individual, but I have something like
this:
> hhid and member are just the household id and number of member. Variables
> fatherm and motherm tell you the number of the member of the father and
the
> mother, if in the household:
> hhid member fatherm  motherm
> 1    1    -    -
> 1    2    -    -
> 1    3    1    2
> 1    4    1    2
> 1    5    1    2
>
> 2    1    -    -
> 2    2    -    1
> 2    3    -    2
> ...
> Family one is a couple with three kids. Family two is a grandma, the
daughter,
> and a grandchild.
> I want to create the variable ownkids that gives me the number of own kids
> living in the house:
> hhid member ownkids
> 1    1    3
> 1    2    3
> 1    3    0
> 1    4    0
> 1    5    0
>
> 2    1    1
> 2    2    1
> 2    3    0
>
> My force brute solution, which makes a lot of unnecessary comparisons and
takes
> very long (because I generate and drop many variables) is of the form:
with
> maxmem being the number of members of each household (group i, max is the
number
> of groups),
> forvalues i = 1/`max' {
>      qui sum member if group==`i'
>      local maxmem=r(max) forvalues j = 1/`maxmem' {
>      di "-----------Household number `i', number of members: `maxmem'"
>      forvalues k = 1/`maxmem' {
>           di "Household `i', member `j', comparing with `k'"
>           qui gen a=motherm==`j' if member==`k'&group==`i'
>           qui egen b=max(a)
>           qui replace mkids=mkids+b if member==`j'&group==`i'
>           drop a b
>           qui gen a=fatherm==`j' if member==`k'&group==`i'
>           qui egen b=max(a)
>           qui replace fkids=fkids+b if member==`j'&group==`i'
>           drop a b
>           }
>      }
> }
>
> This creates two variables, mkids and fkids, which are the number of kids
for
> mothers and fathers. For each member of the household, I compare if . The
egen,
> replace, drop, takes very long, and even longer if the dataset in memory
is
> large (I had to partition the dataset in 25 parts to make this run
faster).
> The main problem (the main awkwardness in this program) is that I gen,
egen,
> etc. because I could not just create a scalar that reflects the value of a
> variable for one precise observation, something of the form (which of
course
> doesn't work):
> local a=mother==`j'    if member==`k'&group==`i' (meaning: mother etc.
should
> refer to the observation: member==`k'&group==`i')
> I coudn't use something like motherm[_...] becauseI was not using by: ...
.
> What I would like to know if there are more efficient ways of doing this
(I'm
> sure there are!).
>
> Thank you all
>
> ***************************
> Guillermo Cruces
>
> *
> *   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/


______________________________________
Universidad Alberto Hurtado
http://www.uahurtado.cl
*
*   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