*! version 2.0.0  20jun2001
program define stcstat, rclass /* [if exp] [in range] */
        version 7
        st_is 2 analysis
        syntax [if] [in] [, ALL noSHow]
        marksample touse

        if `"$S_E_cmd2"' != "stcox" {
                error 301                /* last estimates not found        */
        }
        tempvar h
        quietly {
                if "`all'"=="" {
                        local restriction "if e(sample)"
                }
                predict double `h' `restriction'
                markout `touse' `h'
        }

        st_show `show'
        di

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


        capture assert `_dta[st_t0]'==0 if `touse'
        if _rc {
                di as err /*
        */ "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 Dv
        quietly {
                sort `touse' `h'
                count if `touse'
                local obs = r(N)
                capture assert e(sample)==`touse'
                if _rc {
                        noi di as txt "{p}" _n /*
                        */ "(note: different samples used to estimate" /*
                        */ " model and to calculate c statistic)" _n   /*
                        */ _n
                }

                local D 0
                local N 0   /* N = # as expected; on output we will call
                               N E */
                local T 0
                local i = _N - `obs' + 1
                while `i' < _N {
                        local j = `i' + 1

                        gen byte `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' + r(N)

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

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

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

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

        di as txt "Number of subjects:" _col(37) "N = " as res /*
                */ %16.0g `obs'
        di as txt "Number of comparisons:" _col(37) "D = " as res /*
                */ %16.0g `D'
        di as txt "Number of orderings as expected:" _col(37) "E = "as res /*
                */ %16.0g `N' /* sic */
        di as txt "Number of tied predictions:" _col(37) "T = " as res /*
                */ %16.0g `T'
        di

        ret scalar C = (`N'+`T'/2)/`D'

        di as txt _col(21) "C = (E + T/2) / D =" /*
                */ _col(50) as res %7.5f return(C)
        ret scalar N = `obs'
        ret scalar D = `D'
        ret scalar E = `N'                /* sic        */
        ret scalar T = `T'
end
exit