*!Version 1.1
*!Data management utility: check for existence of variables in a dataset.
*!Authors: Amadou Bassirou DIALLO (World Bank) and Jean-Benoit Hardouin (Regional Health Observatory of Orléans)

program checkvars, rclass
version 8
syntax anything [if] [in] [, noList Tolerance(real 0) TAble noSUm GENMiss(namelist min=1 max=1)]

marksample touse
tempname rat
local av
local unav
local manymissings

quietly count if `touse'
local tot = r(N)

qui isvar `anything'
local badlist `r(badlist)'
local varlist `r(varlist)'

if "`table'"!="" {
   if "`badlist'"!="" {
      di _col(4)in green "Unavailable variables: " in ye "`badlist'"
      di
   }
   di _col(4) in green "{hline 29}"
   display _col(4) in gr "Existing" _col(15) in gr "Rate of"
   display _col(4) in gr "Variable"  _col(14) "missings" _col(24) "Available"
   di _col(4) in green "{hline 29}"
}

tokenize `varlist'
local nbvar:word count `varlist'

forvalues i=1/`nbvar' {
   capture assert missing(``i'')  if `touse'
      if !_rc {
          if "`table'"=="" {
              display in ye "``i''" in gr " is empty in the database." in ye " ``i''" in gr ///
              " is not added to the available list"
          }
          else {
              display _col(4) in gr "``i''" _col(14) "100.00%"
          }
      }
      else {
         if "`table'"=="" {
            display in ye "``i''" in gr " exists and is not empty."
         }
         *if "`available'"~= "" {
             quietly count if ``i'' >= . & `touse'
             local num = r(N)
             scalar `rat' = (`num'/`tot')*100
             if "`table'"=="" {
                display in ye "``i''" in gr " has " in ye r(N) in gr " missings."
                display in gr "Ratio number of missings of" in ye " ``i''" in gr ///
                " to total number of observations: " in ye %6.2f `rat' "%"
             }
             if `rat' <= `tolerance' {
                local av `av' ``i''
                if "`table'"=="" {
                   display in ye "``i''" in gr " is added to the available list."
                }
                else {
                   display _col(4) in gr "``i''" in ye _col(15) %6.2f `rat' "%" _col(32) "X"
                }
             }
             else {
                local manymissings `manymissings' ``i''
                if "`table'"=="" {
                   display in ye "``i''" in gr " has too many missings, compared to the tolerance level."
                   display in ye "``i''" in gr " is not added to the available list."
                }
                else {
                   display _col(4) in gr "``i''" _col(15) in ye %6.2f `rat' "%"
                }
             }
          *}
      }
   if "`table'"=="" {
      di
   }
}
if "`table'"!="" {
   di _col(4) in green "{hline 29}"
}

return local available `av'
return local unavailable `badlist'
return local manymissings `manymissings'

if "`av'" ~= ""&"`sum'"=="" {
  display _newline
  display in ye _col(14) "Unweighted summary statistics for available variables:" _n
  summarize `av'  if `touse'
}


if "`list'"== "" {
   display _newline
   display in ye _d(97) "_"
   display _newline
   if "`badlist'"~="" {
      display in gr "Unavailable variables: " in ye _col(45) "`badlist'" _n
   }
   if "`av'"~="" {
      display in gr "Available variables: " in ye _col(45) "`av'" _n
   }
   if "`manymissings'"~="" {
      display in gr "Available variables but with too missings: " in ye _col(45) "`manymissings'" _n
   }
   display in ye _d(97) "_"
}

if "`genmiss'" !="" {
   capture confirm variable `genmiss'
   if _rc!=0 {
      qui gen `genmiss'=0
      local nbav:word count `av'
      tokenize `av'
      forvalues i=1/`nbav' {
            qui replace `genmiss'=`genmiss'+1 if ``i''>=.
      }
   }
   else {
      di in green "The variable" in ye " `genmiss' " in green "already exists".
   }

}


end