*! version 1.0.0 09/21/94 program define dstndiz version 3.1 local varlist "required existing min(3)" local if "opt" local in "opt" local options "BY(string) BAse(string) USing(string) SAVing(string) PRint Format(string) Level(int $S_level)" parse "`*'" parse "`varlist'", parse(" ") local death `1' local pop `2' mac shift 2 local Ga `*' local nblnk = 0 if "`format'"=="" { local format "%10.6f" } quietly capture di `format' 4.2 if _rc { di in red "invalid format string" exit 198 } parse "`format'", parse("%.fg") capture confirm number `2' local fwid = int(`2') local nblnk = cond(10-`fwid'<=0,0,10-`fwid') if "`by'"=="" {exit 198} _crcunab `by' local Gc "$S_1" capture assert `death' >=0 & int(`death')==`death' if _rc { di in red "`death' must have non-negative integer values" exit 499 } capture assert `pop' >=0 & int(`pop')==`pop' if _rc { di in red "`pop' must have non-negative integer values" exit 499 } capture assert `death'<=`pop' if _rc { di in red "`death' must be <= `pop'" exit 499 } if "`using'"!="" & "`base'"!="" {exit 198} if "`base'"!="" { local method 2 } else if "`using'"!="" { confirm file `using' local method 3 } else local method 1 if "`saving'"!="" { confirm new file `saving' } preserve tempfile std tab tempvar newpop if `method'==1 { if "`if'"!="" | "`in'"!="" { keep `if' `in' } keep `Ga' `pop' sort `Ga' quietly { by `Ga': gen double `newpop'=sum(`pop') by `Ga': keep if _n==_N drop `pop' rename `newpop' `pop' } } else if `method'==2 { local type : type `Gc' if substr("`type'",1,3)=="str" { keep if `Gc' == "`base'" } else { local vlab : value label `Gc' if "`vlab'"=="" { confirm number `base' keep if `Gc'==`base' } } if "`if'"!="" | "`in'"!="" { keep `if' `in' } keep `Ga' `pop' sort `Ga' quietly { by `Ga': gen double `newpop'=sum(`pop') by `Ga': keep if _n==_N drop `pop' rename `newpop' `pop' } } else { /* method==3 */ qui use `using', clear capture confirm variable `Ga' `pop' if _rc { di in red "In using data: " _c confirm variable `Ga' `pop' } keep `Ga' `pop' sort `Ga' quietly { by `Ga': gen double `newpop'=sum(`pop') by `Ga': keep if _n==_N drop `pop' rename `newpop' `pop' } } /* drop any missing values in standard population*/ local j 1 parse "`Ga'", parse(" ") while "``j''"!="" { capture drop if ``j''==. capture drop if ``j''=="" local j = `j' + 1 } /* we need to compute and save the standard group fraction before continuing */ qui sum `pop' [w=`pop'] tempvar stdfrac local totpop=_result(2) gen `stdfrac' = `pop'/_result(2) sort `Ga' qui save `std' if "`print'"!="" { di /* take care of any value labels */ local j 1 parse "`Ga'", parse(" ") while "``j''"!="" { local vl : value label ``j'' if "`vl'" != "" { tempvar lab`j' decode ``j'', generate(`lab`j'') drop ``j'' rename `lab`j'' ``j'' } local j = `j' + 1 } /* now. print out the table */ local j 1 local col -6 while "``j''"!="" { local col = 9 + `col' local j = `j' + 1 } local dashes = 28+`col' local d2 = round((`dashes'-19)/2,1) local d2 = cond(`d2'>=80,80,`d2') di in gr _dup(`d2') "-" "Standard Population" _dup(`d2') "-" sort `Ga' di in gr _col(`col') /* */"Stratum Pop. Dist." local dashes = cond(`dashes'>=80,80,`dashes') di in gr _dup(`dashes') "-" local i 1 while `i' <= _N { parse "`Ga'", parse(" ") local j 1 while "``j''"!="" { local type : type ``j'' if substr("`type'",1,3)=="str" { local cl = 10-length(``j''[`i']) di in gr _col(`cl') ``j''[`i'] _c } else { di in gr %9.0g ``j''[`i'] _c } local j = `j' + 1 } di " " %9.0g `pop'[`i'] %10.3f `stdfrac'[`i'] local i = `i' + 1 } local dashes = cond(`dashes'>=80,80,`dashes') di in gr _dup(`dashes') "-" di in gr "Total:" in ye _col(`cl') %14.0g `totpop' } restore, preserve if "`if'"!="" | "`in'"!="" { keep `if' `in' } keep `Ga' `Gc' `death' `pop' sort `Ga' `Gc' /* drop any missing values in data */ local j 1 parse "`Ga'", parse(" ") local start = _N while "``j''"!="" { capture drop if ``j''==. capture drop if ``j''=="" local j = `j' + 1 } local stop = _N local num = `start' - `stop' if `num' > 0 { di in bl "(" `num' " observation" _c if `num'!=1 {di in bl "s" _c} di in bl " deleted due to missing values)" } tempvar gcpop gcdeath left right crude adj qui gen `left' = . qui gen `right' = . qui by `Ga' `Gc' : gen double `gcpop'=sum(`pop') qui by `Ga' `Gc' : gen double `gcdeath'=sum(`death') qui by `Ga' `Gc' : keep if _n==_N drop `pop' `death' sort `Ga' qui merge `Ga' using `std' /* at this point, the data set contains variables `Ga' `Gc', `gcpop', `gcdeath', `pop' and `stdfrac'. We need to calculate `frac' `gcrate', and `pdeath' */ sort `Gc' /* calculate frac */ tempvar gctot frac gcdt qui by `Gc' : gen `gctot'=sum(`gcpop') qui by `Gc' : gen `frac' = `gcpop'/`gctot'[_N] qui by `Gc' : replace `gctot' = `gctot'[_N] qui by `Gc' : gen `gcdt'=sum(`gcdeath') qui by `Gc' : replace `gcdt' = `gcdt'[_N] qui gen `crude' = `gcdt'/`gctot' /* calculate `gcrate' */ tempvar gcrate gen `gcrate' = `gcdeath'/`gcpop' /* calculate pdeath */ tempvar pdeath pdtot gen `pdeath' = `gcdeath'*`stdfrac'/`frac' qui by `Gc' : gen `pdtot'= sum(`pdeath') qui by `Gc' : replace `pdtot' = `pdtot'[_N] qui gen `adj' = `pdtot'/`gctot' /* take care of any value labels */ local j 1 parse "`Ga'", parse(" ") while "``j''"!="" { local vl : value label ``j'' if "`vl'" != "" { tempvar lab`j' decode ``j'', generate(`lab`j'') drop ``j'' rename `lab`j'' ``j'' } local j = `j' + 1 } /* now. print out the table */ sort `Gc' tempvar marker counter qui by `Gc': gen int `counter' = 1 if _n==1 qui replace `counter'=sum(`counter') local j 1 local col -6 while "``j''"!="" { local col = 9 + `col' local j = `j' + 1 } local dashes = 55+`col' qui sum `counter' local start = _result(5) local stop = _result(6) qui gen byte `marker'=. while `start'<=`stop' { di local dashes = cond(`dashes'>=80,80,`dashes') di in gr _dup(`dashes') "-" qui replace `marker'=1 if `counter'==`start' * sort `Gc' sort `marker' di in bl "-> `Gc'= " _c parse "`Gc'", parse(" ") local j 1 while "``j''"!="" { di in bl ``j'' " " _c local j = `j' + 1 } di sort `Ga' di in gr _col(`col') /* */" -----Unadjusted----- Std." di in gr _col(`col') /* */" Pop. Stratum Pop. " di in gr _col(`col') /* */"Stratum Pop. Cases Dist. Rate[s] Dst[P] s*P" di in gr _dup(`dashes') "-" local i 1 local ci_sum 0 /* sum up for conf. intervals */ while `i' <= _N { if `marker'[`i']==1 { parse "`Ga'", parse(" ") local j 1 while "``j''"!="" { local type : type ``j'' if substr("`type'",1,3)=="str" { local cl = 10-length(``j''[`i']) di in gr _col(`cl') ``j''[`i'] _c } else { di in gr %9.0g ``j''[`i'] _c } local j = `j' + 1 } di %11.0g `gcpop'[`i'] _c di %10.0g `gcdeath'[`i'] _c di %7.3f `frac'[`i'] _c di %7.4f `gcrate'[`i'] _c di %7.3f `stdfrac'[`i'] _c di %7.4f `gcrate'[`i'] * `stdfrac'[`i'] local ci_sum=`ci_sum' + /* */ `stdfrac'[`i']*`stdfrac'[`i'] * /* */ `gcrate'[`i']/`gcpop'[`i'] * /* */ (1-`gcrate'[`i']/`gcpop'[`i']) / /* */ `gcpop'[`i'] } local i = `i' + 1 } local dashes = cond(`dashes'>=80,80,`dashes') di in gr _dup(`dashes') "-" sort `marker' local cl = `col' + 6 local ci = invnorm(.5+`level'/200)*sqrt(`ci_sum') qui replace `left' = max(0,`pdtot'/`gctot'-`ci') if `marker'==1 qui replace `right' = `pdtot'/`gctot'+`ci' if `marker'==1 di in gr "Totals:" in ye _col(`cl') %12.0g `gctot' %10.0g `gcdt' in gr " Adjusted Cases:" in ye %9.1f `pdtot' di in gr _col(`cl') _dup(30) " " "Crude Rate:" in ye %9.5f `crude' di in gr _col(`cl') _dup(27) " " "Adjusted Rate:" in ye %9.5f `adj' di in gr _col(`cl') _dup(12) " " `level' "% Conf. Interval: [" in ye %7.5f `left' " , " %7.5f `right' in gr "]" local start = `start' + 1 * qui drop if `marker'==1 qui replace `marker'=. } di di in gr "Summary of Study Populations:" parse "`by'", parse(" ") local j 1 while "``j''"!="" { local k=`j'+1 if "``k''"=="" { local cflag "_c" } else { local cflag " " } local cl = 10-length("``j''") di in gr _col(`cl') "``j''" `cflag' local j=`j'+1 } di _col(14) in gr "N" _col(21) in gr "Crude" /* */ _col(31) in gr "Adj_Rate" _col(46) in gr "Confidence Interval" di in gr " " _dup(74) "-" sort `Gc' qui by `Gc': replace `marker'=1 if _n==1 local i=1 while `i'<=_N { if `marker'[`i'] == 1 { local nblnk = cond(`nblnk'>=79,79,`nblnk') parse "`Gc'", parse(" ") local j 1 while "``j''"!="" { local k=`j'+1 if "``k''"=="" { local cflag "_c" } else { local cflag " " } local type : type ``j'' if substr("`type'",1,3)=="str" { local cl = 10-length(``j''[`i']) di in gr _col(`cl') ``j''[`i'] `cflag' } else { di in gr %9.0g ``j''[`i'] `cflag' } local j = `j' + 1 } #delimit ; di in ye _col(4) %11.0g `gctot'[`i'] _col(16) _dup(`nblnk') " " `format' `crude'[`i'] _col(29) _dup(`nblnk') " " `format' `adj'[`i'] _col(43) in gr "[" in ye _dup(`nblnk') " " `format' `left'[`i'] in gr "," _col(57) _dup(`nblnk') " " `format' in ye `right'[`i'] in gr "]" ; #delimit cr } local i=`i'+1 } end exit