As @Dimitriy V. Masterov points out, rename
(in Stata 12 up) is capable of multiple renamings.
Indeed,
renpfix q5 aspect
would seem to be what you wanted.
The rest of this answer focuses on what was wrong with your approach. In your case, the first time you call up
replace `swap' =
local macro swap
is just spaces, so Stata sees
replace =
which is illegal. But the whole idea of using replace
is wrong here. It is not how to change local macros at all. In short, you are misreading replace
as a way to manipulate strings. It is a way to change the contents of variables.
local letters `c(alpha)'
local i = 1
foreach var of varlist q5_a-q5_o {
local letter : word `i' of `letters'
rename `var' aspects_`letter'
local i = `i' + 1
}
should work. Here we are exploiting the fact that the lower case letters "a" to "z" are already in c(alpha)
: see towards the end of the output of creturn list
for other useful bits and pieces. This could be made shorter:
local letters `c(alpha)'
local i = 1
foreach var of varlist q5_a-q5_o {
local letter : word `i++' of `letters'
rename `var' aspects_`letter'
}
and shorter still:
local i = 1
foreach var of varlist q5_a-q5_o {
local letter : word `i++' of `c(alpha)'
rename `var' aspects_`letter'
}
and shorter still
foreach letter in `c(alpha)' {
rename q5_`letter' aspect_`letter'
}
although as implied earlier this is what renpfix
does for you any way.