Hi, this sounds like an intuitively simple task, while I got stuck.
The goal is to add double quotation marks to some of the variables in the dataset with an excluding list.
My code:
However, all variables had quotation marks added. It seems the "local exclude..." doesn't work.
Sample data:
forum test.dta
Thank you.
The goal is to add double quotation marks to some of the variables in the dataset with an excluding list.
My code:
Code:
// Retrieve all variable list
unab allvars : _all
// Turn all variables into string
foreach var of local allvars {
capture confirm variable `var', string
if _rc {
tostring `var', replace
}
}
// Trim all string variables
foreach var of local allvars {
replace `var' = trim(`var')
}
replace dob = "0" + dob if length(dob) == 7
replace commencementdate = "0" + commencementdate if length(commencementdate) == 7
replace cessationdate = "0" + cessationdate if length(cessationdate) == 7
// Exclude the following variables (no quotation marks to be added)
local exclude "episodeid dob dobstatus commencementdate cessationdate"
foreach var of local allvars {
if "`exclude'" != "`varname'" {
gen temp_var = `"""' + `var' + `"""'
replace `var' = temp_var if temp_var != ""
drop temp_var
}
}
Sample data:
forum test.dta
Thank you.
Comment