Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Data Manipulation--is the merge command appropriate?


From   "Maarten Buis" <M.Buis@fsw.vu.nl>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Data Manipulation--is the merge command appropriate?
Date   Mon, 25 Sep 2006 17:45:23 +0200

Elizabeth:
It should work. Below is an example using the data and the method 
that you sketched, and it works. (copy everything between 
*--- begin example ---- and *---- end example --- in the do-file 
editor and do it. You can get the do-file editor by typing 
-doedit- without the hyphens) Note that the tempfile statement
together with the ` and the ' when saving data are not strictly 
necessary, but this way no files are left behind on your hard disk 
when you run this example. See help tempfile if you are interested.  

HTH,
Maarten

*----------- begin example -------------
clear
tempfile data countries
input Countryid year foreignaid
1 1993 230
1 1994 130
2 1990 100
2 1994 120      
end
sort Countryid year
save `data', replace

clear
set obs 2
gen Countryid = _n
save `countries', replace

clear
set obs 4
gen year = 1989 + _n 
cross using `countries'

sort Countryid year

merge Countryid year using `data'
sort Countryid year
recode foreignaid .=0 
/*be careful that all . are due to no aid and not real missing values*/

list
*--------------- end example -----------------



-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology 
Vrije Universiteit Amsterdam 
Boelelaan 1081 
1081 HV Amsterdam 
The Netherlands

visiting adress:
Buitenveldertselaan 3 (Metropolitan), room Z434 

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Asiedu, Elizabeth
Sent: maandag 25 september 2006 17:13
To: statalist@hsphsun2.harvard.edu
Subject: st: Data Manipulation--is the merge command appropriate?

I have an unbalanced panel dataset. I'd like to convert to a balanced panel dataset where the data for the missing years are zero. How do I do this in Stata? I'll use an example to illustrate my problem.





Dataset 1

Country id        year                  foreign aid

1                      1993                230

1                      1994                130

2                      1990                100

2                      1994                120     



There are 2 countries. Country 1 did not receive any aid for 1990,1991, and 1992. Country 2 did not receive aid for 1991, 1992 and 1993. I'd like each country to have data from 1990-1994, and for foreign aid=0 for the missing years.





The dataset I need for my analysis should look like



Dataset 2

Country id        year                  foreign aid

1                      1990                0

1                      1991                0

1                      1992                0

1                      1993                230

1                      1994                130

2                      1990                100

2                      1991                0

2                      1992                0

2                      1993                0

2                      1994                120





This is what I tried.

Step 1: I created another dataset with only two columns; country id and year:



Dataset 3

Country id        year                 

1                      1990               

1                      1991               

1                      1992               

1                      1993               

1                      1994               

2                      1990               

2                      1991               

2                      1992               

2                      1993               

2                      1994               



Step 2: I merged the two datasets (i.e., Dataset 1 and Dataset 3) using country id and year.



I thought the merge will generate a balanced panel with "null" values for the variable foreign aid, for the years for which the country did not receive any aid. I could then replace the "null" with zeros. However the merge did not produce the desired result-it just did not work. 



Any suggestions? Is the merge command appropriate?



Thanks in advance.



Elizabeth Asiedu


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

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