Renaming Merged Variables in STATA


I am merging two datasets, but the using dataset has variables with the same names as the master. In this case, it overwrites the master variables. Is there a way to have STATA automatically rename the variables in the using dataset... say postfixing them with "_2" or something. (So if "income" exists in the master dataset and in the using dataset, after the merge I want to have "income" and "income_2".)

It's not going to be efficient to rename them manually before the merge as there are 50+ variables with matching names in my using dataset. (I'm actually merging the same using dataset twice on a different key each time... hence the duplicate names the second time I merge.) I will be running this program often, so I want to automate it.


So why don't you rename the variables in the using dataset automaticaly ? For instance by doing smtg like this :

d, varl
local list = r(varlist)
foreach var of local list {
rename `var' `var'2

Or maybe I didn't understand the problem...
That's it, thanks! I'm relatively new to STATA, so I didn't realize there was a foreach command nor did I know how to use it. This solved the problem... thanks!