program define univar
*! version 1.1.2
** Author:  John R. Gleason, Syracuse University, Syracuse NY, USA
**          (loesljrg@ican.net)
** 01Nov97:  Added vlabel option; minor fix to value label display

   qui version
   local vers = _result(1)
   version 4.0
   if `vers' >= 5 { version 5.0 }  /* to display value labels */

   local ch1 "-"     /* whisker character */
   local ch2 ":"     /* box character     */
   local ch3 "|"     /* median character  */
   local clr "blue"  /* box-whisker color */

   local varlist "req ex min(1)"
   local if "opt"
   local in "opt"
   local weight "aweight fweight"
   #delimit ;
   local options "BOXplot BYvar(string) Dec(int 2) FMT(string)
      Lstwise SE VLabel";
   #delimit cr

   parse "`*'"
   parse "`varlist'", parse(" ")
   local weight "[`weight'`exp']"
   tempvar use
   quietly {
      mark `use' `if' `in'
      if "`lstwise'" != "" { markout `use' `varlist' }
      count if `use'
      if !_result(1) { error 2000 }
   }
   if "`byvar'" != "" {
      uv `byvar'
      local byvar "$S_1"
      preserve
      qui keep if `use'
      sort `byvar'
      local nbvar : word count `byvar'
      tempvar bz
      local n2 "int"
      if _N > 32766 { local n2 "long" }
      qui by `byvar': gen `n2' `bz' = _N
      local n2 0
   }
   else {
      local n2 = _N
      local ifuse "if `use'"
   }
   local SD "S.D."
   if "`se'" != "" {
      local SD "S.E."
      local se "/_result(1)"
   }
   if "`fmt'" != "g" { local fmt "f" }
   local oft "%8.`dec'`fmt'"

   tempname aa bb p1 p2
   local n1 1
   while `n1' <= _N {
      if "`byvar'" != "" {
         local n2 = `n2' + `bz'[`n1']
         noi di _new in `clr' "->", _con
         local vv 0
         while `vv' < `nbvar' {
            local vv = `vv' + 1
            local bv : word `vv' of `byvar'
            local tt = string(`bv'[`n1'])
            local lbl : value label `bv'
            if `vers' >= 5 & "`lbl'" != "" {
               local tt : label `lbl' `tt'
            }
            noi di in `clr' "`bv'=`tt'", _con
         }
         noi di ""
      }
      if "`boxplot'" != "" {
         noi di in `clr' _col(41) _dup(10) "`ch1'" _dup(9) "`ch2'"  /*
            */  "`ch3'" _dup(9) "`ch2'" _dup(10) "`ch1'"
      }
      else {
         noi di in gr _col(41) _dup(14) "-" " Quantiles " _dup(14) "-"
      }
      noi di in gr "Variable" _sk(7) "n" _sk(5) "Mean" _sk(5)       /*
         */  "`SD'" _sk(6) "Min" _sk(6) ".25" _sk(6) "Mdn" _sk(6)   /*
         */  ".75" _sk(6) "Max" _new _dup(79) "-"
      local newl "qui"
      local vv 1
      while "``vv''" != "" {
         local here = 9 - length("``vv''")
         qui summ ``vv'' `weight' `ifuse' in `n1'/`n2', d
         `newl' di ""
         if "`vlabel'" != "" {
            local l1 : var lab ``vv''
            if "`l1'" != "" {
               noi di in gr _col(`here') "``vv''" _col(11) "`l1'"
            }
            local newl "noi"
         }
         if "`boxplot'" != "" & (_result(6) > _result(5)) {
               scalar `bb' = 68/(_result(6)-_result(5))
               scalar `aa' = 11 - `bb'*_result(5)
               scalar `p1' = round(`bb'*_result(9)+`aa',1)
               scalar `p2' = round(`bb'*_result(10)+`aa',1)
               local l1 = `p1' - 11
               local l2 = `p2' - `p1'
               noi di in gr _col(`here') "``vv''" in `clr'    /*
                  */ _col(11) _dup(`l1') "`ch1'" _dup(`l2') "`ch2'" /*
                  */ "`ch3'" _con
               scalar `p1' = round(`bb'*_result(11)+`aa',1)
               local l1 = `p1' - `p2'
               local l2 = 79 - `p1'
               noi di in `clr' _dup(`l1') "`ch2'" _dup(`l2') "`ch1'"
               local newl "noi"
         }
         noi di in gr _col(`here') "``vv''", in ye %7.0f    /*
            */  _result(1), `oft' _result(3), `oft'         /*
            */  sqrt(_result(4)`se'), `oft' _result(5), `oft'       /*
            */  _result(9), `oft' _result(10), `oft' _result(11),   /*
            */  `oft' _result(6)
         local vv = `vv' + 1
      }
      noi di in gr _dup(79) "-"
      local n1 = `n2' + 1
   }
end


program define uv
   local varlist "req ex min(1)"
   parse "`*'"
   parse "`varlist'", parse(" ")
   global S_1
   while "`1'" != "" {
      global S_1 "$S_1 `1'"
      mac shift
   }
end