Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: using hierarchical data of household and persons, need to copy some variables from parents observations and append to children


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: using hierarchical data of household and persons, need to copy some variables from parents observations and append to children
Date   Tue, 14 Dec 2010 09:53:21 +0000 (GMT)

--- On Tue, 14/12/10, hilary ligon wrote:
> I am looking at effects of parent's work flexibility on
> children's travel.  So, my regression model and the
> dataset will only include the children.  But, I want to
> include characteristics about the parents. I want to
> write some code that will copy the variable for work
> flexibility from the parent (person 01) and append it to
> the observation for the children (e.g. person 03 and
> person 04).  I want to do this for each household.  Not
> all households have children, so I need to be able to
> check if there are children.  If so, then I need to
> copy parent information to end of child record.
> 
> Some households have one parent and some have two. 
> Some houses also have grandparents, older siblings,
> etc so I can't assume there are the only 2 adults per
> family's household.

The trick would be to create a dataset with one observation
per child that contains the family id and person id of that
child and the characteristics of the parents. Once you have 
that file you can merge it with your main file analysis file
(which also contains only the children).

In order to do this you need a variable that identifies what
the persons role in the family is (parent, grandparent, child,
or other). This information needs to be somehow recorded, it
could be in its own variable, or somehow encoded in the id, but
it needs to be there. In the example below I am assuming it is 
there in the form of the variable role. In this example I 
created some complexities, like extended families, single 
parent families, and just because I am Dutch I also added a 
family with same sex parents. 

Here the trick is to first remove irrelevant persons and families.
than to create as many copies of the parents as there are children
in each familiy. Than create ids such that you can reshape the file
in a way that gives one row per child. In the example I make heavy
use of -bysort- (which out of habit I abreviate to -bys-). A good
tutorial can be found in Nicholas J. Cox (2002) "Speaking Stata: 
How to move step by: step". The Stata Journal, 2(1):86-102.
<http://www.stata-journal.com/article.html?article=pr0004>

*---------------------- begin example -----------------------------
clear
input famid persid role female flexible
      1     1      1    1      1
      1     2      1    0      0
      1     3      2    0      1
      1     4      2    1      0
      1     5      9    1      1
      2     1      0    1      0
      2     2      1    1      1
      2     3      1    1      1
      2     4      2    1      0
      3     1      1    0      1
      3     2      2    1      0
      3     3      2    1      0
      4     1      1    1      1
      4     2      1    0      0
end
label define role 0 "grand-parent" ///
                  1 "parent"       ///
                  2 "child"        ///
                  9 "other"
label value role role

list, sepby(famid)

// remove grandparents and "others"
drop if role == 0 | role == 9

// remove families without children
gen kid = role == 2
bys famid (role) : egen kids = total(kid)
list famid persid role kid kids, sepby(famid)
drop if kids == 0
drop kid

// create par_id
bys famid (role persid) : gen parid = cond(role == 1, _n, 0)

// create one coppy of the parent for each child
replace kids = 1 if role == 2
expand kids

// create a child_id
gen mrole = -role
bys famid (mrole persid) : gen child_id = _n if role == 2
drop mrole
bys famid persid : replace child_id = _n if role == 1
drop role kids 

reshape wide flexible female persid, i(famid child_id) j(parid)

// persid0 is the persid of the child, the remaining id's are useless
rename persid0 persid
drop persid? child_id 

// female0 and flexible0 are characteristics of the child,
// these are superfluous since you would use this dataset to merge it
// back into the analysis dataset that already contains that info
drop female0 flexible0

list , sepby(famid)
*-------------------------------- end example -------------------------
(For more on examples I sent to the Statalist see: 
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


      

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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   |   Site index