I want to encode a list of variables *if* the respective variable is of type string, else I simply want to generate a variable with missing values and a label defined with the name of that variable.
An example of my data:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str8 rts2270890 byte rts2270892 "pjduty" . "pjattri1" . "pjattri2" . "pjattri3" . "pjbribes" . "" . "" . "" . "" . "" . end
Code:
ocal n = 0
foreach v of varlist rts* {
local n = `n'+1
local ns : di %03.0f `n'
ds `v', has(type str# strL)
if "r(varlist)" != "" {
di "`r(varlist)'"
encode `v', gen(ba_pt`ns')
}
else {
gen ba_pt`ns' = .
lab def ba_pt`ns' 1 " "
}
}
0 Response to conditional encode
Post a Comment