*! version 1.0.5
* nproc calls individual sub-programs to save sensitivity/specificity
* for graphs, calculate auc, calculate se, calculate cov and
* chi2 + p-value.
* Statistical program by Phillip Price, graphs & interface by
* Fred Wolfe 12 Feb 1997
program define nproc
    version 4.0
    local varlist "req ex"
    local options "FIle(string) BY(string) pause noGraph *"
    parse "`*'"
    parse "`varlist'", parse(" ")
    if "`by'"==""{
      di in red "You must specify the Grouping variable"
      exit 198
    }
    if length("`file'") > 7{
      di in red "File is longer than 7 characters"
      exit 198
    }
    if `by' ~= int(`by'){
      di in red "Grouping variable is not integer"
      exit
    }

    if "`file'"==""{
      local file="nosave"
    }

    if "`file'" == "nosave" & "`graph'" ~= "nograph"{
      di in red "Default graph option requires file " _c
      di in red "(fname) for sensistivity/specificity data"
      exit 198
    }
    else {global rocfile = "`file'"  }


    display _newline "Nonparametric Receiver Operating Curves (ROC)"
    global counter = 0
    while "`1'" ~= ""{
      qui corr `1' `by'
        if _result(4) < 0{
      replace `1' = `1' * -1
      }

      global counter = $counter + 1
      global Sroc_v$counter `1'
      rocauc `1',by(`by') `pause'

      if "`graph'"!="nograph"{
        rocplot `1',by(`by') file(`file') `pause'
      }
       rocse `1',by(`by') `pause' auc(${Sroc$counter})

        if $counter > 1{
          #delimit ;
          roccov "$Sroc_v1" "${Sroc_v$counter}",
          by(`by') auc1($Sroc1) auc2(${Sroc$counter});
          local A =
          ($Sroc1 - ${Sroc$counter}) * ($Sroc1 - ${Sroc$counter});
          local S = ((($Sroc_se1)^2) +
          ((${Sroc_se$counter})^2)) -(2 * ${Scov1_$counter});
          #delimit cr
          global Sroc_T$counter = `A' *(1/`S')
          local shchi2: display %9.2f ${Sroc_T$counter}
          local shchi2 = trim("`shchi2'")
          local p = chiprob(1,${Sroc_T$counter})
          local shpval: display %9.3f `p'
          local shpval = trim("`shpval'")

          display  in green "Chisq statistic for the difference " _continue
          display in green "in AUCs = " in yellow `shchi2' ", p=" `shpval'
          global Sroc_p$counter = chiprob(1,${Sroc_T$counter})

        }
      mac shift
      }

      if "`graph'" ~="nograph"{
      savefile `by'
      }
      /* clear global macros */

      macro drop Sroc*
      macro drop Scov*
      macro drop rocfile
      macro drop Scounter
end

program define savefile
    version 4.0
    local varlist "req ex"
    local options
    parse "`*'"
    parse "`varlist'", parse(" ")
       qui{
      preserve
      clear
      set obs 2
      gen neutral = 1
      gen spec = 0
      replace neutral = 0 in 1
      replace spec = 1 in 1
      local num = 0
      while `num' < $counter{
        local num = `num' + 1
        append using ${rocfile}`num'
      }
      drop if spec ==.
      sort spec
      gen x = 1-spec
      gen str8 vname = ""
      gen str8 byvar = ""
      gen auc = .
      gen se = .
      gen chi2 = .
      gen pvalue = .
      local k = 0
      while `k' < $counter{
        local k = `k' + 1
        replace vname = "${Sroc_v`k'}" in `k'
        replace byvar = "`1'" in `k'
        replace auc = ${Sroc`k'} in `k'
        replace se = ${Sroc_se`k'} in `k'
        if `k' > 1{
          replace chi2 = ${Sroc_T`k'} in `k'
          replace pvalue = ${Sroc_p`k'} in `k'
        }

      }
      save $rocfile, replace


  /* erase intermediate rocfiles */
      local num = 0
      while `num' < $counter{
        local num = `num' + 1
      erase ${rocfile}`num'.dta
      }
      }

end