I am using a rather big data set in terms of variables. I have crop specific variables for 18 crops. Every crop has 44 variables (so basically 44 variables that repeat themselves for 18 different crops giving a total of 792 variables). Some of these variables are string and some are numeric. All of them should be numeric so in need to destring them. (obs I cannot use destring codes because some cells include non numeric values which I first need to eliminate)
I´m using the following loop:
foreach v of varlist example1 example2 example3 {
egen `v'_n = sieve(`v'), char(0123456789.,)
replace `v'_n= subinstr(`v'_n, ",", ".",.)
destring `v'_n, replace
local x : variable label `v'
label variable `v'_n "`x'"
} Which works just fine when I test it on string variables. When I include the numeric variables, I get error messages. However I don’t want to manually check which of the 792 variables are strings so I was wondering how can I alter my code so the loop would run only for string variables?
I found on statalist [https://www.stata.com/statalist/arch.../msg00953.html] the following code:
qui ds *, has(type string)
foreach x of varlist `=r(varlist)' {
do string things
}
So when I alter it in the following manner
qui ds *, has(type string)
foreach v of varlist `=r(example1 – example44)' {
egen `v'_n = sieve(`v'), char(0123456789.,)
replace `v'_n= subinstr(`v'_n, ",", ".",.)
destring `v'_n, replace
local x : variable label `v'
label variable `v'_n "`x'"
}
I get: invalid name varlist required
r(100);
what am I doing wrong?
thank you in advance

0 Response to Run a loop only on string variables (without specifying them)
Post a Comment