In Stata, how do I merge two data sets in the many-to-one relationship?
.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?
Suppose you have two data sets,
B.dta (see below), which share the same key variable,
To merge these two data sets, follow the appropriate instructions below.
Stata 11 and later versions
Sort by key variable(s) first, and then enter the
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 versions
In both versions, the
.merge command creates a variable
_merge indicating the results. For example, the
(3) in the column on the right means that the
observation appears in both data sets and that they're matched:
If you have questions about using statistical and mathematical software at Indiana University, email UITS Research Analytics (formerly known as the Stat/Math Center). Research Analytics is located on the IU Bloomington campus at Woodburn Hall 200, and is open for consultation by appointment Monday-Friday 9am-5pm. For more, visit Research Analytics on the web, or call 812-855-4724 (IUB) or 317-278-4740 (IUPUI).
Last modified on December 09, 2013.