Merging Datasets in Stata: A Practical Guide to 1:1, 1:m, and m:1 Joins
Learn how to use Stata’s merge command for 1:1, 1:m, and m:1 joins. Follow a step‑by‑step workflow, see a concrete example, and understand trade‑offs like memory usage and key uniqueness.
06 Jun 2026, 23:11 UTC

Why Merging Matters
In empirical research you almost always need to bring together information that lives in separate files. Whether you’re appending demographic data to survey responses or combining panel observations with administrative records, the merge command is your primary tool in Stata. The challenge is that a bad merge can silently drop observations, duplicate rows, or overwrite variables – all of which corrupt your analysis.
Problem Statement
Suppose you have two datasets: master.dta contains a unique identifier id and a variable age, and using.dta contains the same id plus a variable income. You want a clean, 1:1 merge so that each id appears exactly once in the final dataset. How do you guarantee that the merge behaves as expected and that you can detect problems quickly?
Thesis
By carefully selecting the join type, using the keep() and keepusing() options, and inspecting the system variable _merge, you can perform a robust merge that preserves data integrity and provides a clear audit trail.
Step 1 – Prepare the Datasets
Before merging, ensure each dataset is sorted on the key variables and that the key is unique in the master file for the intended join type.
use master.dta, clear
sort id
save master_sorted, replace
use using.dta, clear
sort id
save using_sorted, replace
Risk: If id is not unique in the master file for a 1:1 merge, Stata will silently drop duplicate matches. Verify uniqueness with:
by id: if _N > 1 {
display "Duplicate id in master: " id
}
Step 2 – Execute the Merge
Choose the appropriate join type. For a strict 1:1 merge:
use master_sorted, clear
merge 1:1 id using using_sorted, keep(master using) keepusing(age income) assert(match) nogen
Explanation of options:
keep(master using)keeps only observations that have matches in both datasets.keepusing(age income)specifies which variables from the using file to import.assert(match)stops the command if any unmatched observations exist.nogensuppresses the generation of the_mergevariable; remove it if you want diagnostics.
After the merge, Stata automatically creates _merge unless you suppress it. The values mean:
| Value | Meaning |
|---|---|
| 1 | Only in master |
| 2 | Only in using |
| 3 | Matched in both |
Diagnosing the Result
Immediately run:
tab _merge
merge report
These commands show the distribution of matched and unmatched observations and give a detailed summary of key matches, missing keys, and overlapping values. If the counts don’t match your expectations, revisit the key uniqueness check.
Step 3 – Handling 1:m and m:1 Joins
When the using file contains multiple rows per key, a 1:m merge is appropriate. The syntax is similar, but you must be sure that the master key is unique:
merge 1:m id using using_sorted
Conversely, if the master file has duplicates and the using file is unique, use m:1:
merge m:1 id using using_sorted
Warning: m:m merges can create combinatorial explosions. Avoid them unless you are certain that each key appears only once in both datasets.
Trade‑Offs and Limitations
- Memory Footprint: Large merges can exhaust RAM. Use
compressbefore merging or split the data into chunks and merge iteratively. - Unintended Dropping: The default
keep(master using)will discard unmatched observations. If you need to keep them for diagnostics, usekeep(match master using). - Version Differences: The behavior of
merge 1:mhas changed in newer releases. Always consulthelp mergefor your Stata version.
Actionable Checklist
- Sort both datasets on the key(s) and ensure key uniqueness where required.
- Choose the correct join type (1:1, 1:m, m:1, or m:m with caution).
- Run the merge with
keep()andkeepusing()options tailored to your needs. - Immediately tabulate
_mergeand runmerge reportto verify the outcome. - If memory issues arise, compress the datasets or merge in smaller subsets.
- Save the merged dataset with a descriptive name and keep the originals for auditability.
By following this workflow, you reduce the risk of silent data loss and gain a clear audit trail that makes post‑merge troubleshooting straightforward.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.