### Sunday, February 26, 2006

## st: RE: a question about making a tabel using STATA

I can't speak for STATA. The program I know is called Stata. Please read the Statalist FAQ before posting to be familiar with the advice given.

Specific answers below.

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

Jian Zhang > Dear STATA users, > > I have a question about making a table using STATA. I > couldn't figure out > how to make it. Hope that you could help me. > > The table that I need has the following format: > > group1 group2 group3 > variabel 1 > variabel 2 > variabel 3 > > The celles are filled with variable means for each group and for each > varialbe. For example, the first cell (row 1 and column 1) is > the mean of variable 1 for group 1. > > I know that I can use command table to make the following table: > > table group, by(mean variable1 mean variable2 mean variable3) > > variable1 variable2 variable3 > group1 > group2 > group3 > > > Since I have so many variables and groups, it would take too > much time to > convert the format of the secon table to that of the first table > manually. I wonder if there is a shortcut way in which I > could directly > generate the first table?

I can't find anything to do precisely this. Here's a quick and dirty program to do it. It's not especially smart about row or column labels. Watch for wrapped lines in copying.

*! NJC 1.0.0 26 Feb 2006 program meantable version 8.2 syntax varlist(numeric) [if] [in] [, by(varname) format(str) stat(str) ]

quietly { marksample touse if "`by'" != "" markout `touse' `by', strok count if `touse' if r(N) == 0 error 2000

if "`by'" != "" { levels `by' if `touse', clean local(levels) local levels : subinstr local levels "." " ", all } else { tempvar by gen byte `by' = 1 local levels "all" }

local nstat : word count `stat' if `nstat' >= 2 { di as err "single statistics only in stat()" exit 198 } tempvar group egen `group' = group(`by') if `touse' su `group', meanonly local c = r(max) numlist "1/`c'" local safelevels "`r(numlist)'" local r : word count `varlist' tokenize `varlist' tempname tmatrix mat `tmatrix' = J(`r', `c', .) if "`stat'" == "" local stat "mean" if inlist("`stat'", "N", "sum_w", "sum", "mean", "min", "max") { local opt "meanonly" } else if inlist("`stat'", "skewness", "kurtosis") { local opt "detail" } else if inlist("`stat'", "p1", "p5", "p10", "p25", "p50", "p75", "p90", "p95", "p99") { local opt "detail" }

forval j = 1/`c' { forval i = 1/`r' { su ``i'' if `group' == `j', `opt' matrix `tmatrix'[`i', `j'] = r(`stat') } }

matrix rownames `tmatrix' = `varlist' capture matrix colnames `tmatrix' = `levels' if _rc matrix colnames `tmatrix' = `safelevels' if "`format'" == "" local format "%4.3f" } mat li `tmatrix', format(`format') noheader end . sysuse auto, clear (1978 Automobile Data)

. meantable mpg

all mpg 21.297

. meantable mpg, by(rep78)

1 2 3 4 5 mpg 21.000 19.125 19.433 21.667 27.364

. meantable mpg turn trunk , by(rep78)

1 2 3 4 5 mpg 21.000 19.125 19.433 21.667 27.364 turn 41.000 43.375 41.067 38.500 35.636 trunk 8.500 14.625 15.267 13.500 11.455

. meantable mpg turn trunk , by(rep78) format(%2.1f)

1 2 3 4 5 mpg 21.0 19.1 19.4 21.7 27.4 turn 41.0 43.4 41.1 38.5 35.6 trunk 8.5 14.6 15.3 13.5 11.5

. meantable mpg turn trunk , by(rep78) format(%4.3f) stat(skewness)

. meantable mpg turn trunk , by(rep78) format(%4.1f) stat(p50)

1 2 3 4 5 mpg 21.0 18.0 19.0 22.5 30.0 turn 41.0 43.5 42.0 37.0 36.0 trunk 8.5 16.0 16.0 13.5 11.0 > A related question is that: is there an option in command table or in > other commands that we could automatically indicate the statistical > signifance of the difference between groups for same variables on the > tables? I looked through the help file for command table and > did not find > such options. Do I need to use command ttest for each group > and for each > variable and then manually indicate the statistical > significance on the table?

Occasionally there are options that are not documented. In the case of -table- no such option exists. I don't know how a set of results for all possible pairwise t tests would be compatible with the table structure you seek and in any case it would be very poor statistical practice, especially as you seem most interested in getting at the significance level. You need to read up on problems of multiplicity in hypothesis testing.

* * 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/

Tag: statalist