*# var # For converting string vars to numeric. Multiple variables can be encoded at once, and the numeric versions replace the string versions, instead of being created as new vars. There is an option to convert all kinds of fields to numeric 0/1, labeled No/Yes. (includes label numbering by default) # help *! 29aug2007 program encodel version 9 syntax varlist, [Label(string) Multilabel yn] foreach v of local varlist { capture confirm str var `v' if (_rc) mata: errel("`v' is not a string variable") } if mi("`yn'") { if mi("`label'") { gettoken label: varlist local label `label'_enc } foreach v of local varlist { encode `v', g(`v'__) l(`=cond(mi("`multilabel'"),"`label'","`v'_enc")') if mi("`number'") & ~mi("`multilabel'") labelnum `v'_enc movel `v' `v'__ drop `v' rename `v'__ `v' } } else { foreach v of local varlist { quietly { preserve keep `v' drop if mi(`v') replace `v'=lower(`v') replace `v'="0" if inlist(`v',"n","no","f","false") replace `v'="1" if inlist(`v',"y","yes","t","true") duplicates drop sort `v' count if (r(N)==2) { if (`v'[1]~="0"|`v'[2]~="1") mata: errel("cannot interpret `v' as yes/no") } else if (r(N)==1) { if (`v'[1]~="0"&`v'[1]~="1") mata: errel("cannot interpret `v' as yes/no") } else if (r(N))>2 mata: errel("cannot interpret `v' as yes/no") restore replace `v'=lower(`v') replace `v'="0" if inlist(`v',"n","no","f","false") replace `v'="1" if inlist(`v',"y","yes","t","true") } destring `v', replace } qlabel (`varlist') 0 "No" 1 "Yes", na(yn) } end