search across all the following databases:
Data and documentation
Common questions and answers.
Entire collection of data resources.
I have duplicate variable names in two data sets that I would like to merge using stata. Do I have to go through and change variable names?
By default, stata keeps the values of the "master" file when a merge involves variables with the same name in both files.
The merge command includes an update option to override this default. However, it is best to rename your variables so that the master and using files have unique variable names.
One way to rename variables is to use the renpfix command. This command allows you to add a prefix to variable names. Thus, all variables in your hospital file could have an h_ appended to them. Unfortunately, this requires:
renpfix a* h_ renpfix b* h_
etc., unless you can figure out how to use the foreach command.
You have lots of variables, so typing in the renpfix command 26 times is less tedious than renaming each item one by one.