st: RE: Frequencies and proportions side by side in tables

 From "Nick J. Cox" To Subject st: RE: Frequencies and proportions side by side in tables Date Fri, 23 Jan 2004 15:16:55 -0000

```There is some discussion of related
problems in the Speaking Stata columns
in Stata Journal 3(3) and 3(4) 2003,
and pointers to pertinent software, but
that did not extend to any canned solution
yielding precisely this.

An alternative route starts from Roger Newson's
-pcontract- and -xcontract- (Roger:
why are there are two commands?).
This example will give the flavour; the
commands offer a lot more flexibility.

The key, however, to "side-by-side" tabulations
is often that you need to create composite
_string_ variables with the cell contents
you want.

. sysuse auto
(1978 Automobile Data)

. pcontract for rep78, zero f(freq) p(percent)

. l

+-----------------------------------+
|  foreign   rep78   freq   percent |
|-----------------------------------|
1. | Domestic       1      2      2.70 |
2. | Domestic       2      8     10.81 |
3. | Domestic       3     27     36.49 |
4. | Domestic       4      9     12.16 |
5. | Domestic       5      2      2.70 |
|-----------------------------------|
6. | Domestic       .      4      5.41 |
7. |  Foreign       1      0      0.00 |
8. |  Foreign       2      0      0.00 |
9. |  Foreign       3      3      4.05 |
10. |  Foreign       4      9     12.16 |
|-----------------------------------|
11. |  Foreign       5      9     12.16 |
12. |  Foreign       .      1      1.35 |
+-----------------------------------+

. gen show = string(freq) + " (" + string(percent, "%3.2f") + ")"

. tabdisp for rep78, c(show)

------------------------------------------------------------------------
----------
|                           Repair Record 1978

Car type |          1           2           3           4           5
.
----------+-------------------------------------------------------------
----------
Domestic |   2 (2.70)   8 (10.81)  27 (36.49)   9 (12.16)    2 (2.70)
4 (5.41)
Foreign |   0 (0.00)    0 (0.00)    3 (4.05)   9 (12.16)   9 (12.16)
1 (1.35)
------------------------------------------------------------------------
----------

Nick
n.j.cox@durham.ac.uk

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of n p
> Sent: 23 January 2004 14:33
> To: statalist@hsphsun2.harvard.edu
> Subject: st: Frequencies and proportions side by side in tables
>
>
> Hi statalisters,
> Is there a way to show tables of frequencies and
> proportions (row or column) like in the tabulate
> command but with the proportions side by side with the
> frequencies?
>
> Something like (row proportions case):
>
>
>          Education Level
>          High      |  Low
> Gender   N (%)     |  N(%)
> -------------------------------
> Male     4 (40.0)  |  6  (60.0)
> Female   3 (15.0)  |  17 (85.0)
> --------------------------------
>
> I tried to implement it using something like
>
> preserve
> gen cons=1
> bysort gender:egen N=sum(cons)
> bysort gender education:egen n=sum(cons)
> bysort gender education:keep if _n==1
> gen perc=n/N*100
> .
> .
> reshape ....
> list ....
> restore
>
>
> but I had problems dealing with cells with zero
> frequencies. I also did a quick search (findit,
> archives) without success.
> The primary use of such a command would be
> copy-pasting in word processors.

*
*   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/
```