Statalist The Stata Listserver


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

Re: st: Table of summary statistics


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: Table of summary statistics
Date   Thu, 11 May 2006 16:49:42 -0500

On May 11, 2006, at 2:50 PM, Hugh Colaco wrote:
I have 15 variables (X1, X2, ........., X15) and I want to create a table with the mean, median and no of obs of the different variables. I want this for 3 different categories. See example below.
<snip>


If I understand correctly, the following simulates your setup:


. input str9 category

category
1. "Category1"
2. "Category2"
3. "Category3"
4. end

. expand 100
(297 observations created)

. set seed 123456789

. forv i = 1/15 {
2. qui gen X`i' = uniform() if uniform()<0.9
3. }

. ds
category X2 X4 X6 X8 X10 X12 X14
X1 X3 X5 X7 X9 X11 X13 X15


If this is the case, then it's pretty straightforward to get the table you want:


. gen obs = _n

. reshape long X, i(obs category) j(var)
<output omitted>

. gen variable = "X" + string(var,"%02.0f")

. table variable category, c(mean X median X count X) format("%4.2f")

-------------------------------------------
| category
variable | Category1 Category2 Category3
----------+--------------------------------
X01 | 0.50 0.49 0.53
| 0.54 0.47 0.57
| 91 91 91
|
X02 | 0.52 0.54 0.46
| 0.57 0.59 0.46
| 91 87 93
...


Note that the first line (-gen obs = _n-) is necessary because - reshape- needs a way to uniquely identify the observations. You can then tweak the two format strings, as desired. If you want to work a bit harder, you can even add the additional cell formatting (i.e., parentheses and brackets) you asked for:


. collapse (mean) mean=X (median) median=X (count) n=X, by(variable category)

. gen mean_fmt = string(mean,"%4.2f")

. gen med_fmt = "(" + string(median,"%4.2f") + ")"

. gen n_fmt = "[" + string(n) + "]"

. tabdisp variable category, cellvar(mean_fmt med_fmt n_fmt) cen

-------------------------------------------
| category
variable | Category1 Category2 Category3
----------+--------------------------------
X01 | 0.50 0.49 0.53
| (0.54) (0.47) (0.57)
| [91] [91] [91]
|
X02 | 0.52 0.54 0.46
| (0.57) (0.59) (0.46)
| [91] [87] [93]


-- Phil

*
* 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index