*! version 1.0.0 27Mar98.
program define stbtplot
version 5.0
st_is
if "$S_E_cmd"!="stbtcalc"	{ error 301 }
local varlist "req min(1)"
local if "opt"
#delimit ;
local options "CI LEvel(int $S_level) Connect(str) Symbol(str) SAving(str) HR
 TItle(string) L1title(str) B2title(str) noRescale MEan SINgle COEF(str)
 SE(str) *" ;
#delimit cr
parse "`*'"
local nvar : word count `varlist'
if `level'<10 | `level'>99 { error 198 }
if "`ci'"!="" & "`single'"!="" {
	noi di in red "ci and single cannot be used together"
	exit 198
}
local l1 "l1title(`l1title')"
if "`rescale'"=="" { local rescale "Rescale"}
if "`hr'"!="" {
	local hr1 "exp("
	local hr2 ")"
	local base 1
	tempvar expy
	qui gen `expy'=.
	local l1p "Hazard Ratio"
}
else {
	local base 0
	local l1p "Parameter"
}
if "`l1title'"=="" {
	local l1 "`l1p' Estimate"
	local l1 "l1title(`l1')"
}
if "`t1title'"!="" {local t1 "t1title(`t1title')"}
if "`b2title'"=="" {
	local b2 : var lab $S_E_depv
	if "`b2'"=="" { local b2 "$S_E_depv" }
	local b2 "b2title(`b2')"
}
else local b2 "b2title(`b2title')"
if "`title'"=="" {
	local title "Beta of t Plot"
}
if "`saving'"!="" { local save "sav(`saving')" }
tempname z
scalar `z' = -invnorm((100-`level')/200)

capture confirm var _touse
if _rc==0 {
	if "`if'"=="" { local if "if _touse==1" }
	else local if "`if' & _touse==1"
}

local tvar $S_E_depv
if "`coef'"=="" {local coef BT}
if "`se'"  =="" {local se BS}
parse "`varlist'",parse(" ")
tempvar lci uci
qui gen `lci'=.
qui gen `uci'=.
local i 0
if "`single'"=="" {
	while "`1'"!="" {
		local i=`i'+1
		vlook4 `1' `coef'*
		local y $S_1
		mlook4 `1'
		local b0: word 2 of $$S_1
		if "`ci'"!="" {
			vlook4 `1' `se'*
			local s $S_1
		}
		local vlab : var lab `y'
		local vlab : word 1 of `vlab'
		local vlab2 : var lab `vlab'
		if "`valb2'"!="" {local vlab "`vlab2'"}
		if "`t1title'"=="" {local t1 "t1title(`vlab')"}
		if "`ci'"!="" {
			qui replace `lci'=`hr1' `y'-`z'*`s' `hr2'
			qui replace `uci'=`hr1' `y'+`z'*`s' `hr2'
		}
		if "`hr'"!="" {
			local b0 = exp(`b0')
			qui replace `expy'=exp(`y')
			local y "`expy'"
		}
		if `nvar'>1 {
			tempfile gr`i'
			set graphics off
			graph `y' `lci' `uci' `tvar', s(iii) c(llll)  /*
 */  ti("`title'") yline(`base',`b0') `l1' `t1' `options' sav(`gr`i'')
			local graphs "`graphs' `gr`i''"
		}
		else {
			graph `y' `lci' `uci' `tvar', s(iii) c(llll)  /*
 */  ti("`title'") yline(`base',`b0') `l1' `t1' `options' `save'
		}
		mac shift
	}
	if `nvar'>1 {
		set graphics on
		gr using `graphs', `save'
	}
}
else {
	local Bs "`base'"
	while "`1'"!="" {
		local i=`i'+1
		vlook4 `1' `coef'*
		local y`i' $S_1
		mlook4 `1'
		local b0: word 2 of $$S_1
		local vlab : var lab `y`i''
		local vlab : word 1 of `vlab'
		local vlab2 : var lab `vlab'
		if "`valb2'"!="" {local vlab "`vlab2'"}
		if "`hr'"!="" {
			local b0 = exp(`b0')
			tempvar expy`i'
			qui gen `expy`i''=exp(`y`i'')
			lab var `expy`i'' `vlab'
			local ys "`ys' `expy`i''"
		}
		else {
			local ys "`ys' `y`i''"
		}
		local Bs "`Bs',`b0'"
		local con "`con'l"
		local sym "`sym'i"
		mac shift
	}
	graph `ys' `tvar', c(`con') s(`sym') ti("`title'") yline(`Bs') /*
	 */ `l1' `t1' `options'  sort `save'
}
end

program define vlook4
	version 3.0
	local varlist "req ex"
	parse "`*'"
	parse "`varlist'", parse(" ")
	local find "`1'"
	mac shift
	while "`1'"!="" {
		local lbl : variable label `1'
		local var : word 1 of `lbl'
		if substr("`var'",1,.)==substr("`find'",1,.) {
			global S_1 "`1'"
			exit
		}
		mac shift
	}
end

program define mlook4
	version 3.0
	local find "`1'"
	local i 1
	while "${B_`i'}"!=""  {
		local var : word 1 of ${B_`i'}
		if substr("`var'",1,.)==substr("`find'",1,.) {
			global S_1 "B_`i'"
			exit
		}
		local i = `i' + 1
	}
end