Code:
clear
input str7 index int data
"EOSQ041" 42
"EOSQ041" 13
"EOSQ049" 17
"EOSQ049" 210
end
save "my_dataset", replace
I tried the following script, but I get an error on the rename command.
Code:
local index_considered EOSQ041 EOSQ049
local index_considered_name public_trust transparency_gov_policimaking
forvalues n = 1/`: word count `index_considered'' {
clear
use "my_dataset"
keep if index == word("`index_considered'", `n')
rename data word("`index_considered_name'", `n')
}
Code:
local index_considered EOSQ041 EOSQ049
local index_considered_name public_trust transparency_gov_policimaking
forvalues n = 1/`: word count `index_considered'' {
clear
use "my_dataset"
keep if index == word("`index_considered'", `n')
local name word("`index_considered_name'", `n')
* display `name'
rename data `name'
}
(Note: I'm using two independent local macros: one for the indexes, one for their user-friendly name. I think it would be more elegant to use one unique 2-dimensional macro, but I'm not sure it's possible with Stata...)
Additional question: what if I now want to save my subset? The following doesn't seem to work neither
Code:
local index_considered EOSQ041 EOSQ049
local index_considered_name public_trust transparency_gov_policimaking
forvalues n = 1/`: word count `index_considered'' {
clear
use "my_dataset"
keep if index == word("`index_considered'", `n')
save "dataset_`word("`index_considered_name'", `n')'", replace
}
0 Response to Dynamically rename variable (using "word()")
Post a Comment