*! Version 1.1.3 <10Aug1999>                            [STB-51: sg67.1]
program define univar
*! Univariate summaries with boxplots, in horizontal display style
*! Syntax: . univar varlist(min=1 numeric) [if] [in] [aweight fweight]
*!                  [ ,  BOXplot BYvar(varlist) ONEScal ONEHdr
*!                       Dec(int 2) FMT(string) Lstwise SE VLabel ]
** Author:  John R. Gleason, Syracuse University, Syracuse NY, USA
**          (loesljrg@accucom.net)

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


   if "`1'" == "?" {
      which univar
      exit
   }

   version 6.0
   syntax varlist(min=1 numeric) [if] [in] [aweight fweight]   /*
         */  [ ,  BOXplot BYvar(varlist) ONEScal ONEHdr    /*
         */       Dec(int 2) FMT(string) Lstwise SE VLabel ]

   marksample use, novarlist
   if "`lstwise'" != "" { markout `use' `varlist' }
   qui count if `use'
   if !r(N) { error 2000 }

   if "`byvar'" != "" {
      preserve
      qui keep if `use'
      sort `byvar'
      local nbvar : word count `byvar'
      local nvar : word count `varlist'
      tempvar bz
      local n2 "int"
      if _N > 32766 { local n2 "long" }
      qui by `byvar': gen `n2' `bz' = _N
      local n2 0
      tempname BB
      mat `BB' = J(`nvar', 2 , 1)
   }
   else {
      local n2 = _N
      local ifuse `"if `use'"'
   }
   local SD "S.D."
   if "`se'" != "" {
      local SD "S.E."
      local se "/r(N)"
   }
   if "`fmt'" != "g" { local fmt "f" }
   local oft "%8.`dec'`fmt'"
   tokenize `varlist'
   tempname aa bb p1 p2
   local n1 1

   local noihdr "*"
   if "`onehdr'" != "" {
      local onehdr "*"
      local noihdr
   }
   `noihdr' x `clr' `ch1' `ch2' `ch3' `SD' `boxplot'

   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 `"`lbl'"' != "" { local tt : label `lbl' `tt' }
            noi di in `clr' `"`bv'=`tt'"', _con
         }
         noi di ""
      }
      `onehdr' x `clr' `ch1' `ch2' `ch3' `SD' `boxplot'

      local newl "qui"
      local vv 1
      while `"``vv''"' != "" {
         local here = 9 - length(`"``vv''"')
         local CS1 = ("`byvar'"!="") &    /*
               */    ("`boxplot'"!="") & ("`onescal'"!="")
         if `CS1' & (`n1'==1) {    /* overall scaling constants */
            qui summ ``vv'' [`weight'`exp'] `ifuse', d
            mat `BB'[`vv',1] = r(max) - r(min)
            mat `BB'[`vv',2] = r(min)
         }

         qui summ ``vv'' [`weight'`exp'] `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) /*
                  */    substr(`"`l1'"', 1, 69)
            }
            local newl "noi"
         }
         if ("`boxplot'" != "") & (r(max) > r(min)) {
            scalar `bb' = 68/(r(max)-r(min))
            scalar `aa' = 11 - `bb'*r(min)
            if `CS1' {
               scalar `bb' = 68/`BB'[`vv',1]
               scalar `aa' = 11 - `bb'*`BB'[`vv',2]
            }
            local LB = round(`bb'*r(min)+`aa', 1)
            local UB = round(`bb'*r(max)+`aa', 1)
            if `UB'-`LB' > 4 {
               scalar `p1' = round(`bb'*r(p25)+`aa',1)
               scalar `p2' = round(`bb'*r(p50)+`aa',1)
               local l1 = `p1' - `LB'
               local l2 = `p2' - `p1'
               noi di in gr _col(`here') "``vv''" in `clr'        /*
                  */   _col(`LB') _dup(`l1') "`ch1'" _dup(`l2')   /*
                  */   "`ch2'" "`ch3'" _con
               scalar `p1' = round(`bb'*r(p75)+`aa',1)
               local l1 = `p1' - `p2'
               local l2 = `UB' - `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 r(N),  /*
            */    `oft' r(mean), `oft' sqrt(r(Var)`se'), `oft'  /*
            */    r(min), `oft' r(p25), `oft' r(p50), `oft'     /*
            */    r(p75), `oft' r(max)
         local vv = `vv' + 1
      }
      `onehdr' noi di in gr _dup(79) "-"
      local n1 = `n2' + 1
   }
   `noihdr' noi di in gr _dup(79) "-"
end


program define x
   if "`6'" != "" {
      noi di in `1' _col(41) _dup(10) "`2'" _dup(9) "`3'" /*
         */  "`4'" _dup(9) "`3'" _dup(10) "`2'"
   }
   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)   /*
      */    "`5'" _sk(6) "Min" _sk(6) ".25" _sk(6) "Mdn"    /*
      */    _sk(6) ".75" _sk(6) "Max" _new _dup(79) "-"
end