Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Merging two datasets multiple times

From   "Pavlos C. Symeou" <>
To   "" <>
Subject   Re: st: Merging two datasets multiple times
Date   Wed, 07 Apr 2010 13:01:51 +0200

Thanks Maarten!

On 07/04/2010 11:51, Maarten buis wrote:
--- On Wed, 7/4/10, Pavlos C. Symeou wrote:
I have two datasets (A and B) which I need to merge.
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

*--------------------------- begin example -----------------------
// create some example data
input id   str4 ipc_1  str4 ipc_2
       1         AAAA        BBBB
       2         AAAA        CCCC
       3         DDDD        CCCC

tempfile a
save `a'

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
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)
*--------------------- end example --------------------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen

*   For searches and help try:
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index