I need to merge two datasets. One has individual-level data and the other has county-level data.


Individual Level Dataset
Year ID County
2010 1 A
2010 2 B
2010 3 C
2010 4 C
2011 5 B
2011 6 B
2011 7 B

County Level Dataset
Year County Rate County
2010 10 A
2010 22 B
2010 33 C
2011 44 A
2011 26 B
2011 55 C
2011 64 D

What I need
Year County Rate County ID
2010 10 A 1
2010 22 B 2
2010 33 C 3
2010 33 C 4
2011 26 B 5
2011 26 B 6
2011 64 D 7

The end result I want is to make sure that each individual has a county rate associated with them based on the year and county in which they live. I have been trying to use the merge command with no success.

Sample code:

use mydata1

sort id year county countyrate

save mydata1, replace


use mydata2

sort id year county

save mydata2, replace

merge 1:1 id year county countyrate using mydata1

Can anyone provide assistance with this?