Mastering Stata’s MERGE: Syntax, Pitfalls, and Verification
Stata’s MERGE command fuses two datasets by key variables, producing a _merge variable that flags match status. This guide covers syntax, a worked example, pitfalls, and verification steps to ensure accurate data integration.
18 Jul 2025, 14:33 UTC

What MERGE Does
In Stata, merge fuses two datasets by one or more key variables. The result is a new dataset that contains every observation from both files and a system variable _merge that tells you how each row was matched:
1– observation only in the master file2– observation only in the using file3– observation matched in both files
Key Syntax and Options
The core syntax is:
merge [cardinality] key_vars using file [, options]
Cardinality specifies the relationship between key variables in the two files:
1:1– each key appears once in each file1:m– master key unique, using key may repeatm:1– master key may repeat, using key uniquem:m– both keys may repeat (rare, use with caution)
Common options:
keep()– control which observations remain (e.g.,keep(match master using)keeps all)keepusing(varlist)– keep only selected variables from the using filenogen– suppress creation of the_mergevariable (use only when you will add your own)sort– skip the automatic sort (only if you know the files are already sorted correctly)
Worked Example
Assume you have a master file master.dta with a unique identifier id and a using file panel.dta that contains additional variables for the same id values. You want to keep all variables from both files and retain a record of the match status.
- Load the master dataset in the Stata session. This must be the active dataset.
- Run the merge:
sort id
merge 1:1 id using panel.dta, keepusing(var1 var2) keep(match master using) nogen
Explanation of the command:
sort id– ensures the master dataset is sorted by the key before merging.merge 1:1 id– tells Stata thatidis unique in both files.using panel.dta– the file to merge in.keepusing(var1 var2)– only bringvar1andvar2from the using file; other variables are dropped.keep(match master using)– keep all observations, even those that appear only in one file.nogen– do not create the_mergevariable because we will create it ourselves later.
After the merge, verify the result:
tabulate _merge
assert _merge==3 if id==<expected_id>
The tabulate command shows the count of each match type. The assert line checks that a specific id that should have matched indeed has _merge==3.
Common Pitfalls and How to Avoid Them
- Unsorted Key Variables – If either dataset isn’t sorted by the key variables in the same order, Stata will issue a warning and may merge incorrectly. Always run
sort keyvarson both datasets before merging. - Duplicate Keys – Using
1:1when a key repeats in either file triggers an error. Verify uniqueness withduplicates report idbefore merging. - Wrong Cardinality – Choosing
1:morm:1when the relationship is actually one-to-one can duplicate rows or lose data. Inspect the data first to decide. - Unintended Row Retention – The default
keep(match master using)keeps all rows, potentially inflating sample size. Usekeep(match)if you only need matched observations.
Limitations and Practical Checks
The merge command does not handle complex joins like full outer joins across multiple keys beyond the cardinality options. For more advanced merging, you may need to reshape data or use joinby.
Practical verification steps:
- After merging, run
tabulate _mergeand compare the counts to the expected number of unique keys in each file. - Check that the total number of observations equals the sum of unique keys minus any duplicates that were intentionally removed.
- Use
assert !missing(id) if _merge==3to confirm that all matched rows have a valid key.
Conclusion
By sorting, choosing the correct cardinality, and inspecting the _merge variable, you can reliably combine datasets in Stata and catch common errors early.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.