*! version 6.0.0  4jul200
program define stpyear, rclass
	version 6
	st_is 2 analysis
	syntax [if], AT(numlist >=0 sort) [noPREserve TRIM SMR OUTput(string asis) /*
	*/ SMR(string) Using(string) Level(real $S_level) PER(real 1) noSHow ]
	if `"`_dta[st_id]'"' == "" {
		di in red "stpyear requires you have previously stset an id() variable"
		exit 198
	}
	tempvar touse
	st_smpl `touse' `"`if'"' `"`in'"' `"`by'"' `""'
	preserve
	qui keep if `touse'
	qui keep _*  `_dta[st_id]' `_dta[st_wv]'
	if `"`smr'"'~="" {      /* smr(filename varname ) */
		tokenize `"`smr'"'
		if `"`2'"'=="" | `"`3'"'~="" {
			di in red /*
			*/"option smr requires two variable names: a group variable and /*
			*/ a rate variable"
			exit 198
		}
		local mergvar=`"`1'"'
		local ratevar=`"`2'"'
		if "`using'"=="" {
			di in red /*
			*/"option smr requires option using() containing" _n "the reference population file name"
			exit 198
		}

		local file=trim(`"`using'"')
	}
	qui stsplit group if  `touse' , at(`at') nopreserve `trim'
	if `"`smr'"'~="" {
		qui gen `mergvar'=group
		sort `mergvar'
		qui merge `mergvar' using `"`file'"'
		local smropt="smr(`ratevar')"
		qui keep if _merge==3
		drop _merge
	}
	st_show `show'
	tempfile myfile1
	local outopt="output(`myfile1')"
	qui strate2 group, `outopt' noshow level(`level') per(`per')
	tempfile myfile2
	local outopt="output(`myfile2')"
	qui strate2, `outopt' noshow level(`level') per(`per')
	if "`smr'"=="" {
		qui use `myfile1', clear
		qui append using `myfile2'
		qui replace group=999 if group==.
		sort group
		qui save `myfile2', replace
		MYdisp `level'
		if "`output'"~="" {
			qui save `output'
		}
		exit
	}
	else {
		tempfile myfile3
		local outopt="output(`myfile3')"
		qui strate2 group, `outopt' noshow level(`level') per(`per') `smropt'
		tempfile myfile4
		local outopt="output(`myfile4')"
		qui strate2, `outopt' noshow level(`level') per(`per') `smropt'
		qui use `myfile1', clear
		qui append using `myfile2'
		qui replace group=999 if group==.
		keep _Y group
		sort group
		qui save `myfile2', replace
		qui use `myfile3', clear
		qui append using `myfile4'
		qui replace group=999 if group==.
		sort group
		merge group using `myfile2'
		qui keep if _merge==3
		MYdisp2 `level'
		if "`output'"~="" {
			qui save `output'
		}
	}
end
prog def MYdisp
	args level
	noi di
	*di in gr _col(12) "|      time at"
 	di in gr "Cohort" _col(12) "| person-years" _col(29) "Failures" _col(45) "rate" /*
	*/ _col(52) "[`level'% Conf. Interval]" _n "-----------+" _dup(59) "-"
	local i 1
	while `i'<_N-1 {
		local l =11 - (length(string(group[`i'])) + length(string(group[`i'+1])) +5)
		 di in gr _col(`l') "[" group[`i'] " - " group[`i'+1] ")" _col(12) "|" _col(16) in yel %10.0g _Y[`i'] /*
		*/ _col(29) %8.0g _D[`i'] _col(39) %10.0g _Rate[`i'] _col(51) %9.0g _Lower[`i'] /*
		*/ _col(63) %9.0g _Upper[`i']
		local i=`i'+1
	}
	local i=_N-1
	local l =11 - (length(string(group[`i']))  +2)

	 di in gr _col(`l') ">=" group[`i'] _col(12) "|" _col(16) in yel %10.0g _Y[`i'] /*
	*/ _col(29) %8.0g _D[`i'] _col(39) %10.0g _Rate[`i'] _col(51) %9.0g _Lower[`i'] /*
	*/ _col(63) %9.0g _Upper[`i'] in gr _n "-----------+" _dup(59) "-"
	local i=_N
	 di in gr "     total" _col(12) "|" _col(16) in yel %10.0g _Y[`i'] /*
	*/ _col(29) %8.0g _D[`i'] _col(39) %10.0g _Rate[`i'] _col(51) %9.0g _Lower[`i'] /*
	*/ _col(63) %9.0g _Upper[`i']
end
prog def MYdisp2
	args level
	noi di
	di in gr _col(12)         "|" _col(29)              "observed" _col(39)  "expected"
 	di in gr "Cohort" _col(12) "| person-years" _col(29) "failures" _col(39) "failures" _col(54) "SMR" /*
	*/ _col(60) "[`level'% Conf. Interval]" _n "-----------+" _dup(67) "-"
	local i 1
	while `i'<_N-1 {
		local l =11 - (length(string(group[`i'])) + length(string(group[`i'+1])) +5)
		 di in gr _col(`l') "[" group[`i'] " - " group[`i'+1] ")" _col(12) "|" _col(16) in yel %10.0g _Y[`i'] /*
		*/ _col(29) %8.0g _D[`i'] _col(39) %8.0g _E[`i'] _col(50) %7.0g _SMR[`i'] _col(59) %9.0g _Lower[`i'] /*
		*/ _col(71) %9.0g _Upper[`i']

		local i=`i'+1
	}
	local i=_N-1
	local l =11 - (length(string(group[`i']))  +2)

	 di in gr _col(`l') ">=" group[`i'] _col(12) "|" _col(16) in yel %10.0g _Y[`i'] /*
	*/ _col(29) %8.0g _D[`i'] _col(39) %8.0g _E[`i'] _col(50) %7.0g _SMR[`i'] _col(59) %9.0g _Lower[`i'] /*
	*/ _col(71) %9.0g _Upper[`i'] in gr _n "-----------+" _dup(67) "-"
	local i=_N
	 di in gr "     total" _col(12)  _col(12) "|" _col(16) in yel %10.0g _Y[`i'] /*
	*/ _col(29) %8.0g _D[`i'] _col(39) %8.0g _E[`i'] _col(50) %7.0g _SMR[`i'] _col(59) %9.0g _Lower[`i'] /*
	*/ _col(71) %9.0g _Upper[`i']
end