Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Merging two datasets multiple times
From
Maarten buis <[email protected]>
To
[email protected]
Subject
Re: st: Merging two datasets multiple times
Date
Wed, 7 Apr 2010 09:51:20 +0000 (GMT)
--- On Wed, 7/4/10, Pavlos C. Symeou wrote:
> I have two datasets (A and B) which I need to merge.
<snip>
> Basically, for every patent I want to have all its IPCs and
> all SICs in the same row.
A combination of -reshape- and -merge- may be the easiest solution
here:
*--------------------------- begin example -----------------------
// create some example data
clear
input id str4 ipc_1 str4 ipc_2
1 AAAA BBBB
2 AAAA CCCC
3 DDDD CCCC
end
tempfile a
save `a'
clear
input str4 ipc str6 sic_1 str6 sic_2 str6 sic_3
AAAA zzzzz xxxxx yyyyyy
BBBB tttttt rrrrrr ooooo
CCCC www gggg pppp
DDDD foo bar bla
end
tempfile b
save `b'
// the actual answer
use `a'
reshape long ipc_, i(id) j(destination)
rename ipc_ ipc
save `a', replace
use `b'
merge 1:m ipc using `a'
assert _merge == 3
drop _merge
rename ipc ipc_
rename sic_1 sic_1_
rename sic_2 sic_2_
rename sic_3 sic_3_
reshape wide ipc_ sic_1_ sic_2_ sic_3_, i(id) j(destination)
list
*--------------------- end example --------------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )
Hope this helps,
Maarten
--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany
http://www.maartenbuis.nl
--------------------------
*
* 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/