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
}
Related Posts with Local storing string content not working
Renaming each variable label as a substring of old labelI have a set of variables v1 through v154 that I want to relabel by excluding the first 16 character…
CI 95% Graph mixed model predictionI can't put IC95 on this graph. Can you help me, please? Thank you very much twoway connected ava t…
Specifying xtpmg optimal lags (2 3 1 2)dear community, i am having trouble specifying the optimal lags (2 3 1 2) chosen by AIC in my pmg mo…
How to check if value labels are consistently defined across multiple data sets using a loop?Hi, I'm trying to append some 35 data sets, one for each state. I used a loop to append them and sa…
Store matrix as r()Hello, Is there a way to store a hand-written matrix as if it is a result of a comment (those that a…
Subscribe to:
Post Comments (Atom)
0 Response to Local storing string content not working
Post a Comment