asdocx table1 : column percentage


The table1 template provides the convenience of reporting cell, row, or column percentages. The default is to report cell percentages. In this post, I am going to show several examples of reporting column percentages with categorical and binary variables.

1. Column percentages with categorical variables


To compare the results of asdocx’s table1 template with those reported by Stata, let us start with the tabulation of two variables in Stata. Stata’s tabulate command with the option col displays the relative frequency of each cell within its column in a two-way table. Let’s load the nhanes2b data from the web and create a two-way table of diabetes by race. The variable agegrp  is a categorical variable. The tabulate command automatically detects it and reports its categories in the first column of the table.

webuse nhanes2b, clear
tabulate agegrp diabetes ,  col

Here is how the table looks like.

Tabulation of agegrp diabetes
Race No diabetes Total
age20-29 2305 15 2320
23.401 3.006 22.418
age30-39 1600 21 1621
16.244 4.208 15.663
age40-49 1229 42 1271
12.477 8.417 12.281
age50-59 1219 72 1291
12.376 14.429 12.475
age60-69 2622 238 2860
26.619 47.695 27.636
age 70+ 875 111 986
8.883 22.244 9.527
Total 9850 499 10349
100 100 100

Table1 : column percentages for categorical variable


To create the above table in Table1 format, we need to change the variable order so that the treatment variable is written first.  Also, since we want to report various categories of the variable agegrp, therefore, we need to add the prefix i. to this variable. If we do not add this, asdocx will assume that we prefer to treat this variable as a continuous variable. asdocx command for Table1 template is:

asdocx tabulate diabetes i.agegrp ,  template(table1) col replace
Table 1: Demographics
Variables 0 (n=9850) 1 (n = 499) Total (10349) P-value
Age groups 1-6 0.00
age20-29
2305 (23.40%) 15 (3.01%) 2320 (22.42%)
age30-39
1600 (16.24%) 21 (4.21%) 1621 (15.66%)
age40-49
1229 (12.48%) 42 (8.42%) 1271 (12.28%)
age50-59
1219 (12.38%) 72 (14.43%) 1291 (12.47%)
age60-69
2622 (26.62%) 238 (47.70%) 2860 (27.64%)
age 70+
875 (8.88%) 111 (22.24%) 986 (9.53%)

 

2. Column percentages – binary variables


asdocx offers two options to report a binary variable in the table1 template. (1) report both of its categories. This is the default. asdocx identifies binary variables and report both of its categories. (2) report just the category that has a value of 1. This can be done by using option report(0) or report(1). In both cases, we can choose to report either column, row, or cell percentages.

The variable female is coded as zero and one, and hence is a binary variable. When using report(1) option, asdocx reports the statistics for the category that is coded as one. The col option reports the column percentages, that is the percentage of the female population with and without diabetes. If we want to report both the female and non-female population, we shall just drop the option report().

asdocx tabulate diabetes female ,  template(table1) report(1)  col
Table 1: Demographics
Variables Not diabetic (n=9850) Diabetic (n = 499) Total (10349) P-value
Female 5152 (52.30%) 282 (56.51%) 5434 (52.51%) 0.07

Removing option report() from the code, both categories of the binary variable female are reported.

asdocx tabulate diabetes female , template(table1) col
Table 1: Demographics
Variables Not diabetic (n=9850) Diabetic (n = 499) Total (10349) P-value
Female 0.07
Male
4698 (47.70%) 217 (43.49%) 4915 (47.49%)
Female
5152 (52.30%) 282 (56.51%) 5434 (52.51%)