1
votes

I would like to create a 5-by-5 grid of tabulate twoway frequency count tables, like the following table.

enter image description here

Generating each sub-table is easy with nested foreach loops, but the long list output is more difficult to interpret than a 5-by-5 grid (and has redundant entries -- it provides both halves of the symmetric matrix).

Is it possible to make a table like this in Stata? To be clear, I can figure out the LaTeX later, I am just interested in getting clear and concise console output.

Thanks! Here is some code that does the basics using the auto data, but generates a list instead of a matrix. xtile is from the egenmore package

sysuse auto, clear
global vars price mpg headroom trunk weight
foreach x of global vars {
    egen d_`x' = xtile(`x'), nquantiles(2)
}

* can make diagonal entries
tabulate d_price d_price

* can make off-diagonal entries
tabulate d_price d_mpg

* crude solution that generates list output with redundant entries
foreach x of global vars {
    foreach y of global vars {
        tabulate d_`x' d_`y'
    }
}
1

1 Answers

3
votes

I've added some matrix operations into your loop.

tempname col all tabout
foreach x of global vars {
    foreach y of global vars {
        qui tabulate d_`x' d_`y', matcell(`tabout')
        mat colnames `tabout' = `x' `x'
        mat rownames `tabout' = `y' `y'
        mat `col' = (nullmat(`col') \ `tabout' )
    }
    mat `all'= (nullmat(`all') , `col')
    mat drop `col'
}
mat list `all'

It is a little similar to my program named meantab, at http://code.google.com/p/kk-adofiles/