*! version 1.0.1  PR 08May98.
program define stgtplot
version 5.0
st_is
*if "$S_E_cmd2"!="stcox" {
*	error 301
*}
local varlist "req min(1)"
local if "opt"
#delimit ;
local options "CI LEvel(int $S_level) Connect(str) Symbol(str) YLIne(str)
 TItle(string) L1title(str) B2title(str) noRescale MEan *" ;
#delimit cr
parse "`*'"
local nvar : word count `varlist'
if `level'<10 | `level'>99 { error 198 }
local l1 "l1title(`l1title')"
if "`rescale'"=="" { local rescale "Rescale"}
if "`l1title'"=="" {
	local l1 "Parameter 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 "Grambsch and Therneau Plot"
}
if "`yline'"=="" { local yline 0 }
local yl "yline(`yline')"
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
local prefix GT
parse "`varlist'",parse(" ")
local i 0
while "`1'"!="" {
	local i=`i'+1
	cap unabbrev "`prefix'`1'"
	if _rc {
		di in red "`1' not found"
		exit 111
	}
	local gvar $S_1
	local vlist "`vlist' `gvar'"
	local stlist "`stlist' `tvar' `gvar'"
	local vlab`i' : var lab `gvar'
	local vlab`i' : word 1 of `vlab`i''
	local y `gvar'
	mac shift
}
quietly {
	if `nvar'>1 {
		preserve
		if "`if'"!="" {
			keep `if'
			local if
		}
		keep $S_E_depv `vlist'
		describe, short
		if _result(1)==0 {
			error 2000
		}
		local maxobs =_result(4)
		local reqobs=`nvar'*_result(1)
		if `maxobs'<`reqobs' {
			tempfile user
			save `user'
			drop _all
			local reqobs=int(`reqobs'*1.02)+100
			set maxobs `reqobs'
			use `user'
			erase `user'
		}
		tempvar tvar y
		stack `stlist', into(`tvar' `y') clear
		lab var `tvar' "Time"
		tempvar param
		rename _stack `param'
		local _stack "`param'"
		local byst "by `param':"
	}
	tempvar Y sm
	if "`ci'"!="" {
		tempvar lci uci se
	}
	if `nvar'>1 {
		gen `Y'=.
		if "`ci'"!="" {
			gen `lci'=.
			gen `uci'=.
		}
		local j 0
		while `j'<`nvar' {
			local j=`j'+1
			local slab "`slab' `j' `vlab`j''"
			if "`ci'"!="" {
				running `y' `tvar' if `_stack'==`j', `ci' /*
				 */ gen(`sm') gense(`se') nograph /*
				 */ `mean'
				replace `lci'=`sm'-`z'*`se' if `_stack'==`j'
				replace `uci'=`sm'+`z'*`se' if `_stack'==`j'
				drop `se'
			}
			else {
				running `y' `tvar' if `_stack'==`j', /*
				 */ gen(`sm') /*
				 */ nograph `mean'
			}
			replace `Y'=`sm' if `_stack'==`j'
			drop `sm'
		}
		tempname stlab
		lab def `stlab' `slab'
		lab val `_stack' `stlab'
		local by "by(`param') `rescale'"
	}
	else {
		if "`t1title'"=="" { local t1 "t1title(`vlab1')" }
		if "`ci'"!="" {
			running `y' `tvar' `if', gen(`Y') `ci' /*
			 */ gense(`se') nograph `mean'
			gen `lci'=`Y'-`z'*`se'
			gen `uci'=`Y'+`z'*`se'
		}
		else {
			running `y' `tvar' `if', gen(`Y') /*
			 */ nograph `mean'
		}
	}
}
sort `_stack' `tvar'
graph `Y' `lci' `uci' `tvar', s(iii) c(llll) ti("`title'") /*
 */ xsca(0,.) yline(`yline') `by' `l1' `t1' `options'

if `nvar'>1 {
	if `maxobs'<`reqobs' {
		drop _all
		quietly set maxobs `maxobs'
	}
}
end