Hi, I would like to take the levels of a variable (string) in one file and use that list to loop through variables in another file. Particularly I want to do some variable transformations only to the variables in the list of that variable transformation.

I have loaded the dataset where I need to loop through variables (do the transformations), and using preserve and restore I create the locals with the levels of the vars there. however, when I want to call this locals some few lines later, it doesn't work.

I think the local is being created because I can see the different strings being quoted after the command creating the local, but then when calling it nothing happens. Should I call it differently if the content is a string? Or would it not work because I'm in the preserve/restore environment?
If there's any alternative could you please let me know?

Thanks a lot



use "${dta_path}/dataset_raw.dta", clear

This is the part of the code I have:

*generate a local with the vars to be transformed for each transformation
preserve
import excel using "${codes_path}\havercodes_list_test.xlsx", sheet("variable_transformation") firstrow clear
drop if var == ""
ds var, not
local transf `r(varlist)'
foreach t of local transf {
tostring(`t'), replace //to be able to replace by a string
replace `t' = var[_n] if `t' == "1" // to make a list of vars for that transf
levelsof `t', local(vars_`t') clean
}
restore


*3y average (avg_3y) (one of the vars in the previous files is called avg_3y so a local called vars_avg_3y should have been created
foreach v of local vars_avg_3y {
by country_code: egen `v'_3y = filter(`v'), lags(0/12) normalise
}