Immo
Immo

Reputation: 111

Stata Panel: How to assign value to a group, merge?

I have a panel of firms (id, year). Each firm also belongs to a specific country. Now I would like to add a GDP time series by country.

The goal is that e.g. each firm from the US is assigned the US' GDP of that year, etc.

Can anyone tell me how to go about that? Is there some way to use the merge command here?

Upvotes: 1

Views: 628

Answers (1)

StasK
StasK

Reputation: 1555

As the help file for merge rightfully states, merge m:m is a wrong approach most of the time. If you have the firm data set as

clear
input country id year profit
1 1 2010 152
1 1 2011 -8
1 2 2010 1090
2 3 2010 502
2 3 2011 -15
end
tempfile firms
save `firms'

and the country file

clear
input country year gdp
1 2010 287582354
1 2011 298723412
2 2010 89734531
2 2011 87340120
end
tempfile gdp
save `gdp'

then starting from your firms file, you can merge the results as follows:

use `firms', clear
merge m:1 country year using `gdp'
* check results
tab _merge
assert _merge == 3
* clean up the remaining mess
drop if _merge !=3
drop _merge

This should produce the output that looks like

Result                           # of obs.
-----------------------------------------
not matched                             0
matched                                 5  (_merge==3)
-----------------------------------------

and if you don't have anything that was not matched (which is my verification and clean up code looks at), you should be good to go.

Upvotes: 1

Related Questions