Daniel R Sabath
> > I need to create a table which contains percent of row
> > total and frequency.
> > I've been looking through table and tabdisp and haven't
> > figured it out.
> >
> > The table I am looking for is very similar to the following. From
> > http://www.biomedcentral.com/1472-6963/3/4/table/T1
> > ------------------------------------------------------------
> > Characteristics of diabetic subjects according smoking
> > status. Figures are
> > frequencies (percent of row total).
> >
> > Never smoked (968) Current smoker (296)
> > Ex-smoker (582) All
> >
> >
> > Sex Men 374 (40) 166 (18)
> > 361 (39) 928
> > Women 594 (61) 130 (13)
> > 221 (23) 971
> > Age Group (Years)
> > < 45 117 (53) 60 (27)
> > 42 (19) 219
> > 45-54 126 (57) 49 (22)
> > 45 (20) 221
> > 55-64 281 (54) 78 (15)
> > 151 (29) 520
> > 65-74 263 (47) 78 (14)
> > 200 (36) 556
> > ? 75 158 (48) 25 (8)
> > 134 (41) 326
> > Not known 23 (40) 6 (11)
> > 10
> > (18) 57
I earlier presented a hack based on a wrapper
for -tabdisp-. This fell short of a right-hand column
giving row totals. For that, it seems cleaner
to use -list- as the output command.
The sequel program is -oct8- (although it isn't yet,
as of this longitude):
. program li oct8
oct8:
1. version 8
2. syntax varlist(max=2) [if] [in] [, *]
3. marksample touse, strok
4. tokenize `varlist'
5. args row col
6. tempvar freq percent show
7. qui {
8. bysort `touse' `row' `col' : gen `freq' = _N
9. by `touse' `row' : gen `percent' =
string(round(100 * `freq' / _N))
10. replace `percent' = "(" + `percent' + ")"
11. replace `percent' = " " + `percent' if
length(`percent') == 3
12. if "`col'" == "" gen `show' = `freq'
13. else gen `show' = string(`freq') + " " +
`percent'
14. if "`col'" == "" char `show'[varname] "Total"
15. else {
16. tempvar total
17. separate `show' if `touse', by(`col')
18. local show "`r(varlist)'"
19. levels `col' if `touse', local(levels)
20. local j = 1
21. foreach v of local show {
22. char `v'[varname] `"`: word `j++'
of `levels''"'
23. bysort `touse' `row' (`v') :
replace `v' = `v'[_N]
24. }
25. by `touse' `row': gen `total' = _N
26. char `total'[varname] "Total"
27. }
28. by `touse' `row': replace `touse' = `touse' * (_n
== 1)
29. }
30. list `row' `show' `total' if `touse', subvarname noobs
`options'
. oct8 foreign
+------------------+
| foreign Total |
|------------------|
| Domestic 52 |
| Foreign 22 |
+------------------+
. oct8 foreign rep78
+----------------------------------------------------------------+
| foreign 1 2 3 4 5 Total |
|----------------------------------------------------------------|
| Domestic 2 (4) 8 (17) 27 (56) 9 (19) 2 (4) 48 |
| Foreign 3 (14) 9 (43) 9 (43) 21 |
+----------------------------------------------------------------+
. oct8 foreign rep78, divider
+----------------------------------------------------------------+
| foreign | 1 | 2 | 3 | 4 | 5 | Total |
|----------+--------+--------+---------+--------+--------+-------|
| Domestic | 2 (4) | 8 (17) | 27 (56) | 9 (19) | 2 (4) | 48 |
| Foreign | | | 3 (14) | 9 (43) | 9 (43) | 21 |
+----------------------------------------------------------------+
. bysort himpg : oct8 foreign rep78, divider
______________________________________________________________________
_________
-> himpg = 0
+-----------------------------------------------------------------+
| foreign | 1 | 2 | 3 | 4 | 5 | Total |
|----------+--------+--------+---------+--------+---------+-------|
| Domestic | 1 (3) | 5 (16) | 20 (63) | 6 (19) | | 32 |
| Foreign | | | | | 4 (100) | 4 |
+-----------------------------------------------------------------+
______________________________________________________________________
_________
-> himpg = 1
+---------------------------------------------------------------+
| foreign | 1 | 2 | 3 | 4 | 5 | Total |
|----------+--------+--------+--------+--------+--------+-------|
| Domestic | 1 (6) | 3 (19) | 7 (44) | 3 (19) | 2 (13) | 16 |
| Foreign | | | 3 (18) | 9 (53) | 5 (29) | 17 |
+---------------------------------------------------------------+
The example is not as interesting as the method:
1. construct variables to be shown making heavy use of -by:-,
-_N-, -_n- etc.
2. use string variables for all special effects, e.g. composites
of variables within one cell.
3. use -list- as an output command.
Nick
[email protected]
*
* 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/