[Stata] How to Export Table 1 to Word or Excel files (table1_mc)
Creating descriptive statistics tables, often referred to as “Table 1” in research papers, is a fundamental task in statistical analysis. These tables typically present baseline characteristics of study participants, often comparing these characteristics across different groups. While Stata 17 officially offers dtable
command for creating such tables, it is not available for Stata versions under 17.
What is “Table 1”?
In research papers, particularly in clinical trials, epidemiology, and social sciences, “Table 1” typically presents baseline descriptive statistics for the study population. Its main purposes are:
- Describe the Sample: It gives readers a clear picture of who participated in the study (e.g., age, gender distribution, relevant characteristics).
- Assess Comparability (in comparative studies): If you have multiple groups (e.g., treatment vs. control, exposed vs. unexposed), Table 1 shows whether these groups were similar concerning key baseline characteristics before the intervention or exposure. Significant differences noted here might need to be adjusted for in later analyses.
- Evaluate Generalizability: It helps readers understand to whom the study results might apply.
A typical Table 1 includes:
- Means and standard deviations (or medians and IQRs) for continuous variables (like age, blood pressure).
- Counts and percentages for categorical variables (like gender, education level, disease status).
- Often, a column for the total population and separate columns for each comparison group.
- P-values from statistical tests comparing the groups.
table1_mc
command in Stata
There is a user-written package called table1_mc
. This package will drastically reduce the time you spend creating Table 1! You can read a detailed description of it at the link below.
table1_mc (available from the SSC archive) simplifies the creation of Table 1s. You first need to install it:
ssc install table1_mc
Below is the sample code I used. As mentioned in the guidelines above, be sure to choose the value label for each variable appropriately before running it. You can also use by(varname)
to create Table 1 by a specific variable (put the variable in the place of varname
), and the resulting t-test and chi-square test will be automatically included in the results. If you don’t put by(varname)
, only the Total column will be output.
table1_mc, ///
vars( ///
age contn %4.2f \ ///
gender cat %4.2f \ ///
education cat %4.2f \ ///
income contn %4.2f \ ///
) ///
nospace missing ///
saving("table 1.xlsx", replace)
Command options mean:
- vars(…): This is where you define the variables to include in your table rows.
- Each variable is listed on a new line (using /// or \ for line continuation).
contn
orcat
: You MUST specify the variable type.- contn: Continuous variable. table1_mc will calculate Mean (SD) by default.
- cat: Categorical variable. table1_mc will calculate N (%) by default.
- %fmt (e.g., %4.2f): Stata format controlling how the numbers are displayed (e.g., %4.2f means use 2 decimal places). This is optional but highly recommended for clean output.
- Options:
nospace
: Removes blank lines between variables for a more compact table.onecol
: For categorical variables, displays N (%) in a single column instead of separate columns for N and %.missing
: Reports the count of missing values for each variable.saving("table 1.xlsx", replace)
: This is incredibly useful! It directly exports the generated table to an Excel file named “table 1.xlsx”, replacing it if it already exists.
Group Comparison
This is where table1_mc truly shines. When you use the by(varname) option:
The command automatically performs the appropriate statistical test to compare the groups for each variable listed in vars().
The choice of test depends on the variable type (contn or cat) and the number of groups defined by by(varname):
- Continuous (contn):
- 2 groups: Independent samples t-test.
- >2 groups: One-way Analysis of Variance (ANOVA).
- Categorical (cat):
- Chi-squared test (χ²). (Note: For small cell counts, it might ideally use Fisher’s Exact Test, but table1_mc primarily relies on Chi-squared for automation. Always check assumptions if cell counts are very low).
- The resulting p-value is added to the table in the final column, instantly showing whether baseline differences between groups are statistically significant.
table1_mc, by(varname) ///
vars( ///
age contn %4.2f \ ///
gender cat %4.2f \ ///
education cat %4.2f \ ///
income contn %4.2f \ ///
) ///
nospace onecol missing total(before) ///
saving("table 1.xlsx", replace)
Command options mean:
- by(varname): This is the core option for group comparisons. Replace varname with the categorical variable defining your groups (e.g., treatment_group, exposure_status). table1_mc will create separate columns for each category within varname.
- total(before): Adds a column summarizing the entire sample (all groups combined). total(after) places it at the end. This is crucial for understanding the overall cohort.
By default, for categorical variables (cat), table1_mc calculates column percentages. This means that within each group defined by by(varname), the percentages add up to 100% (or close to it, allowing for multiple categories). This answers the question: “What proportion of Group A has characteristic X?”
Sometimes, however, you might want row percentages. This answers the question: “Of all the people with characteristic X, what proportion fall into Group A, Group B, etc.?” To get row percentages for categorical variables, you add the catrowperc
option.
table1_mc, by(varname) ///
vars( ///
age contn %4.2f \ ///
gender cat %4.2f \ ///
education cat %4.2f \ ///
income contn %4.2f \ ///
) ///
nospace onecol missing catrowperc total(before) ///
saving("table 1.xlsx", replace)
Using row percentages changes the interpretation of the percentages for categorical variables, so be sure which one is appropriate for your research question.
Here is a sample table that I extracted by using this command.
1 Response
[…] Next [Stata] How to Export Table 1 to Word or Excel files (table1_mc) […]