I have a dataset that looks as follows:

Code:
clear
input str7 index int data
    "EOSQ041"    42
    "EOSQ041"    13
    "EOSQ049"    17
    "EOSQ049"   210
end
save "my_dataset", replace
I want to create subsets containing data from one index only, and then rename data with a user friendly title.

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')
}
The next code (less clean), doesn't 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')

    local name word("`index_considered_name'", `n')
    * display `name'
    rename data `name'
}
Question: How to dynamically rename my variable in the loop?

(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
}