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

# Re: st: how to switch the values of a variable for two persons within the same household?

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: how to switch the values of a variable for two persons within the same household? Date Sun, 19 Aug 2012 02:31:03 +0100

```This is documented in

SJ-8-4  dm0043  . Tip 71: The problem of split identity, or how to group dyads
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q4/08   SJ 8(4):588--591

http://www.stata-journal.com/sjpdf.html?articlenum=dm0043

bysort HHID : gen v2 = v1[3 - _n] if _N == 2

On Sun, Aug 19, 2012 at 1:21 AM, xueliansharon <xuelianstata@gmail.com> wrote:

> I have a data file containing records for households with either 1 or 2
> persons. I want to generate a new variable v2 based on an old variable v1,
> and the values of the new variable v2 should satisfy the following
> condition:
> If there are 2 persons within the household, then v2 should be constructed
> by switching the values of v1 for the 2 persons, i.e. within each 2-person
> household, v2[1] =v1[2] and v2[2]=v1[1].
> If there is only 1 person within the household, v2 takes missing value.
>
> Here is an example:
> HHID        person ID       v1        v2
> 1             01                 1          3
> 1             02                 3          1
> 2             01                 2          .
> 3             02                10         55
> 3             04                55         10
>
> How to generate such v2 based on v1?
*
*   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/
```