Stata merge silently keeps master values when variable names collide
0 reputation · 18 Apr 2021, 02:14 UTC
0 reputation · 18 Apr 2021, 02:14 UTC
When running merge 1:1 between a master and a using dataset that share variable names (other than the key), Stata keeps the master dataset's values and the using dataset's versions are effectively lost. The update and replace options change how missing and non-missing values are handled, but neither preserves both versions of a conflicting variable under distinct names.
The goal is a reproducible workflow where overlapping variables from both sources survive the merge without manual pre-renaming in each dataset beforehand, and without risking irreversible overwrites from replace once the result is saved. Assume a recent Stata version (17 or later) and standard .dta files.
Is there a documented option or supported pattern for rename-on-collision during merge itself, or is pre-processing with rename the only intended approach? Does the behavior differ between merge 1:1 and merge m:1 in any way relevant to collision handling? Is there a built-in way to get a report of which variables were in conflict before values are resolved?
34775 reputation · 18 Apr 2021, 03:52 UTC
In Stata 17 and later, the merge command has no option that automatically renames overlapping variable names between the master and using datasets. When a non-key variable exists in both, merge retains the master dataset's values and discards the using version. This behavior is consistent across merge 1:1 and merge m:1; the match type controls observation pairing and _merge codes, not how name collisions are resolved.
The only documented pathway to preserve both versions is to rename variables in a working copy of the using dataset before merging. For example, loading a temporary copy of the using data, running rename (varlist) (u_) to prefix conflicting non-key variables, then performing the merge will result in distinct variable names in the output, with the original on-disk files untouched.
Before merging, a practical conflict report can be generated with the cf _all using otherfile.dta command. This command lists variables that differ or exist in both datasets, helping you identify which non-key variables need renaming before the merge proceeds.
When applying renaming, always exclude the match key(s); renaming a key variable will cause the merge to fail. Working on copies of the datasets on disk ensures that any subsequent use of update and replace options remains reversible before the final save.
If you share the names of your key variables and the overlapping non-key variables, I can help tailor a rename pattern that skips the keys and avoids merge failures.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.