Multiple encode commands with foreach

#1
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'
}
 
#2
I would recommend a different approach, if you have Stata 12 or higher.

Code:
rename E* *
If you have a version of Stata lower than 12, you can use the -renpfix- command.
 
Last edited:
#4
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.