# Re: st: Teach an old dog new tricks

 From "Austin Nichols" To statalist@hsphsun2.harvard.edu Subject Re: st: Teach an old dog new tricks Date Thu, 8 May 2008 11:55:52 -0400

```Sergiy Radyakin <serjradyakin@gmail.com>:
Easier in Mata, perhaps, but still easy to put in an ado:
_______________________________________
prog tabmat
version 8.2
syntax varname [fw aw iw], by(varlist min=1 max=2) Matname(string) [replace]
cap conf matrix `matname'
if _rc!=111 & "`replace'"=="" {
di as err "Matrix `matname' already defined;" _n "specify"  _c
di as txt " replace " as err "option to overwrite"
error 110
}
cap matrix drop `matname'
tokenize `by'
tempvar iv jv
tempname v
egen long `iv'=group(`1')
su `iv', meanonly
loc maxi=r(max)
egen long `jv'=group(`2')
su `jv', meanonly
loc maxj=r(max)
forv j=1/`maxj' {
su `2' if `jv'==`j', meanonly
loc col: label (`2') `=r(mean)'
loc colc: subinstr local col "." "`=char(183)'", all
loc colnam `"`colnam' "`colc'""'
}
forv i=1/`maxi' {
su `1' if `iv'==`i', meanonly
loc row: label (`1') `=r(mean)'
loc rowc: subinstr local row "." "`=char(183)'", all
loc rownam `"`rownam' "`rowc'""'
forv j=1/`maxj' {
su `varlist' [`weight'`exp'] if `iv'==`i' & `jv'==`j', meanonly
if r(N)>0 loc m=r(mean)
else loc m=.
mat `v'=nullmat(`v') , `m'
}
mat `matname'=nullmat(`matname') \ `v'
mat drop `v'
}
matrix rownames `matname' = `rownam'
matrix colnames `matname' = `colnam'
matrix roweq `matname' = `1':
matrix coleq `matname' = `2':
mat li `matname'
end
_______________________________________
sysuse auto, clear

On Thu, May 8, 2008 at 10:16 AM, Sergiy Radyakin <serjradyakin@gmail.com> wrote:
> Hello Statalisters,
>
> I am looking for a way to get the matrix of means, which is displayed
> but not saved by, e.g.:
>
> sysuse auto
> table rep78 head, c(mean price)
>
> The (relatively new) svy commands allow this, but they are quite slow
> for the purpose. -table- was in Stata for ages, but it still does not
> save any matrix results.
>
> I am aware of an option replace, which saves the means in the dataset,
> but it destroys the data, - something I am trying to avoid. Adding
> preserve/restore will slow the program down sufficiently enough to
> reject this way of doing it.
>
> Is it possible to obtain a matrix of means from table (or another
> command with a similar functionality) (preferred) ? Or is it possible
> to tell table,replace to keep the existing data and add the computed
> statistics as new variables (good)?
>
> Note that tabstat does not allow what I am looking, since it only
> supports one var in the by() clause, and I will need to call it N
> times (where N is the number of categories in the second var) and
> combine the columns together, which is not fast either.
>
> Also note, that while
>
>   tabulate rep78 foreign, matcell(A)
>
> is a valid syntax,
>
>  tabulate rep78 foreign, matcell(A) summarize(price) mean
>
> which presumably could be the solution I am looking for, is not valid:
>  r(198); option matcell() not allowed
>
> Any suggestions are welcomed.
>
> Thank you,
> *
> *   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/
>
*
*   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/
```