*! version 0.0.1  16oct2020
cap program drop stprisk
program stprisk
	syntax [varlist(default=none)] [in] [if] [, LEVEL(cilevel) Graph NOWhisker]
	marksample touse
	tempvar H Hl Hu group Risk Low High
	sts gen `H' = na if `touse', by(`varlist')
	sts gen `Hl' = lb(na) if `touse', by(`varlist') level(`level')
	sts gen `Hu' = ub(na) if `touse', by(`varlist') level(`level')
	di _n _col(5) "Incidence risk"
	if "`varlist'"=="" {
		qui sum _t, meanonly
		local t = r(max)
		qui sum `H', meanonly
		local risk = 1 - exp(-r(max)/`t')
		qui sum `Hl', meanonly
		local lb = 1 - exp(-r(max)/`t')
		qui sum `Hu', meanonly
		local ub = 1 - exp(-r(max)/`t')
		di "  {c TLC}" _dup(34) "{c -}" "{c TRC}"
		di "  {c |}" _col(10) "Risk"  _col(17) "[  `level'% Conf. Int.  ] {c |}"
		di "  {c LT}" _dup(34) "{c -}" "{c RT}"
		di "  {c |}" _col(6) %7.6f `risk' _col(18) %7.6f `lb' "  " %7.6f `ub' "  {c |}"
		di "  {c BLC}" _dup(34) "{c -}""{c BRC}"
		}
	else {
		qui gen `Risk' = .
		qui gen `Low' = .
		qui gen `High' = .
		di "  {c TLC}" _dup(44) "{c -}" "{c TRC}"
		di "  {c | } " abbrev("`varlist'",13) _col(20) "Risk" _col(27) "[  `level'% Conf. Int.  ] {c |}"
		di "  {c LT}" _dup(44) "{c -}""{c RT}"
		egen `group' = group(`varlist'), label
		qui levelsof `group'
		foreach i of any `=r(levels)' {
			qui sum _t if `group'==`i', meanonly
			local t = r(max)
			qui sum `H' if `group'==`i', meanonly
			local risk = 1 - exp(-r(max)/`t')
			qui replace `Risk' = `risk' if `group'==`i'
			qui sum `Hl' if `group'==`i', meanonly
			local lb = 1 - exp(-r(max)/`t')
			qui replace `Low' = `lb' if `group'==`i'
			qui sum `Hu' if `group'==`i', meanonly
			local ub = 1 - exp(-r(max)/`t')
			qui replace `High' = `ub' if `group'==`i'
			di "  {c |} " abbrev("`:label `group' `i''",13) _col(16) %7.6f `risk' _col(28) %7.6f `lb' "  " %7.6f `ub' "  {c |}"
			}
		di "  {c BLC}" _dup(44) "{c -}""{c BRC}"
		}

	if "`graph'"!="" & "`varlist'"!="" {
		if "`nowhisker'"=="" {
			tw rspike `Low' `High' `group',lc(gs10) || ///
			sc `Risk' `group',mc(navy) legend(off)yti(Risk)
		}
		else {
			tw sc `Risk' `group',mc(navy) legend(off)yti(Risk)
		}
	}

	end