Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# RE: st: RE: rearrange table

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: RE: rearrange table Date Fri, 10 Feb 2012 12:24:56 +0000

```This is shorter than my original (quoted below) and the same length at 10 lines as my second solution.

I am sure that Phil and I would both be pleased to see shorter solutions.

The bitter-sweet principle is that there is a syntax to produce any table you want: it's just called Stata.

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

Phil Clayton

-statsby- is very nifty, but in this case you can of course get away with something a little simpler for constructing the results dataset:

sysuse auto, clear
collapse (mean) mean=mpg (sd) sd=mpg (count) n=mpg, by(foreign)
gen ub=mean + invttail((n-1), 0.025) * sd/sqrt(n)
gen lb=mean - invttail((n-1), 0.025) * sd/sqrt(n)

Followed by Nick's:
rename (sd mean lb  ub) (statsd statmean statlb statub)
reshape long stat, i(foreign) string
label def which 1 mean 2 sd 3 lb 4 ub
encode _j , label(which) gen(which)
label def which 3 "95% limit: lower" 4 "upper", modify
tabdisp which foreign, c(stat) format(%3.2f)

On 10/02/2012, at 10:21 PM, Nick Cox wrote:

> sysuse auto, clear
> statsby mean=r(mean) ub=r(ub) lb=r(lb), by(foreign) : ci mpg
> save bystat, replace
> sysuse auto, clear
> statsby sd=r(sd), by(foreign)  : su mpg
> merge 1:1 foreign using bystat
> drop _merge
> rename (sd mean lb  ub) (statsd statmean statlb statub)
> reshape long stat, i(foreign) string
> label def which 1 mean 2 sd 3 lb 4 ub
> encode _j , label(which) gen(which)
> label def which 3 "95% limit: lower" 4 "upper", modify
> tabdisp which foreign, c(stat) format(%3.2f)

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