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

st: RE: copying the contents of a variable to another observation


From   "Steven Stillman (LMPG)" <Steven.Stillman@lmpg.dol.govt.nz>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: copying the contents of a variable to another observation
Date   Tue, 9 Sep 2003 16:34:00 +1200

Rebecca.  You have a lot of options here depending on what you are really
trying to end up with.  One very generic way of getting information from all
individuals in a household into each observation is to use reshape.

For example:
bys hhid: gen totmem = _N  (gives you the total number of members in each
household)
reshape wide income education age ... , i(hhid) t(person)  (this will give
you one record per households with everyone's info
expand totmem (you know have the correct number of individual observations)
bys hhid: gen person = _n  (you now have a person number)

gen inc = .
qui su totmem, meanonly
forval i = 1/r(max){
replace income = income`i' if person==`i' 
}

these steps recreate your individual data but keep all your 'wide' household
information as well.

A much simpler approach if you just want to record each person's income data
in each household record is:

qui su totmem, meanonly
forval i = 1/r(max){
gen inc_`i' = income if person==`i'
bys hhid (inc_`i'): replace inc_`i' = inc_`i' [_n-1] if inc_`i' [_n-1]~=. 
};

Steve

> -----Original Message-----
> From:	Rebecca Kalmus [SMTP:kalmus@fas.harvard.edu]
> Sent:	Tuesday, September 09, 2003 10:30 AM
> To:	statalist@hsphsun2.harvard.edu
> Subject:	st: copying the contents of a variable to another obervation
> 
> Does anyone know a simple way to copy the contents of a variable for one 
> observation over to a new variable in a different observation?  For 
> instance, suppose I have a data set consisting of married couples.  I have
> 
> three variables: a household identifier (hhid), a person identifier within
> 
> the household (person) which equals one or two, and a variable for income 
> (income).  I want to create a new variable for spouse's income 
> (inc_spouse).  To do so I code the following:
> 
> by hhid, sort: gen inc_spouse=income[2] if person==1;
> by hhid: replace inc_spouse=income[1] if person==2;
> 
> This is a cumbersome process, which becomes more complicated for more 
> complex data set--if, for instance, the households contain different 
> numbers of people.  In addition, with more complicated data, it becomes 
> more difficult to check whether or not the coding is done correctly.
> 
> Does anyone know a simple way of copying the contents of a variable for 
> one observation over to a new variable in a different observation?  It 
> seems like something that would be done frequently enough that there 
> should be some simple way of doing it.  Any help is much appreciated.
> 
> Rebecca
> 
> 
> 
> 
> 
> *
> *   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/

The information contained in this document is intended only for the
 addressee and is not necessarily the views nor the official 
communication of the Department of Labour.  All final/official papers 
which are sent from the Department will be sent by non-electronic
means, on appropriate letterhead, signed by authorised personnel.
*
*   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–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index