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

Re: st: Data Processing; How to swap around observations?


From   sdriver@stata.com (Shannon Driver, StataCorp)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Data Processing; How to swap around observations?
Date   Thu, 06 Nov 2003 12:27:13 -0600

Altay Mussurov <aam00@aber.ac.uk> writes:

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

Assuming that there are no observations for members without a spouse in your
data and each member is immediately followed by their spouse, you should be
able to use the following commands to make the switch you want.

    . sort hhid memid
    . egen pairnum = fill(1 1 2 2)
    . sort pairnum memid
    . by pairnum: gen newsmale = cond(_n==1,smale[_n+1],smale[_n-1])
    . by pairnum: gen newsfemale = cond(_n==1,sfemale[_n+1],sfemale[_n-1])

An alternative solution is to generate a single variable that represents the
spouse's schooling level.  To get this, you could use the following code.

    . sort hhid memid
    . egen pairnum = fill(1 1 2 2)
    . sort pairnum memid
    . by pairnum: gen s_spouse = cond(_n==1,s[_N],s[_n-1])

--Shannon Driver
  sdriver@stata.com

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index