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 obervation


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: copying the contents of a variable to another obervation
Date   Mon, 8 Sep 2003 23:50:10 +0100

Rebecca Kalmus
>
> 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.

It is simple, apart from the complications you allude to!

With just two people in a household, the spouse's income is

bysort hhid : gen inc_spouse = income[3-_n]

which includes your two special cases.

If you have some extra code (e.g. -child- is 1 for children
and 0 otherwise), there is stuff like

bysort hhid (child) : gen inc_spouse = income[3 - _n]
                                         if _n < 3 & !child

Otherwise I am not clear what generalisations and/or
complications you have in mind. Nevertheless there is
some advice at

How do I create variables summarizing for each individual
properties of the other members of a group?
http://www.stata.com/support/faqs/data/members.html

Adjacent FAQs could contain helpful examples.

Nick
[email protected]

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