Multiple encode commands with foreach

So I have a bunch of string variables that cannot be destringed and encode does the job. However, I have many and found this code especially compelling since it uses the names of the other variables so I don't have to do a lot of renaming. However I hit a snag; I get an error from this telling me that it can't locate my first variable in varlist and I can see why; it has put a capitol E in front of it (so if the variable is country it says can't find Ecountry). I can't figure out what the problem is.

foreach v of var varlist {
drop `v'
rename E`v' `v'
I would recommend a different approach, if you have Stata 12 or higher.

rename E* *
If you have a version of Stata lower than 12, you can use the -renpfix- command.
Last edited:
No need for the loop. Just use my one line of code instead of a loop. That will remove E prefix from any variable in your data set.