Statalist


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

st: re: merging data sets


From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: re: merging data sets
Date   Wed, 20 Jan 2010 08:14:31 -0500

<>
Fatih said

I want to merge two different data sets from different data sources. In
one data set I have 120 countries and other is about 140. Basically in the
bigger one there are more countries. I tried to merge them according to
country name and year but it did not work due to sorting problem.
Another problem is also my country variable; it was string and therefore I
used encode commend to change its format and to obtain ID numbers.

Data set 1
Country    	id 	year   		ppi
USA     	1	1999		2
USA		1	2000		5
USA		1	2001		0
UK		2	1999		1
UK		2	2000		8
CAN		3	2002		6

Data set 2
Country	id    	year   		corr
USA     	1	1999		2
USA		1	2000		1
USA		1	2001		0
CAN		2	2000		1
CAN		2	2002		2



-encode- is obviously not helpful here as it has assigned different integers to CAN in the two datasets, so merging on id is junk.
As you mention sorting with respect to -merge-, I presume you are not using Stata 11, which has a new -merge- syntax obviating the need for sorting. In pre-Stata 11 terms,

use dataset2
sort country year 
save dataset1, replace

use dataset1
sort country year
merge country year using dataset1, unique

should work, as long as country codes are the same in each file. The fact that they are strings is not an issue as long as they are the same string values (CAN not CA, for instance).

Kit

Kit Baum   |   Boston College Economics & DIW Berlin   |   http://ideas.repec.org/e/pba1.html
                              An Introduction to Stata Programming  |   http://www.stata-press.com/books/isp.html
   An Introduction to Modern Econometrics Using Stata  |   http://www.stata-press.com/books/imeus.html


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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index