Don't be too afraid of a little work
with -generate-.
. syuse auto, clear
We need another "categorical variable".
. gen himpg = mpg > 20
Dan needs column totals as his denominators
. bysort rep78 : gen count = _N
and cell frequencies as his numerators
. bysort himpg foreign rep78 : gen freq = _N
so the percents are then
. gen pc = 100 * freq / count
and the table is then achievable from
. tabdisp foreign rep78, c(pc) by(himpg)
----------------------------------------------------------------------
himpg and | Repair Record 1978
Car type | 1 2 3 4 5 .
----------+-----------------------------------------------------------
0 |
Domestic | 50 62.5 66.66666 33.33333 20
Foreign | 36.36364 20
----------+-----------------------------------------------------------
1 |
Domestic | 50 37.5 23.33333 16.66667 18.18182 60
Foreign | 10 50 45.45454
----------------------------------------------------------------------
-tabdisp- is often billed as a programmer's command, but that's
not to say you can't use it interactively. There is more on this
theme in
Speaking Stata: Problems with tables, Part I.
Stata Journal 3(3):309--324 (2003)
Nick
[email protected]
Daniel Egan
> I feel like maybe I am just overlooking something very simple,
> especially considering the ongoing conversation of tabstat etc, but I
> haven't been able to figure out how to do this myself yet...
>
> I want a table with a supercolumn with 5 cell entries corresponding to
> the percentage of observations by column.
>
> for example
>
> table inum ynum, by(fnum) row
>
> yields.
>
> y1 y2 y3 y4 y5
> ---------------------------------------------------
> f1 i | 11i 12i 13i 14i 15i
> ii | 11ii 12ii 13ii 14ii 15ii
> ----------------------------------------------------
> f2 i | 21i 22i 23i 24i 25i
> ii | 21ii 22ii 23ii 24ii 25ii
> ------------------------------------------------------
> Total T1n T2n T3n T3n T3n
>
>
>
> I want:
>
>
> | y1 | y2 | y3 | y4
> | y5
> --------------------------------------------------------------
> ------------
> f1 i | 11i/T1n | 12i/T2n | 13i/T3n | 14i/T4n | 15i/T5n
> ii | 11ii/T1n | 12ii/T2n | 13ii/T3n | 14ii/T4n
> | 15ii/T5n
> --------------------------------------------------------------
> --------------
> f2 i | 21i/T1n | 22i/T2n | 23i/T3n | 24i/T4n | 25i/T5n
> ii | 21ii/T1n | 22ii/T2n | 23ii/T3n | 24ii/T4n
> | 25ii/T5n
> --------------------------------------------------------------
> ------------------------------
> Total | T1n | T2n | T3n | T3n
> | T3n
>
>
> Note: Txx vary considerably, and my IJix are not indicators.
>
> Is there anyway to do this without creating new variables? Feel free
> to berate me if I am being slow.
*
* 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/