Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: Save results from flexible frequency table in 'matrix like' format

From   Milena Przheska <>
Subject   Re: st: RE: Save results from flexible frequency table in 'matrix like' format
Date   Thu, 18 Jul 2013 11:45:52 +0200

Hi Red,

Thank you very much for this algorithm.

I am quite new to Stata and not yet very familiar with Mata, so I was
wondering how should your code be extended to include more than one
variable in order to create multidimensional frequency table, because
the way I understand it, it creates vector, not a matrix.

Once again, thank you for your response, I appreciate it.

Milena Przheska

On Wed, Jul 17, 2013 at 8:56 PM, Red Owl <> wrote:
> The example below may not be the most efficient way to create a vector of frequencies, but I believe it works.
> sysuse auto, clear
> levelsof mpg, local(mpglevels)
> local lvlsnum = wordcount("`mpglevels'")
> matrix FREQMAT = J(`lvlsnum',1,.)
> local lvlscount = 0
> foreach lvl of local mpglevels {
>    local lvlscount = `lvlscount' + 1
>    quietly tabstat mpg if mpg == `lvl', stat(count) save
>    mat A = r(StatTotal)
>    mat FREQMAT[`lvlscount',1] = A[1,1]
>    }
> mat colnames FREQMAT = Frequency
> mat rownames FREQMAT = `mpglevels'
> matlist FREQMAT
> Red Owl
> (Yes, that's my real name)
>>> Dear members of Statalist,
>>> I have not been able to find appropriate way to save and export
>>> results from flexible frequency tables, so I kindly ask for your help.
>>> I would like to save for later use, a frequency table with multiple
>>> categorical variables, just like it appears in the result - like a
>>> matrix. Also, I need to keep the names of the variables to mark rows
>>> and columns.
>>> The dataset consists of many observations and mainly categorical
>>> variables (with some of them having tens of categories).
>>> I have tried the following ways to save the result after creating
>>> flexible frequency table:
>>> Copying and pasting the table into word is not good because the
>>> resulting table is very large;
>>> Replacing the original data set with the frequencies does not give me
>>> the matrix like format of the table;
>>> Collapsing the data after frequency also does not work well, because
>>> some of the variables are categorical and need to remain so, while
>>> frequencies are summed;
>>> The -matcell- option only worked with two-way frequency tables;
>>> -return- and -ereturn- did not contain any matrices.
>>> I would very much appreciate your suggestions and I thank you in advance.
>>> Regards,
>>> Milena Przheska
> *
> *   For searches and help try:
> *
> *
> *
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index