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

Re: st: Teach an old dog new tricks

From   "Austin Nichols" <>
Subject   Re: st: Teach an old dog new tricks
Date   Thu, 8 May 2008 11:55:52 -0400

Sergiy Radyakin <>:
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'
sysuse auto, clear
table rep78 head, c(mean price)
tabmat price, by(rep78 head) mat(A)

On Thu, May 8, 2008 at 10:16 AM, Sergiy Radyakin <> 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,
>  Sergiy Radyakin
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index