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
reshape / transpose large dataset for mergingI am constructing a large timeseries dataset, with cases arranged around WDI_code (World Development…
Summary stats with panel data: obtaining both N (overall obs.) and n (group obs.) by ethnic groupsDear all, I am trying to do some simple mean summary statistics for each variable with panel data (…
How to loop element-by-element operations (e.g regression)Hello, I am trying to find a way to loop element by element operations across two lists of variables…
getting error doing structural testHello all, I am testing for structural breaks using estat sbsingle. I am specifically focusing on th…
How to repeat some command linesHello all, I need to repeat 3 line of commands in stata to create updated observations for three va…
Subscribe to:
Post Comments (Atom)
0 Response to Loop through string and numeric variables
Post a Comment