*# var # {cmd:random id} - Generate an anonymous replacement for an id variable # help *! 9sep2006 program randid version 8.2 syntax anything(equalok) , [Range(numlist ascending integer min=1 max=2) Save] gettoken newvar anything: anything, parse("=") gettoken equals oldvar: anything, parse("=") confirm var `oldvar' confirm new var `newvar' preserve keep `oldvar' qui duplicates drop local tot=_N if mi("`range'") { local r1 1 local r2= _N } else if (`:word count `range''==1) { local r1 `range' local r2=10^(`:length local r1')-1 } else gettoken r1 r2: range if (`r2'-`r1'+1<_N) mata: errel("There are more IDs than ID numbers in the specified range") gen `newvar'=0 mata: Randomid("`newvar'",`r1',`r2') if ~mi("`save'") { window fsave cfile "Save a copy of the crosswalk" "Stata data file|*.dta" if ~mi("$cfile") qui savel $cfile, quiet else disp "Crosswalk not saved" } if mi("$cfile") { tempfile oldids qui savel `oldids', quiet global cfile `oldids' } restore qui mergel `oldvar' using $cfile, keep(`newvar') ex macro drop cfile display "{txt:`tot' random values created in `newvar'}" end version 9.2 mata: void Randomid (string scalar var,real scalar r1, real scalar r2) { theview=0 st_view(theview,.,var) theview[.]=jumble(r1::r2)[|1\rows(theview)|] } end