[Stata] How to Export Table to Word (putdocx, asdoc, outreg2, and table1_mc)
In this post, I would love to introduce several ways to export the tables in STATA into a Word document.
1. Easiest and Simplist Way: Copy and Paste
The simplest method is to copy and paste into Excel. Instead of simply copying, right-click and click on “Copy Table” or press Ctrl+Shift+C to copy the table format, and then paste into Excel. The table will be pasted in a table format. Note that the whole table needs to be copied.
2. putdocx command
The putdocx
command is available for Stata version 15 and later. The following is an example of saving a table that extracts the mean of a specific item.
** install package **
net install http://www.stata.com/users/kcrow/tab2docx
putdocx begin // open docx file
tab2docx var1, summarize(var2)
putdocx save filename.docx, replace // The "replace" option is not necessary. It is an overwrite option in case you need to save multiple times.
putdocx clear // close docx file
You can save all tables/graphs, including regression tables and graphs, using the putdocx
command. For more information, refer to the Stata 15 putdocx
package manual.
Note that even if you don’t use the append command, you can still save by writing consecutive commands, as shown below, and it will be saved as an append at the bottom.
3. asdoc command
The following is the asdoc
command developed by a (very smart) professor. Personally, it is handier than putdocx
command so I use asdoc
command much more frequently. Detailed explanations can be found in the developer’s blog.
net install asdoc, from(http://fintechprofessor.com) replace // install package
With the asdoc
command, you can save most tables, such as summary statistics, correlations, regressions, frequency tables, t-tests, and more, in a pretty format!
Application: Pearson Correlation Table
asdoc pwcorr var1 var2 ..., star(all) nonum // star(all) replace nonum options allow for automatic *** labeling on significant correlation coefficients.
The output will be saved in a word document as follows.
Application: Cross-Tabulation Table
tab var1 var2, column dec(2)
// "column" is to save the percentage by column
// "dec(2)" is to save up to two decimal places.
In addition, one of the advantages of asdoc
is that you can easily save multiple tables at once through the append command. After the first command, you can continue to add append
, and it will be saved in one document. Note that the document cannot be saved if it is open, so be sure to close the document.
Application: Summary Statistics of continuous variables (mean / standard deviation / Sample Size / …)
In addition to mean, sd, and N, other statistics such as median, min, max, p1-p4, and t-stat can also be stored together.
asdoc tabstat var1, stat(mean sd N) by(var2) dec(2)
// The "stat(mean sd N)" option includes mean, standard deviation, and sample size.
// You can place the variable to be used for categorization in the "by(var2)" option.
// "dec(2)" means to show the values to two decimal places.
The results are stored as described above. The only drawback is that it seems there is a bug that the Total row is not displayed.
Note that the file name is automatically saved as Myfile.doc, but if you want to specify the file name, simply append “save(filename.doc)” to the end of the asdoc
command.
asdoc tab var1 var2, save(filenamehere.doc)
As a side note, the person who developed this package no longer updates it. He developed asdocx
a package for the advanced version with other useful features (e.g., exporting codebook, svy tabulate). The package is $15/year for subscription-based use. You can find the details here: https://fintechprofessor.com/asdocx/
4. outreg2 command
Specifically for the summary statistics, basic crosstabulations, and regression tables, you can use the user-developed command outreg2
. You can see the how-to guide for outreg2
in the following slide (I dislike reinventing the wheel).
https://www.princeton.edu/~otorres/Outreg2.pdf
The command is really simple. You can simply run the regression command and then run outreg2
after it. The document will be saved in the working directory.
ssc install outreg2 // install package
reg y x1 x2 x3 // run the regression first
outreg2 // saving regression table
The following is the option that I frequently use for the beautifully formatted regression tables: 2 decminal points for coefficients and standard errors and print label instead of variable name without constant.
outreg2 using table2a.xls, alpha(0.001, 0.01, 0.05) nocons dec(2) label
// saving regression tables with p-value levels and no constants
If you would love to export the tables with coefficients, p-value, and ci, then you can specify the stats
option. The following command returns the sideway table, suitable for the APA-style regression table with 2 decimals for all estimates and 3 decimals for p-values. It also includes the (unstandardized) coefficients, standard errors, and standardized beta coefficients.
outreg2 using results.xls, alpha(0.001, 0.01, 0.05) sideway stats(coef se pval ci beta) dec(2) pdec(3)
// you can change the stats option as per your need
If you would love to export odds ratio rather than coefficients for your logistic regressions, you can put the option eform cti(odds ratio)
as follows:
outreg2 using results.xls, alpha(0.001, 0.01, 0.05) eform cti(odds ratio) ci dec(2) pdec(3)
Here is the command to save the summary statistics and cross-tabulations.
outreg2, sum(log) keep(y x1 x2 x3) eqkeep(N mean)
// put the list of variables you want to create the summary statistics inside keep()
outreg2 y x1, replace cross // save the cross-tabulations with frequency and percentage
5. table1_mc command: Code for Table 1
Next 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.
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.
ssc install table1_mc
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)
Here is a sample table that I extracted by using this command.
asdocx offers a variety of tools to export tables created from tabulation of categorical variables. The tables can be exported to Word, Excel, LaTeX, or HTML. Here is a few links to explore:
tabmany – Table of multiple coded answers
mrtab – One- and two-way tables of multiple responses
tabcount – tabulates frequencies for up to 7 variables
tab3way – Three way table of frequencies and percentages
missings – Various utilities for managing missing values
tabulate, tab1, tab2
pctab – report percentages in cross-tabulations
crosstab – Tabulation with weighted means, percentages, etc.