*! Version 1.1.3 (STB-52 ip18.1) program define resample *! Randomly resample existing variables without overwriting them *! Syntax: . resample varlist(min=1) [if] [in] [, Names(string) Retain] *! Version 1.1.3 * John R. Gleason (loesljrg@accucom.net) version 6.0 if "`1'" == "?" { which resample exit } syntax varlist(min=1) [if] [in] [, Names(string) Retain] tokenize `varlist' tempvar use quietly { mark `use' `if' `in' count if `use' if !r(N) { error 2000 } local N1 = r(N) count if `use' in 1/`N1' local sort = (r(N) != `N1') if `sort' { tempvar indx gen long `indx' = _n compress `indx' replace `use' = -`use' sort `use' } tempvar ix gen long `ix' = 1 + int(`N1'*uniform()) in 1/`N1' compress `ix' if "`retain'" != "" { local retain "in 1/`N1'" } local i 1 while "``i''" != "" { local xname : word `i' of `names' if "`xname'" == "" { local xname = substr("``i''", 1, 7) + "_" if "`xname'" == "``i''" { local xname = upper("``i''") if "`xname'" == "``i''" { local xname=lower("``i''") } } } capture confirm new variable `xname' if _rc { replace `xname' = ``i''[`ix'] `retain' } else { local sz : type ``i'' gen `sz' `xname' = ``i''[`ix'] in 1/`N1' local sz : val lab ``i'' if "`sz'" != "" { lab val `xname' `sz' } local sz : format ``i'' format `xname' `sz' } lab var `xname' "Random resample of ``i''" local i = `i' + 1 } if `sort' { sort `indx' } } end