# Re: st: creating a matrix of summary stats

 From "Austin Nichols" To statalist@hsphsun2.harvard.edu Subject Re: st: creating a matrix of summary stats Date Tue, 26 Sep 2006 17:40:31 -0400

```Scott--
What I do in these situations is write a nested loop and collect the
relevant numbers in locals, and use a -file- command to write to a
text file. Like this:

cap file close o
file open o using /out.txt, write replace
forv y=1997/2002 {
file write o _tab "`y'"
}
foreach v of varlist vbl1 vbl2 {
forv y=1997/2002 {
su `v' [aw=wvar] if year==`y'
local m`y'=r(mean)
local sd`y'=r(sd)
local n`y'=r(N)
}
file write o _n "`: var lab `v''" _tab
foreach stat in m sd n {
forv y=1997/2002 {
file write o _tab "``stat'`y''"
}
}
file write o _n _tab
}
file close _all
type /out.txt

Note: The formula used by summarize with aweights for what it labels
"Std. Dev." is the correct formula for estimating the population
standard deviation with pweighted data.
http://www.stata.com/support/faqs/stat/supweight.html

On 9/26/06, Scott Cunningham <scunning@gmail.com> wrote:
```
```I am using data on individuals from the National Longitudinal Survey
of Youth 1997, and would like to create a matrix of summary stats
wherein the column identifies the year of the survey, and the rows
are:  1) the variable's mean; 2) the variable's standard deviation;
3) the number of observations.  I believe that I can use -reshape- in
combination with -egen- to create means for each of these, followed
by dropping all duplications of the mean so that I have a single mean
for each variable by year.  I could also, in principle, use this same
thing to calculate the std deviation and the number of observations.
But I was wondering if there existed a more efficient way of doing
this in people's minds?  For instance, the matrix (or ultiamtely, a
TeX file, as that's the final product I am making) would look like this:

1997    1998    ...     2002
vbl1    4.5             5.5                     6.5
(2.0)   (1.3)           (2.3)
200             400                     600
vbl2    ...

And so on.

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