Hello, I am trying to make the following code work but it returns "var not found r(111);" I understand the problem is in the else statement. Any help will be appreciated, thank you, Alberto
*****
version 17
clear all
set more off
***************************************GEN TOY DATASET**************************
input str2(x1 x2 x3 x4)
1 4 6 Su
2 Su 7 9
3 Un 8 10
4 7 9 Un
5 5 10 12
end
destring _all, replace //note: x2 & x4 remain strings
****************************************PROGRAM*** ******************************
local cleanvar x1 x2 x3 x4
foreach var of local cleanvar {
capture confirm string variable `var'
if !_rc {
*here do nothing because numeric
}
else {
replace `var' =".a" if var == "Su"
replace `var' =".b" if var == "Un"
destring `var', replace
}
}
saveold try_tidy.dta, replace
Related Posts with Loop through string and numeric variables
parplot: different color for byvars when using by() optionDear Stata users, I have a question about -parplot- command (SSC). This command can produce paralle…
Creating dummy variables using tabulate (Too many values)Hi, I would like to create about 20,000 dummy variables from one variable studentID. When I used th…
Do you have a favorite command or feature in Stata?Dear Stata user, About 7 years ago, StataCorp officially had a contest in Facebook that let contest…
Pooled cross section dataHi Stata users I am working with pooled cross section data (randomly sampled 26,000 SMEs over the la…
Multilevel bootstrappingHello, I am interested in bootstrapping multilevel models like this example, from the Stata manual …
Subscribe to:
Post Comments (Atom)
0 Response to Loop through string and numeric variables
Post a Comment