# st: Re: Save -tabulate- output as matrix

 From Friedrich Huebler <[email protected]> To [email protected] Subject st: Re: Save -tabulate- output as matrix Date Wed, 13 Oct 2004 16:47:37 -0700 (PDT)

```Thank you for your suggestions, Nick. -contract- does not work with
my data because it only accepts frequency weights. I looked at
-groups- but I am afraid I don't have the skills to modify it.

Instead, I found a solution that involves -egen- and -tabstat- so
that -tabstatmat- can be used to create the matrix. I have to do some
additional work to normalize the weight so that the results are
identical to the -tabulate- output.

. sysuse auto
. sum weight
. gen normwgt = weight/r(mean)
. bysort foreign: egen wgtcount = sum(normwgt)
. egen wgtsum = sum(normwgt)
. gen wgtshare = 100*wgtcount/wgtsum
. tabstat wgtshare, by(foreign) stat(mean count) nototal save
. tabstatmat share
. tabstat wgtcount, by(foreign) stat(mean count) nototal save
. tabstatmat count
. matrix table = share, count
. matrix list table

table[2,4]
mean     N       mean     N
Domestic  77.197456    52  57.126118    52
Foreign  22.802542    22   16.87388    22

The matrix contains the same values as this table:

. tab foreign [aw=weight]

Car type |      Freq.     Percent        Cum.
---------+-----------------------------------
Domestic | 57.1261189       77.20       77.20
Foreign | 16.8738811       22.80      100.00

I have a separate question about saving a matrix with correct row
names and I will start a new thread for this.

Friedrich Huebler

--- Nick Cox <[email protected]> wrote:
> Various things come close. The easiest way to do this
> I can think of is
>
> 	preserve
> 	contract ... , freq(freq) percent(percent)
> 	mkmat freq percent, matrix(mymatrix)
> 	levels ..., local(levels)
> 	mat rownames mymatrix = `levels'
> 	restore
>
> You could hack at a clone of -groups- (SSC)
> to make it save matrices optionally.
>
> Friedrich Huebler
>
> > I am looking for a way to save -tabulate- output as a matrix,
> > with row names and preferably without the "Total" row.
> >
> > . sysuse auto
> > . tab foreign
> >
> > Car type |      Freq.     Percent        Cum.
> > ---------+-----------------------------------
> > Domestic |         52       70.27       70.27
> >  Foreign |         22       29.73      100.00
> > ---------+-----------------------------------
> >    Total |         74      100.00
> >
> > I would like to create a matrix that looks like this:
> >
> > tab[2,2]
> >           freq  percent
> > Domestic    52    70.27
> >  Foreign    22    29.73

_______________________________
Do you Yahoo!?