Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st:"sum" showing varible labels


From   "Steichen, Thomas J." <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st:"sum" showing varible labels
Date   Fri, 25 Jul 2008 09:30:14 -0400

David Radwin suggested my -labsumm- program for this problem.
This came as a surprise to me as I had long forgotten the existence
Of this program (it was written in 1997). Nonetheless, the suggestion
prompted me to try it out... And that tryout suggested an enhancement
to deal more gracefully with long variable labels.

Harking back to the days where we embedded code into the list messages,
I include below the new code and help file (though it remains a Stata
Version 5 program). Be sure to take care of any email-wrapped lines!

Tom

---- begin code -------------------------------------------------------
*  version 1.0.0 TJS 31oct1997
*  version 1.0.1 TJS 02jan2002
*! version 1.0.2 TJS 23jul2008
program define labsumm
version 5.0
  local varlist "opt ex"
  local if "opt"
  local in "opt"
  local weight "aweight fweight opt"
  local options "Format Maxlab(integer 0) Dropstr"
  parse "`*'"
  parse "`varlist'", parse(" ,")

  tempname o xb sd min max

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

* get max label length
  local lmax 9
  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 lmax9 = `lmax' - 9
     macro shift
  }

  if `maxlab' != 0 {
        if `maxlab' < 9 {
          local maxlab 9
        }
        local lmax9 = `maxlab' - 9
        local lmax  = `maxlab'
  }
  if "`maxlab'" == "0" {
        local maxlab `lmax'
  }

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

  if "`1'" != "" {
     di in gr _dup(`lmax9') " " " 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 `o' = _result(1)
     scalar `xb' = _result(3)
     scalar `sd' = sqrt(_result(4))
     scalar `min' = _result(5)
     scalar `max' = _result(6)

   if "`dropstr'" != "dropstr" | ("`dropstr'" == "dropstr" & `o' != 0) {

* ...do label
     unabbrev `1'
     local lab "$S_1"
     local vrl : variable label `1'
     if "`vrl'" != "" { local lab "`vrl'" }
     local ll = length("`lab'")
     local ls = 1
     local con
     local num 1
     local lmax2 = `lmax'
     if `ll' > `maxlab' {
       local lmax2 = `lmax' - 2
     }
     while `ls' <= `ll' {
       local l1 : piece `num' `lmax2' of `"`lab'"', nobreak
       local num = `num' + 1
       local ls = `ls' + length("`l1'") + 1
       local sp = `lmax' - length("`l1'") - 2
       local bar "> |"
       if `ls' > `ll' {
         local sp = `sp' + 2
         local bar "|"
         local con "_c"
       }
       di in gr _skip(`sp') "`l1' `bar'" `con'
     }

* ...set display format
     if "`format'" != "" {local f : format `1'}
     else {local f "%9.0g"}
     local sfmt = index("`f'","s")
     if `sfmt' {local f "%9.0g"}
     local lfmt = length("`f'")
     local cfmt = index("`f'", "c")
     if `cfmt' > 1 { local cfmt = 1 }
     local b = max(10 - int(real(substr("`f'",2,`lfmt' -2 -`cfmt'))),0)

* ...display results for each variable
     di in ye %8.0f `o' _skip(`b') `f' `xb'  _skip(`b') `f' `sd'  _c
     di in ye           _skip(`b') `f' `min' _skip(`b') `f' `max'

   } /* end if */

     macro shift

  } /* end while */

exit
end
---------end code------------------------------------------------------
---------begin help----------------------------------------------------
.-
help for ^labsumm^ (v1.0.2; a Stata version 5 program)
.-

Labeled summary statistics
--------------------------

   ^labsumm [^varlist^] [weight] [if^ exp^] [in^ range^] [, Format Maxlab Dropstr ]^

aweights and fweights are allowed; see ^help weights^.


Description
-----------

^labsumm^ calculates and displays a variety of univariate summary statistics
(see ^summarize^ for details).  If no varlist is specified, summary statistics
are calculated for all the variables in the data.  This is a variation of the
^summarize^ command where variable labels are displayed in place of variable
names and the ^detail^ option is not allowed.


Options
-------

^format^ requests that the summary statistics be displayed using the display
    format associated with the variables rather than the default g display
    format; see ^help format^.

^maxlab(#)^ sets the label column width at ^#^ and wraps longer labels onto
    additional lines. The statistics display with the last of such lines. The
    default is to use the width of the longest label, among variables in
    ^varlist^, as the label column width.

^dropstr^ requests that string variables not be displayed. The default is to
    include them in the display with a zero count and missing for the remaining
    statistics.

Author
------

T. J Steichen, steichen@@triad.rr.com

Examples
--------

        ^. labsumm^
        ^. labsumm mpg weight^
        ^. labsumm mpg weight if foreign, maxlab(20)^


Also see
--------

 Manual:  [R] summarize
On-line:  help for centile, cf, ci, codebook, compare, describe, egen, inspect,
                   lv, means, pctile, stsum, svymean, svytab, table, tabstat,
                   tabsum, xtsum
---------end help------------------------------------------------------

-----------------------------------
Thomas J. Steichen
[email protected]
-----------------------------------

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of David Radwin
Sent: Wednesday, July 23, 2008 11:51 AM
To: [email protected]
Subject: Re: st:"sum" showing varible labels

One more suggestion is Thomas Steichen's -labsumm-, available from ssc.

ssc install labsumm

David

>On Tue, Jul 22, 2008 at 6:14 PM, Man Jia <[email protected]> wrote:
>>
>>  Hi all,
>>
>>  Could anyone give me some suggestion of how to show variable labels in
>  > summarize command output ?


--
David Radwin // [email protected]
Office of Student Research, University of California, Berkeley
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

-----------------------------------------
CONFIDENTIALITY NOTE: This e-mail message, including any
attachment(s), contains information that may be confidential,
protected by the attorney-client or other legal privileges, and/or
proprietary non-public information. If you are not an intended
recipient of this message or an authorized assistant to an intended
recipient, please notify the sender by replying to this message and
then delete it from your system. Use, dissemination, distribution,
or reproduction of this message and/or any of its attachments (if
any) by unintended recipients is not authorized and may be
unlawful.   

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index