*!version 1.0.1 13sep1996
program define rocse
 version 4.0
 local varlist "req ex max(1)"
 local options "BY(string) AUC(real 0) pause *"
 parse "`*'"
 if "`by'"==""{
     di in red "You must specify the Grouping Variable"
     exit 198
 }
 confirm var `by'
 preserve
 tempvar i S X Y Ux Uy Vx Vy
 quietly{
   keep `varlist' `by'
   keep if `varlist'~=. & `by'~=.
   sort `by'
   if `by'[_N]~=2{
        recode `by' 0=2
        sort `by'
   }
   if `by'[_N]~=2{
        noisily di in red "2 Groups required coded as 1,2 or 0,1"
        exit 198
           }
   sort `varlist'
   gen `X'=`varlist' if `by'==1
   gen `Y'=`varlist' if `by'==2
   gen `Ux'=.
   gen `Vx'=.
   gen `Uy'=.
   gen `Vy'=.
   local i=1
   while `i'<_N+1{
        if `by'[`i']==1{
        tempvar u v
        scalar l=`X'[`i']
        gen `u'=1 if `Y'<l & `Y'~=.
        gen `v'=1 if `Y'>l & `Y'~=.
        replace `u'=. if `Y'>=l|`Y'==.
        replace `v'=. if `Y'<=l|`Y'==.
        replace `u'=sum(`u')
        replace `v'=sum(`v')
        replace `Ux'=`u'[_N] if _n==`i'
        replace `Vx'=`v'[_N] if _n==`i'
        scalar drop l
        drop `u' `v'
        }
        else if `by'[`i']==2{
        tempvar u v
        scalar l=`Y'[`i']
        gen `u'=1 if `X'<l & `X'~=.
        gen `v'=1 if `X'>l & `X'~=.
        replace `u'=. if `X'>=l|`X'==.
        replace `v'=. if `X'<=l|`X'==.
        replace `u'=sum(`u')
        replace `v'=sum(`v')
        replace `Uy'=`u'[_N] if _n==`i'
        replace `Vy'=`v'[_N] if _n==`i'
        scalar drop l
        drop `u' `v'
        }
  local i=`i'+1
}
   tempvar np nn sum1 sum2 sum3
   gen `sum1'=`Ux'+`Vx' if `Ux'~=.
   replace `sum1'=sum(`sum1')
   gen `sum2'=`Vx'*(`Vx'-1)+`Ux'*(`Ux'-1)-2*`Ux'*`Vx' if `Ux'~=.
   replace `sum2'=sum(`sum2')
   gen `sum3'=`Vy'*(`Vy'-1)+`Uy'*(`Uy'-1)-2*`Uy'*`Vy' if `Uy'~=.
   replace `sum3'=sum(`sum3')
   gen `np'=1 if `by'==1
   replace `np'=sum(`np')
   gen `nn'=1 if `by'==2
   replace `nn'=sum(`nn')
   scalar b=(`sum1'[_N])/(`np'[_N]*`nn'[_N])
   scalar bx=(`sum2'[_N])/(`nn'[_N]*(`nn'[_N]-1)*`np'[_N])
   scalar by=(`sum3'[_N])/(`np'[_N]*(`np'[_N]-1)*`nn'[_N])
   #delimit;
   scalar
    varauc=(1/(4*(`np'[_N]*`nn'[_N])))*(b+(`nn'[_N]-1)
    *bx+(`np'[_N]-1)*by -4*(`np'[_N]+`nn'[_N]-1)*((`auc'-.5)^2));
   #delimit cr
   * noisily di in gr "The Variance of the AUC=" in ye varauc
   scalar SE=sqrt(varauc)
   local shse: display %9.3f SE
   local shse = trim("`shse'")
   noisily di in gr "The Standard Error of the AUC=" in ye `shse'
   global Sroc_se$counter = SE
   drop `np' `nn' `sum1' `sum2' `sum3'
   }
end