*! version 1.0.0  6feb1997
program define stcstat /* [if exp] [in range] */
        version 5.0
        st_is

        local if "opt"
        local in "opt"
        local options "noSHow"
        parse "`*'"

        if "$S_E_cmd2" != "stcox" {
                error 301                /* last estimates not found        */
        }

        st_show `show'
        di

        local wt : char _dta[st_w]
        if "`wt'" != "" {
                di in red "stcstat may not be used with weighted data"
                exit 498
        }

        local t0 : char _dta[st_t0]
        if "`t0'" != "" {
                di in red /*
        */ "stcstat may not be used with late entry or time-varing data"
                exit 498
        }

        local t : char _dta[st_t]
        local d : char _dta[st_d]

        tempvar h Dv touse
        quietly {
                predict `h' `if' `in'
                mark `touse' `if' `in'
                markout `touse' `h'

                sort `touse' `h'
                count if `touse'
                local obs = _result(1)
                if $S_E_subj != _result(1) {
                        noi di in blu "note:  " in ye "$S_E_subj" in blu /*
                        */ " obs used in estimating Cox model, whereas" _n /*
                        */ _col(8) in ye _result(1) in blu /*
                        */ " obs used to calculate c statistic"
                        noi di
                }


                local D 0
                local N 0
                local T 0
                local i = _N - `obs' + 1
                while `i' < _N {
                        local j = `i' + 1

                        gen `Dv' = `d'[`i'] & `d' in `j'/l
                        replace `Dv' = 2 /*
                        */ if (!`Dv') & `d'[`i'] & `t'[`i']<=`t' in `j'/l
                        replace `Dv' = 3 /*
                        */ if (!`Dv') & `d' & `t'[`i']>=`t' in `j'/l

                        count if `Dv' in `j'/l
                        local D = `D' + _result(1)

                        count if `Dv' & `h'[`i']==`h' in `j'/l
                        local T = `T' + _result(1)

                        count if `Dv'==1 & `h'[`i']!=`h' & `t'[`i']>`t' /*
        			*/ in `j'/l
                        local N = `N' + _result(1)

                        count if `Dv'==3 & `h'[`i']!=`h' & `t'[`i']>=`t' /*
        			*/ in `j'/l
                        local N = `N' + _result(1)

                        drop `Dv'
                        local i = `i' + 1
                }
        }

        di in gr "Number of subjects:" _col(37) "S = " in ye /*
                */ %16.0g `obs'
        di in gr "Number of comparisons:" _col(37) "D = " in ye /*
                */ %16.0g `D'
        di in gr "Number of orderings as expected:" _col(37) "N = "in ye /*
                */ %16.0g `N'
        di in gr "Number of tied predictions:" _col(37) "T = " in ye /*
                */ %16.0g `T'
        di

        global S_5 = (`N'+`T'/2)/`D'

        di in gr _col(25) "(N + T/2) / D =" /*
                */ _col(50) in ye %7.5f $S_5
        global S_1 `obs'
        global S_2 `D'
        global S_3 `N'
        global S_4 `T'
end
exit