In Stata, how do I merge two data sets in the many-to-one relationship?
Note: The .merge command supports 1:1,
1:m, m:1, and m:m match merges. For 1:1, see In Stata, how do I merge two data sets?
To merge two data sets, A.dta and B.dta (see
below), which share the same key variable, id :
Data set A.dta :
Data set B.dta :
Stata 11
Sort by key variable(s) first, and then enter the merge command, making sure the data set with the "many" observations is the current data set in memory (for m:1 merges). An example using the above data sets follows:
Stata 10 and earlier
Use the .joinby command:
Results
In both versions, the .merge command creates a variable
_merge indicating the results. For example, the
designation (3) in the column on the right means that the
observation appears in both data sets and that they're matched:
For more about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.
Last modified on March 25, 2011.







