Stata 'not sorted' Error in Merge: The Fix That Takes 5 Seconds
Stata says your data is not sorted. The fix is one line — but understanding why prevents hours of debugging merge issues.
You try to merge two datasets and Stata says your data is not sorted. The fix is one line. But understanding why prevents hours of merge debugging.
variable id does not uniquely identify observations in the master data not sorted r(5);
All examples tested in Stata 18 SE. Compatible with Stata 15+.
Quick Answer
1// Fix 1: Sort before merging (old syntax)2sort id3merge id using schools.dta45// Fix 2: Use modern merge syntax (no sorting needed)6merge 1:1 id using schools.dta78// Fix 3: Sort both datasets9sort id10save master_data.dta, replace11use schools.dta, clear12sort id13save schools.dta, replace14use master_data.dta, clear15merge 1:1 id using schools.dtaBest fix: Use the modern merge syntax (merge 1:1, merge m:1,merge 1:m). It doesn’t require pre-sorting and gives better error messages.
Old Merge vs. Modern Merge
Stata has two merge syntaxes. The old one requires sorting. The modern one (Stata 11+) doesn’t.
// Must sort both datasets first!sort student_idmerge student_id using test_scores.dta// You also need to sort the using dataset// before saving it// No sorting requiredmerge 1:1 student_id using test_scores.dta// Or for many-to-one:merge m:1 school_id using school_data.dta// Or for one-to-many:merge 1:m firm_id using employee_data.dtamerge 1:1,merge m:1, merge 1:m) is clearer, doesn’t require sorting, and creates the _merge variable automatically. If you see code without the type specifier, it’s legacy code.When Sorting Isn’t Enough: Duplicates
Sometimes you sort your data and still get merge errors. This usually means you have duplicate keys where uniqueness is expected.
1// Check if your merge key is unique2isid student_id3// If this fails, you have duplicates45// Find the duplicates6duplicates report student_id7duplicates tag student_id, gen(dup)8list student_id name if dup > 0910// Decide what to do with duplicates:11// Option A: Keep first occurrence12duplicates drop student_id, force1314// Option B: Investigate and fix the data15browse if dup > 0Duplicates in terms of student_id
--------------------------------------
Copies | Observations Surplus
----------+---------------------------
1 | 9,847 0
2 | 306 153
--------------------------------------In this example, 153 student IDs appear twice. You need to resolve these before a merge 1:1will work. Either deduplicate, or use merge m:1 if the duplication is intentional (e.g., panel data with multiple years per student).
duplicates drop, force keeps only the first occurrence. If different duplicates have different values for other variables, you’re silently losing data. Always investigate duplicates before dropping them.Choosing the Right Merge Type
| Type | Master | Using | Example |
|---|---|---|---|
| 1:1 | Unique key | Unique key | Student ↔ test score |
| m:1 | Repeated key | Unique key | Workers ↔ firm characteristics |
| 1:m | Unique key | Repeated key | Firm ↔ worker records |
1// One student, one test score2merge 1:1 student_id using test_scores.dta34// Many workers per firm, one firm characteristic row5merge m:1 firm_id using firm_data.dta67// One firm, many worker records8merge 1:m firm_id using worker_data.dta910// Always check the merge results11tab _merge Result from │
merge │ Freq. Percent Cum.
──────────────────┼───────────────────────────────────────
Master only (1) │ 247 2.47 2.47
Using only (2) │ 183 1.83 4.30
Matched (3) │ 9,570 95.70 100.00
──────────────────┼───────────────────────────────────────
Total │ 10,000 100.00Pre-Merge Safety Checks
1// Before ANY merge, run these checks:23// 1. Verify key uniqueness (for 1:1 or the "1" side of m:1)4isid student_id56// 2. Check for missing values in the key7count if missing(student_id)89// 3. Check the key type matches10describe student_id11// (prevent type mismatch errors)1213// 4. Check overlap14// How many keys from master appear in using?15preserve16use schools.dta, clear17levelsof school_id, local(using_ids)18restore19// Compare with master dataset school IDsSytra catches these errors before you run.
Sytra automatically validates merge keys — checking uniqueness, type compatibility, and expected match rates — before running the merge. No more debugging sort errors or silent mismatches.
Join the Waitlist →FAQ
Why does Stata say “not sorted” when I try to merge?
The old merge syntax (without 1:1, m:1, or 1:m) requires both datasets to be sorted on the merge key. The modern merge syntax does NOT require sorting. Switch to merge 1:1 id using data.dta.
How do I fix the “not sorted” error?
Either sort both datasets with sort id before merging, or use the modern merge syntax which doesn’t require pre-sorting. The modern syntax is always preferred.
Do I need to sort before merge 1:1?
No. merge 1:1, merge m:1, and merge 1:m do not require pre-sorting. Only the legacy syntax (no type specifier) requires it.
Why does sort not fix my merge error?
You likely have duplicate keys. Run isid varname to check uniqueness andduplicates report varname to find duplicates.
Related Guides
- Stata Type Mismatch Error in Merge: String vs Numeric Fix
- Stata Error r(2000): No Observations After Merge
- Stata Error r(198): Invalid Syntax Complete Guide
We build practical, reproducible workflows for Stata and R teams working on real empirical research pipelines.