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

st: RE: Re: two-year panel, combining datasets


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: two-year panel, combining datasets
Date   Mon, 12 Aug 2002 14:58:10 +0100

Andres Vork asked 

> > panel.dta
> > obsid	year
> > 1	2000
> > 2	2000
> > 3	2001
> > 4	2001
> >
> > obsidpairs.dta
> > year2000	year2001
> > 1	3
> >
> > I would like to end up with
> > obsid	year	hhid
> > 1	2000	1
> > 2	2000	2
> > 3	2001	1
> > 4	2001	4

Kit Baum replied 
> 
> You do not give the second record in the 'pairs' dataset, 
> but I presume it 
> should be '2 4' -- which as I understand it would imply 
> that the last 
> element in 'hhid' above should be '2', not '4'. This will 
> achieve that:
> 
> clear
> input obsid year
> 1 2000
> 2 2000
> 3 2001
> 4 2001
> end
> sort obsid
> save panel,replace
> clear
> input obs2000 obs2001
> 1 3
> 2 4
> end
> sort obs2001
> save pairs,replace
> use panel
> rename obsid obs2001
> merge obs2001 using pairs
> replace obs2000 = obs2001 if _merge==1
> drop _merge
> rename obs2001 hhid
> rename obs2000 obsid
> sort hhid
> list

It can also be done with a -reshape-. Let's 
assume Andres' data file names, and also that
the data are as corrected by Kit.

> > panel.dta
> > obsid	year
> > 1	2000
> > 2	2000
> > 3	2001
> > 4	2001
> >
> > obsidpairs.dta
> > year2000	year2001
> > 1	3
    2 4                          <--- correction 
> >
> > I would like to end up with
> > obsid	year	hhid
> > 1	2000	1
> > 2	2000	2
> > 3	2001	1
> > 4	2001	2                   <--- correction      


u obsidpairs 
gen hhid = _n 
reshape long year, i(hhid) j(_year) 
rename year obsid 
rename _year year 
sort obsid 
merge obsid using panel 

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