*! version 1.0.1 24apr1996 program define svydes version 4.0 local char1 = substr("`*'", 1, 1) local char3 = substr("`*'", 1, 3) if "`char1'" != "" & "`char1'" != "[" & "`char1'" != "," /* */ & "`char3'" != "if " & "`char3'" != "in " { local varlist "req ex" } local weight "pweight iweight noprefix" local if "opt" local in "opt" local options "STRata(str) PSU(str) FPC(str) BYPsu" parse "`*'" /* Get weights. */ if "`exp'"=="" { var_get pweight `exp', optional local exp "$S_1" } else if "`weight'"=="pweight" { /* try to varset pweight variable */ capture confirm variable `exp' if _rc==0 { var_get pweight `exp' local exp "$S_1" } } if "`exp'"!="" { if "`weight'"=="" { local weight "pweight" } } /* Generate weights if necessary. */ if "`exp'"!="" { capture confirm variable `exp' if _rc { tempvar w qui gen `w' = `exp' } else local w "`exp'" } /* Get strata, psu, and fpc. */ var_get strata `strata', optional local strata "$S_1" capture confirm string variable `strata' if _rc { local mstrata `strata' } var_get psu `psu', optional local psu "$S_1" capture confirm string variable `psu' if _rc { local mpsu `psu' } if "`psu'"=="" & "`bypsu'"!="" { di in red "psu() required with bypsu option" exit 100 } var_get fpc `fpc', optional local fpc "$S_1" /* Mark. */ tempvar doit nm mark `doit' `if' `in' /* Check for negative weights if pweights. */ if "`weight'"=="pweight" { capture assert `w' >= 0 if `doit' if _rc { error 402 } } /* Markout. */ markout `doit' `w' `mstrata' `mpsu' `fpc' capture confirm string variable `strata' if _rc==0 { /* markout string `strata' */ qui replace `doit' = 0 if trim(`strata')=="" } capture confirm string variable `psu' if _rc==0 { /* markout string `psu' */ qui replace `doit' = 0 if trim(`psu')=="" } qui gen byte `nm' = `doit' markout `nm' `varlist' if "`bypsu'"!="" { ByPSU "`weight'" "`exp'" "`strata'" "`psu'" "`fpc'" /* */ `doit' `nm' `varlist' } else ByStrata "`weight'" "`exp'" "`strata'" "`psu'" "`fpc'" /* */ `doit' `nm' `varlist' end program define ByPSU version 4.0 local weight "`1'" local exp "`2'" local strata "`3'" local psu "`4'" local fpc "`5'" local doit "`6'" local nm "`7'" local varlist "`8'" /* only want to know if empty or not */ tempvar miss order quietly { /* Generate strata variable if there isn't one. */ if "`strata'"=="" { tempvar strata gen byte `strata' = 1 local fakestr 1 } /* Compute total #obs. */ count if `doit' local obs = _result(1) count if `nm' local nmobs = _result(1) /* Sort. */ sort `doit' `strata' `psu' /* Compute total #strata. */ by `doit' `strata': gen byte `miss' = (_n==1) count if `miss' & `doit' local nstr = _result(1) /* Compute `miss' = #missing obs in each PSU. */ if "`varlist'"!="" { replace `miss' = !`nm' by `doit' `strata' `psu': replace `miss' = sum(`miss') } /* Compute `nm' = #nonmissing obs in each PSU. */ by `doit' `strata' `psu': replace `nm' = cond( /* */ _n==_N & `doit', sum(`nm'), .) count if `nm'!=. local npsu = _result(1) /* Create `order' variable for listing results. */ gen long `order' = _n if `nm'!=. sort `order' count if `order'!=. local nprint = _result(1) } /* Save macros. */ global S_1 "`nmobs'" global S_2 = `obs'-`nmobs' global S_3 "`nstr'" global S_4 "`npsu'" /* Display output. */ if "`weight'"=="" { local weight "pweight" } if "`exp'"!="" { di _n in gr "`weight':" _col(11) "`exp'" } else di _n in gr "`weight':" _col(11) "" if "`fakestr'"=="" { local strname "`strata'" } else local strname "" di in gr "Strata:" _col(11) "`strname'" _n "PSU:" _col(11) "`psu'" if "`fpc'"!="" { di in gr "FPC:" _col(11) "`fpc'" } local cs = 1 + int((9 - length("`strname'"))/2) local cp = 11 + int((9 - length("`psu'"))/2) if "`varlist'"!="" { di in gr _col(20) "#Obs with #Obs with" _n /* */ " Strata PSU complete missing" _n /* */ _col(`cs') "`strname'" _col(`cp') "`psu'" _col(23) /* */ "data data" local ndup 3 local col 24 local cont "_c" } else { di _n in gr " Strata PSU" _n _col(`cs') "`strname'" /* */ _col(`cp') "`psu'" _col(23) "#Obs" local ndup 2 local col 19 local not "*" } di in gr "--------" _dup(`ndup') " --------" capture confirm string variable `strata' local strstr = _rc capture confirm string variable `psu' local psustr = _rc local i 1 if `strstr' & `psustr' { /* both not strings */ while `i' <= `nprint' { di in ye %8.0g `strata'[`i'] %10.0g `psu'[`i'] /* */ %10.0f `nm'[`i'] `cont' `not' di in ye %10.0f `miss'[`i'] local i = `i' + 1 } } else if !`strstr' & `psustr' { /* strata string, psu not string */ while `i' <= `nprint' { local dstrata = substr(trim(`strata'[`i']),1,8) local cs = 9 - length("`dstrata'") di in ye _col(`cs') "`dstrata'" %10.0g `psu'[`i'] /* */ %10.0f `nm'[`i'] `cont' `not' di in ye %10.0f `miss'[`i'] local i = `i' + 1 } } else if `strstr' & !`psustr' { /* strata not string, psu string */ while `i' <= `nprint' { local dpsu = substr(trim(`psu'[`i']),1,8) local cp = 19 - length("`dpsu'") di in ye %8.0g `strata'[`i'] _col(`cp') "`dpsu'" /* */ %10.0f `nm'[`i'] `cont' `not' di in ye %10.0f `miss'[`i'] local i = `i' + 1 } } else if !`strstr' & !`psustr' { /* both not strings */ while `i' <= `nprint' { local dstrata = substr(trim(`strata'[`i']),1,8) local cs = 9 - length("`dstrata'") local dpsu = substr(trim(`psu'[`i']),1,8) local cp = 19 - length("`dpsu'") di in ye _col(`cs') "`dstrata'" _col(`cp') "`dpsu'" /* */ %10.0f `nm'[`i'] `cont' `not' di in ye %10.0f `miss'[`i'] local i = `i' + 1 } } di in gr "--------" _dup(`ndup') " --------" di in ye %8.0f `nstr' %10.0f `npsu' %10.0f `nmobs' `cont' `not' di in ye %10.0f `obs'-`nmobs' _n in gr _col(21) _dup(18) /* */ "-" _n in ye _col(`col') %10.0f `obs' if _N > `obs' { local count = ("`exp'"!="")+("`strname'"!="") /* */ +("`psu'"!="")+("`fpc'"!="") if "`fpc'"!="" { if `count'>=2 { local fpc "or `fpc'" } local behind 1 } else local behind 0 if "`psu'"!="" { if !`behind' & `count'>=2 { local psu "or `psu'" } else if `behind' & `count'>=3 { local psu "`psu', " } else if `behind' { local psu "`psu' " } local behind 1 } if "`strname'"!="" { if !`behind' & `count'>=2 { local strname "or `strname'" } else if `behind' & `count'>=3 { local strname "`strname', " } else if `behind' { local strname "`strname' " } } if "`exp'"!="" & `count'>=3 { local exp "`exp'," } di in ye _col(`col') %10.0f _N-`obs' in gr /* */ " = #Obs with missing `exp'" _n /* */ _col(`col') " -------- `strname'`psu'`fpc'" _n /* */ in ye _col(`col') %10.0f _N } end program define ByStrata version 4.0 local weight "`1'" local exp "`2'" local strata "`3'" local psu "`4'" local fpc "`5'" local doit "`6'" local nm "`7'" local varlist "`8'" /* only want to know if empty or not */ tempvar onestr nhnm min max quietly { /* Generate PSU variable if there isn't one. */ if "`psu'"=="" { tempvar psu gen long `psu' = _n local fakepsu 1 } /* Compute total #obs. */ count if `doit' local obs = _result(1) count if `nm' local nmobs = _result(1) /* Sort. */ sort `doit' `strata' `psu' `nm' /* Compute total #strata. */ by `doit' `strata': gen byte `onestr' = 1 if _n==_N & `doit' count if `onestr'==1 local nstr = _result(1) /* `nhnm' = #PSU in stratum h with at least some nonmissing obs. */ by `doit' `strata' `psu': gen byte `nhnm' = (_n==_N & `nm') by `doit' `strata': replace `nhnm' = sum(`nhnm') if `doit' if "`varlist'"!="" { tempvar nhm miss /* `nhm' = #PSU in stratum h with all nonmissing obs. */ by `doit' `strata' `psu': gen byte `nhm' = /* */ (_n==_N & !`nm') count if `nhm' & `doit' local mpsu = _result(1) by `doit' `strata': replace `nhm' = sum(`nhm') /* `miss' = #missing obs in stratum h. */ gen byte `miss' = !`nm' by `doit' `strata': replace `miss' = sum(`miss') } /* `nm' = #nonmissing obs per PSU. */ by `doit' `strata' `psu': replace `nm' = cond( /* */ _n==_N & `nm' & `doit', sum(`nm'), .) summarize `nm' local nmpsu = _result(1) local strmin = _result(5) local strmax = _result(6) /* `min' = minimum #nonmissing obs per PSU. */ local type : type `nm' gen `type' `min' = `nm' by `doit' `strata': replace `min' = cond(`min'<`min'[_n-1], /* */ `min',`min'[_n-1]) /* `max' = maximum #nonmissing obs per PSU. */ gen `type' `max' = `nm' by `doit' `strata': replace `max' = cond( /* */ (`max'>`max'[_n-1]&`max'!=.)|`max'[_n-1]==., /* */ `max',`max'[_n-1]) /* `nm' = #nonmissing obs in stratum h. */ by `doit' `strata': replace `nm' = sum(`nm') /* Create new strata variable for listing results. */ if "`strata'"!="" { replace `onestr' = sum(`onestr') if `onestr'==1 } sort `onestr' } /* Save macros. */ global S_1 "`nmobs'" global S_2 = `obs'-`nmobs' global S_3 "`nstr'" global S_4 "`nmpsu'" global S_5 "`mpsu'" global S_6 "`strmin'" global S_7 "`strmax'" /* Display output. */ if "`weight'"=="" { local weight "pweight" } if "`exp'"!="" { di _n in gr "`weight':" _col(11) "`exp'" } else di _n in gr "`weight':" _col(11) "" if "`strata'"!="" { local strname "`strata'" } else local strname "" di in gr "Strata:" _col(11) "`strname'" if "`fakepsu'"=="" { di in gr "PSU:" _col(11) "`psu'" local psuname "`psu'" } else di in gr "PSU:" _col(11) "" if "`fpc'"!="" { di in gr "FPC:" _col(11) "`fpc'" } local cs = 1 + int((9 - length("`strname'"))/2) if "`varlist'"!="" { di in gr _col(30) "#Obs with #Obs with" _col(55) /* */ "#Obs per included PSU" _n /* */ " Strata #PSUs #PSUs complete missing" /* */ _col(51) _dup(28) "-" _n _col(`cs') "`strname'" /* */ _col(11) "included omitted data data" /* */ " min mean max" local ndup 7 local col 34 } else { di in gr _col(39) "#Obs per PSU" _n /* */ " Strata" _col(31) _dup(28) "-" _n _col(`cs') "`strname'" /* */ _col(13) "#PSUs #Obs min mean max" local ndup 5 local col 19 local skipn "_n" local not "*" } di in gr "--------" _dup(`ndup') " --------" local i 1 capture confirm string variable `strata' if _rc { /* strata not a string variable */ while `i' <= `nstr' { di in ye %8.0g `strata'[`i'] %10.0f `nhnm'[`i'] _c `not' di in ye %10.0f `nhm'[`i'] _c di in ye %10.0f `nm'[`i'] _c `not' di in ye %10.0f `miss'[`i'] _c di in ye %10.0f `min'[`i'] %10.1f /* */ `nm'[`i']/`nhnm'[`i'] %10.0f `max'[`i'] local i = `i' + 1 } } else { /* strata is a string variable */ while `i' <= `nstr' { local dstrata = substr(trim(`strata'[`i']),1,8) local cs = 9 - length("`dstrata'") di in ye _col(`cs') "`dstrata'" %10.0f `nhnm'[`i'] _c `not' di in ye %10.0f `nhm'[`i'] _c di in ye %10.0f `nm'[`i'] _c `not' di in ye %10.0f `miss'[`i'] _c di in ye %10.0f `min'[`i'] %10.1f /* */ `nm'[`i']/`nhnm'[`i'] %10.0f `max'[`i'] local i = `i' + 1 } } di in gr "--------" _dup(`ndup') " --------" di in ye %8.0f `nstr' %10.0f `nmpsu' _c `not' di in ye %10.0f `mpsu' _c di in ye %10.0f `nmobs' _c `not' di in ye %10.0f `obs'-`nmobs' _c di in ye %10.0f `strmin' %10.1f `nmobs'/`nmpsu' %10.0f `strmax' `not' di in gr _col(31) _dup(18) "-" _n in ye _col(`col') %10.0f `obs' if _N > `obs' { local count = ("`exp'"!="")+("`strata'"!="") /* */ +("`psuname'"!="")+("`fpc'"!="") if "`fpc'"!="" { if `count'>=2 { local fpc "or `fpc'" } local behind 1 } else local behind 0 if "`psuname'"!="" { if !`behind' & `count'>=2 { local psuname "or `psuname'" } else if `behind' & `count'>=3 { local psuname "`psuname', " } else if `behind' { local psuname "`psuname' " } local behind 1 } if "`strata'"!="" { if !`behind' & `count'>=2 { local strata "or `strata'" } else if `behind' & `count'>=3 { local strata "`strata', " } else if `behind' { local strata "`strata' " } } if "`exp'"!="" & `count'>=3 { local exp "`exp'," } di `skipn' in ye _col(`col') %10.0f _N-`obs' in gr /* */ " = #Obs with missing `exp'" _n /* */ _col(`col') " -------- `strata'`psuname'`fpc'" _n /* */ in ye _col(`col') %10.0f _N } end exit ByStrata 10 20 30 40 50 60 70 12345678901234567890123456789012345678901234567890123456789012345678901234567890 ----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----| #Obs with #Obs with #Obs per included PSU Strata #PSUs #PSUs complete missing ---------------------------- varname included omitted data data min mean max -------- -------- -------- -------- -------- -------- -------- -------- 123456781212345678121234567812123456781212345678121234567812123456781212345678 newstr nhnm nhm nm miss min nm/nhnm max -------- -------- -------- -------- -------- -------- -------- -------- 12345678121234567812123456781212345678 12345678121234567812123456781212345678 nstr nmpsu mpsu nmobs obs-nmobs strmin nmobs/nmpsu strmax ----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----| ------------------ obs 12345678 _N - obs 12345678 = #Obs with missing varname1, -------- varname2, varname3, or varname4 _N 12345678 Header for no varlist 10 20 30 40 50 60 70 12345678901234567890123456789012345678901234567890123456789012345678901234567890 ----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----| #Obs per PSU Strata ---------------------------- varname #PSUs #Obs min mean max -------- -------- -------- -------- -------- -------- 1234567812123456781212345678121234567812123456781212345678 newstr nhnm nm min nm/nhnm max -------- -------- -------- -------- -------- -------- 12345678121234567812123456781212345678 123456781212345678 nstr nmpsu nmobs strmin nmobs/nmpsu strmax ----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----| -------- obs 12345678 _N - obs 12345678 = #Obs with missing varname1, -------- varname2, varname3, or varname4 _N 12345678 di in gr _col(35) "#Obs per included PSU" _n /* */ " Strata" _col(31) _dup(28) "-" /* */ " #PSUs #Obs min mean max" global S_1 "`nmobs'" global S_2 = `obs'-`nmobs' global S_3 "`nstr'" global S_4 "`nmpsu'" global S_5 "`mpsu'" global S_6 "`strmin'" global S_7 "`strmax'" ByPSU 10 20 30 40 50 60 70 12345678901234567890123456789012345678901234567890123456789012345678901234567890 ----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----| #Obs with #Obs with Strata PSU complete missing varname varname data data -------- -------- -------- -------- 12345678121234567812123456781212345678 newstr nhnm nhm nm -------- -------- -------- -------- 12345678121234567812123456781212345678 nstr nmpsu nmobs obs-nmobs ----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----| ------------------ obs 12345678 _N - obs 12345678 = #Obs with missing varname1, -------- varname2, varname3, or varname4 _N 12345678 Header for no varlist 10 20 30 40 50 60 70 12345678901234567890123456789012345678901234567890123456789012345678901234567890 ----+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----| Strata PSU varname varname #Obs -------- -------- --------