Title

Comparing a set of risk factors or effects with respect to a
categorical variable - basetable

Summary

The command basetable is a simple yet highly efficient tool for
interactively building the first table required in most medical/epidemiological
papers.

The typical layout of these tables is a grouping (categorical) variable
as column header and then a set of rows of different variables being compared by
each group in the header and in a total.

When the interactive table building is over, the result can be inserted into
one or more sheets in a excel workbook.

If the labelling of variables and values have been done carefully the table
outputs in the excel workbooks will be almost publication ready.

The command basetable works from Stata 12 and onwards except for the option toxl.
In Stata version 12 use the option style(csv) and the modifier using to
specify a csv file to save the csv output in.

Syntax

basetable column_variable
[summary_variables] [if] [in] [using]
[, options]

Arguments

column_variable The first argument must be a (categorical)
variable for subgrouping or the text "_none" for no column variable.

summary_variables The rest of the arguments are either
variable names or variable lists followed by with suboptions inside
parentheses or headers.

* Headers (in square brackets) -
Headers are set by a text string in square brackets.
The syntax for the string inside the square brackets is:

title_text [#] [, local_if]

The different parts of the string are:

- The title_text is a plain text string.

- The hashtag (#) is an option for adding a sub-count.

- The local_if is a Stata if expression.

- The sub-count from the hashtag matches the sub-condition.

* Categorical variables are specified by their name followed by
an option string in brackets.
Possible option strings are:

- r or R (row percentages)

- c or C (column percentages)

- or label value (Show only the row for this value).
After a comma, a second argument can be added: r, R, c, C as above, or a ci
for a Wald confidence interval.

The p-value is by default from a chi-square test.
Fisher's exact test can be chosen instead by option exact.

* Continuous variables are specified by their name followed by
an option string in brackets.

An option string is a numeric format (eg %6.2f) and possibly, a local report
specification separated after a comma.

Types of report specifications are:

- sd (mean and sd, default),

- ci (mean and confidence interval),

- gci (geometric mean and confidence interval),

- pi (mean and prediction interval),

- iqr (median and interquartile range),

- iqi (median and interquartile interval),

- idr (median and interdecentile range),

- idi (median and interdecentile interval),

- imr (median and range), or

- imi (median, min, and max)

When the mean is reported the p-value is based on an ANOVA
test, and when the median is reported the p-value is based on a Kruskal
Wallis test.

Centile calculation are similar to centile,
not summarize.

options

log Show the underlying Stata output

nthousands Add thousands separator to n values

pctformat Alter the format used for the percentages for the
categorical summary variables. The argument must be a numeric format

pvformat Alter the format used for the P value.
The argument must be in a numeric format. There is sub-option top placing
the p-value at the top

exact(#) report Fisher's exact test instead of
chi-square tests.
The recommended value is 1 and 0 means no exact test.

If an error occurs, try with 5 and higher numbers. See tabulate.

continuousreport Specify overall default continuous report.
The values must be one of sd, iqr, iqi, idr, idi, imr, imi, ci, gci or pi

categoricalreport Specify overall default categorical report.
The values to set is n for count or p for percentages.
Default	is count (percentages).

notopcount Exclude the first row with count and percentages for
row columns

nototal Exclude the Total column.

nopvalue Exclude the P-value column.

columnorder Chose sets of column number (>1, <8) to show.

caption(string) Caption for the output. Same as the title option.

title(string) Title for the output. Same as the caption option.
The title option overwrites the caption option.

top(string) A string containing text prior to table content.
Default is dependent of the value of the style option

undertop(string) A string containing text between header and table
content. Default is dependent of the value of the style option

bottom(string) A string containing text after to table content.
Default is dependent of the value of the style option

missing Show missing report to the right of the table

small Specify the limit for being small wrt. hidesmall. Default is 5.

hidesmall Hide data when count values are less than small (default 5).

Note that the number less than "small" sometimes can be deduced from surrounding values

pseudo Option for using pseudo percentiles.
Pseudo percentiles are found by sorting the values and averaging a number of
values around each position. The averages are used to get percentiles.
Option small is used to set the average size

style The output can be shown in the formats: smcl,
csv, html, latex or tex, or md. The default is smcl

replace The styled output can be saved into a file specified by using.

If an existing file should be replaced in the process, replace should be set

version 13 and up
toxl(string) A string containing up to 5 values, separated
by a comma. The values are:

- path and filename on the excel book to save in. Excel book suffix is
set/reset to xls for Stata 13 and to xlsx for Stata 14 and above

- the sheet name to save output in

- (Optional) replace - replace/overwrite the content in the sheet

- (Optional) row, column numbers for the upper right corner of the table in
the sheet

- (Optional) columnn widths in parentheses. If more columns than widths the
last column width is used for the rest

todocx(string) A string containing one or two values separated
by a comma. The values are:

* path and filename on the excel book to save in.

* (Optional) replace - replace/overwrite the content in the sheet

Examples

Retrieve a sample dataset (required for the rest of the clickable commands below):

quietly basetable_example_data

Continuous presentation:

basetable low age(%6.2f) age(%6.2f,sd) age(%6.2f, ci)

Categorical presentation:

basetable low race(c) race(r) race(white) race(white,r) race(white,ci)

Adding a missing report:

basetable low race(c) age(%6.2f), missing

Adding sub report on young (age < 20) mothers:

basetable low race(c) age(%6.2f) [Young mothers #, if age < 20] race(c) age(%6.2f), missing

Save table as tex/latex in file tbl1.tex at current directory:

basetable low race(c) age(%6.2f) using tbl1.tex, style(tex)

To see current directory:

cd

To see file content:

type tbl1.tex

Save tables for "all" and "young mothers" in sheets all and young_mothers in
the Excel workbook tbls.xls(x) at current directory:

basetable low race(c) age(%6.2f), toxl(tbls, all)

basetable low race(c) age(%6.2f) if age < 20, toxl(tbls, young_mothers)

To see current directory:

cd

To see the Excel workbook (stata 13):

shell tbls.xls

To see the Excel workbook (stata 14 and up):

shell tbls.xlsx

Save tables for "all" and "young mothers" in sheets all at row 6 and
column 8:

basetable low race(c) age(%6.2f), toxl(tbls, all, replace,6,8)

To see the Excel workbook (stata 13):

shell tbls.xls

To see the Excel workbook (stata 14 and up):

shell tbls.xlsx

Save table for "all" the Word document tbl.docx at current directory:

basetable low race(c) age(%6.2f), todocx(tbl, all)

To see the Word document (One can not add several tables to the same Word file):

shell tbl.docx

Anonymize individuals by hiding small counts and pseudo percentiles:

basetable low age(%6.2f) ftv(c), hidesmall pseudo

Using group renaming to add prefix cat_ to all categorical variables:

rename (race ftv smoke) cat_=

Handling all renamed variables in the same way:

basetable low cat*(c), missing

Author and support

Author:

Niels Henrik Bruun,

Aalborg University Hospital

Support:

niels.henrik.bruun@gmail.com