*! version 1.0.0 TJS 31oct1997
program define labsumm
version 5.0
  local varlist "opt ex"
  local if "opt"
  local in "opt"
  local weight "aweight fweight opt"
  local options "Format"
  parse "`*'"
  parse "`varlist'", parse(" ,")

  tempname o xb sd min max

  if "`weight'" != "" { local wgt "[`weight'`exp']" }

* get max label length
  local lmax 8
  while "`1'" != "" {
     unabbrev `1'
     local lab "$S_1"
     local vrl : variable label `1'
     if "`vrl'" != "" { local lab "`vrl'" }
     local llab = length("`lab'")
     local lmax = max(`llab',`lmax')
     local lmax8 = `lmax' - 8
     macro shift
     }

  parse "`varlist'", parse(" ,")

  if "`1'" != "" {
     di in gr _dup(`lmax8') " " "Variable |   Obs      Mean" _c
     di in gr "  Std. Dev.      Min       Max"
     di in gr _dup(`lmax') "-" "-+--------------------------" _c
     di in gr "--------------------"
     }

  while "`1'" != "" {
* ...do stats
     qui summarize `1' `wgt' `if' `in'
     scalar `o' = _result(1)
     scalar `xb' = _result(3)
     scalar `sd' = sqrt(_result(4))
     scalar `min' = _result(5)
     scalar `max' = _result(6)

* ...do label
     unabbrev `1'
     local lab "$S_1"
     local vrl : variable label `1'
     if "`vrl'" != "" { local lab "`vrl'" }
     local sp = `lmax' - length("`lab'")

     if "`format'" != "" {local f : format `1'}
     else {local f "%8.0g"}
     local lfmt = length("`f'")
     local b = max(10 - int(real(substr("`f'",2,`lfmt'-2))),0)

* ...display results for each variable
     di in gr _skip(`sp') "`lab' |" _c
     di in ye %6.0f `o' _skip(`b') `f' `xb'  _skip(`b') `f' `sd'  _c
     di in ye           _skip(`b') `f' `min' _skip(`b') `f' `max'

     macro shift
     }

exit
end