# st: RE: Data Processing; How to swap around observations?

 From "Nick Cox" To Subject st: RE: Data Processing; How to swap around observations? Date Thu, 6 Nov 2003 17:33:01 -0000

```I am unclear on how you identify
couples within multiple couple households.

If you can trust the order, i.e. the first two
people in household 2 are one couple, then the
next two, and so forth, a solution might
look like

gen order = _n
bysort hhid (order) : egen coupleid = seq(), block(2)
bysort hhid coupleid : gen smale2 = smale[3-_n]
by hhid coupleid : gen sfemale2 = sfemale[3-_n]

3 - _n is 2 when _n is 1 and vice versa.

Nick
n.j.cox@durham.ac.uk

Altay Mussurov

> I have the following data set containing information on the level of
> schooling (s) for every memeber (memid) of the household
> unit of reference
> (hhid). In my case, I am looking at married couples. There
> are multiple
> couples who reside in the same hhid. The variable "pairs"
> identifies how
> many couples live within the same hhid. Please note that my
> "sex" and
> "memid" variables are not sorted and they can't be sorted.
> This is an
> inherent nature of the data.Head of the household can have
> any "memid" and
> so can his/her spouse.  Hence, my data is organised in the following
> manner:
>
> hhid	memid	sex	s	pairs	smale	sfemale
> 1	1	female	8	1	0	8
> 1	2	male	3	1	3	0
> 2	1	male	12	2	12	0
> 2	2	female	10	2	0	10
> 2	4	female	15	2	0	15
> 2	8	male	10	2	10	0
>
> What I have to do seems easy to achieve. I have to replace
> schooling of
> the male (smale) with the schooling of the female (sfemale)
> taking into
> account the nature of the data set.
>
> This is the expected format:
>
> hhid	memid	sex	s	pairs	smale*	sfemale*
> 1	1	female	8	1	3	0
> 1	2	male	3	1	0	8
> 2	1	male	12	2	10	0
> 2	2	female	10	2	0	12
> 2	4	female	15	2	15	0
> 2	8	male	10	2	0	10
>
> One can use an "egen" command if there was only one pair
> per houeshold
> (sum the schooling level of the male and the female
> respondents and then
> substract and subsitute). Multiple hhid's are problematic.I
> was unable to
> account for them. I tried to reshape the data from "long"
> to "wide" to no
> avail: the fact that "sex" and "memid" do not follow an
> appropriate order
> creates problems.
>
> I hope that you could possible suggest a way to solve this.

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