Statalist The Stata Listserver


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

RE: st: Displaying full variable names using "summarize"


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Displaying full variable names using "summarize"
Date   Wed, 5 Apr 2006 18:24:54 +0100

Clint gave a good answer. 

Another one is to write your own wrapper for -summarize-. 
Here is an example. 

. sysuse auto, clear
(1978 Automobile Data)

. wsummarize 

                  Obs      Mean        SD       Min       Max
price              69   6146.04   2912.44   3291.00  15906.00
mpg                69     21.29      5.87     12.00     41.00
rep78              69      3.41      0.99      1.00      5.00
headroom           69      3.00      0.85      1.50      5.00
trunk              69     13.93      4.34      5.00     23.00
weight             69   3032.03    792.85   1760.00   4840.00
length             69    188.29     22.75    142.00    233.00
turn               69     39.80      4.44     31.00     51.00
displacement       69    198.00     93.15     79.00    425.00
gear_ratio         69      3.00      0.46      2.19      3.89
foreign            69      0.30      0.46      0.00      1.00

. wsummarize, listwise

                  Obs      Mean        SD       Min       Max
price              74   6165.26   2949.50   3291.00  15906.00
mpg                74     21.30      5.79     12.00     41.00
rep78              69      3.41      0.99      1.00      5.00
headroom           74      2.99      0.85      1.50      5.00
trunk              74     13.76      4.28      5.00     23.00
weight             74   3019.46    777.19   1760.00   4840.00
length             74    187.93     22.27    142.00    233.00
turn               74     39.65      4.40     31.00     51.00
displacement       74    197.30     91.84     79.00    425.00
gear_ratio         74      3.01      0.46      2.19      3.89
foreign            74      0.30      0.46      0.00      1.00

. wsummarize, format(%6.0f  %8.2g) 

                  Obs     Mean       SD      Min      Max
price              69     6146     2912     3291    15906
mpg                69       21      5.9       12       41
rep78              69      3.4      .99        1        5
headroom           69        3      .85      1.5        5
trunk              69       14      4.3        5       23
weight             69     3032      793     1760     4840
length             69      188       23      142      233
turn               69       40      4.4       31       51
displacement       69      198       93       79      425
gear_ratio         69        3      .46      2.2      3.9
foreign            69       .3      .46        0        1

. wsummarize, format(%6.0f  %8.3g) 

                  Obs     Mean       SD      Min      Max
price              69     6146     2912     3291    15906
mpg                69     21.3     5.87       12       41
rep78              69     3.41      .99        1        5
headroom           69        3     .853      1.5        5
trunk              69     13.9     4.34        5       23
weight             69     3032      793     1760     4840
length             69      188     22.7      142      233
turn               69     39.8     4.44       31       51
displacement       69      198     93.1       79      425
gear_ratio         69        3     .463     2.19     3.89
foreign            69     .304     .464        0        1

. rename displacement averylongnameindeed 

. wsummarize, format(%6.0f  %8.3g) 

                         Obs     Mean       SD      Min      Max
price                     69     6146     2912     3291    15906
mpg                       69     21.3     5.87       12       41
rep78                     69     3.41      .99        1        5
headroom                  69        3     .853      1.5        5
trunk                     69     13.9     4.34        5       23
weight                    69     3032      793     1760     4840
length                    69      188     22.7      142      233
turn                      69     39.8     4.44       31       51
averylongnameindeed       69      198     93.1       79      425
gear_ratio                69        3     .463     2.19     3.89
foreign                   69     .304     .464        0        1

. rename averylongnameindeed averylongnameindeedandthen

. wsummarize, format(%6.0f  %8.3g) 

                                Obs     Mean       SD      Min      Max
price                            69     6146     2912     3291    15906
mpg                              69     21.3     5.87       12       41
rep78                            69     3.41      .99        1        5
headroom                         69        3     .853      1.5        5
trunk                            69     13.9     4.34        5       23
weight                           69     3032      793     1760     4840
length                           69      188     22.7      142      233
turn                             69     39.8     4.44       31       51
averylongnameindeedandthen       69      198     93.1       79      425
gear_ratio                       69        3     .463     2.19     3.89
foreign                          69     .304     .464        0        1


Here's the code: 

*! 1.0.0 NJC 5 April 2006 
program wsummarize, byable(recall) 
	version 8.2  
	syntax [varlist] [if] [in] [, Format(str) listwise ]
	
	if "`listwise'" != "" local listwise "novarlist" 
	marksample touse, strok `listwise'  
	qui count if `touse' 
	if r(N) == 0 error 2000 

	// variable name width  
	local w = 0 
	foreach v of local varlist { 
		cap confirm string variable `v' 
		if _rc { 
			local w = max(`w', length("`v'")) 
			local vlist `vlist' `v' 
		}	
	}	
	local ++w

	if "`format'" == "" local format %6.0f %9.2f %9.2f %9.2f %9.2f 
	else {
		if _caller() >= 9 { 
			foreach f in `format' { 
				confirm format `f' 
			}
		}	
		
		local nf : word count `format' 
		if `nf' < 5 { 
			local flast : word `nf' of `format' 
			forval j = `= `nf' + 1'/5 { 
				local format `format' `flast' 
			}
		}
	}
	
	tokenize "`format'" 
	local j = 1
	foreach f in `format' { 
		local s = 1 + real(substr("`f'", index("`f'", "%") + 1, 1))
		local s`j++' "%`s's"
	}

	di as txt _n "{space `w'} " `s1' "Obs" `s2' "Mean" ///
		`s3' "SD" `s4' "Min" `s5' "Max" 
	
	foreach v of local vlist { 
		qui su `v' if `touse' 
		di as txt "`v'{col `w'}  "    ///
		" " as res `1' r(N)           /// 
		" " as res `2' r(mean)        /// 
		" " as res `3' r(sd)          /// 
		" " as res `4' r(min)         /// 
		" " as res `5' r(max)          
	}
end 

Nick 
n.j.cox@durham.ac.uk 

clinton.thompson@summitllc.us replied to Yvonne Capstick 

> you may be able to use an alternative command, -tabstat-, w/ 
> the varwidth
> & col(stat) options...
> for example,
> 
> tabstat varnames, stat(n mean sd min max) col(stat) varwidth(#)
> 
> type -findit tabstat- for more detail.

> > If I have long variable names, the names are truncated when 
> I use the
> > "summarize" command. Does anyone know how to change the 
> settings so that I
> > can see the names in full?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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