capture program drop fix1psu program define fix1psu, sortpreserve *! v1.1 gmh aug 9 2004 syntax [if] [in] qui svyset local psu "`r(psu)'" global oldpsu "`psu'" if "`psu'"=="" { tempvar id qui gen `id' = _n local psu "`id'" } /* capture which bign if _rc ~=0 { di in gr "command " in ye "bign" in gr " is not installed." di in gr "click " `"{net "describe http://www.unc.edu/~gholmes/stata/smn": here} "' exit } */ local strata "`r(strata)'" global oldstr "`strata'" tempvar touse qui gen `touse' =1 `if' `in' qui count if `strata'==99999 & `touse'==1 capture assert r(N)==0 if _rc~=0 { di in ye "`strata'" in gr " equals 99999 in " in ye `r(N)' in gr " cases" exit } tempvar by_psu_str by_str psusinstr qui egen `by_psu_str'=sum(1) `if' `in' , by( `strata' `psu') qui egen `by_str'=sum(1) `if' `in' , by( `strata' ) *qui bign `strata' `psu' `if' `in', n(`by_psu_str') *qui bign `strata' `if' `in', n(`by_str') capture drop _strata qui gen _strata = `strata' qui count if `by_psu_str'==`by_str' & `touse'==1 qui replace _strata = 99999 if `by_psu_str'==`by_str' di in gr "new stratum = " in ye " _strata " in gr "recoded to 99999 in " in ye `r(N)' " cases " svyset , strata(_strata) end