Add Data Tables to your Document - The Putdocx Command
The putdocx command was added in Stata v15 and expanded with new features in Stata v16. It allows you to create Microsoft Word documents from within Stata. The command allows you to embed Stata outputs or results as tables or paragraphs, embed Stata graphs as images, and create formatted paragraphs – all without ever having to open Microsoft Word.
In this section we look at how to create data tables using the putdocx command. There are three main types of tables that can be created and added to a word document, and these are: a table created from the data in memory (your active dataset); a table created from a Stata or Mata matrix; and a table created from the last estimation command, margins command or model (displayed by estimates table) you ran in Stata (eg -regress- is an estimation command).
How to Use:
Create a table from data in memory:
Create a table from a Stata or Mata matrix:
Create a table from an estimation command, margins command or model:
Adding row/column names, table title and table notes:
Specify table border formatting
Worked Example 1:
In this example I will generate a table using the dataset in Stata’s memory. In the command pane I type the following:
This generates a table of foreign cars, listing the price, mpg and repair record of each. There is a small title added to the top of the table stating this is a table of “Foreign cars.”, while the obsno option adds the observation number in the left-most column and the varnames option adds the variable names along the top row. You can see an image of the generated table below:
Worked Example 2:
In this example I perform a simple regression using the Stata auto example dataset, and I then add the regression output to a word document. In the command pane I type the following:
This outputs a table to the word document which I then save as example2.docx. The file is saved in my current working directory, which I have left as the default. An image of the generated table is below:
Worked Example 3:
In the previous example I added a regression table to a word document, however only one table was added. A second table containing statistics used to calculate the regression model was not added using the etable method. Even though this table is not saved, I can use the scalars saved when the regression model was run to re-create the table as a matrix. I can then add the matrix to a word document as a table.
The sum of squares (SS) are saved as “e(mss)” for the model and “e(rss)” for the residual. The degrees of freedom (df) are saved as “e(df_m)” for the model and “e(df_r)” for the residual. The mean squares (MS) is calculated by dividing SS by df. In the command pane I type the following:
The rownames and colnames options add the specified row and column names for the matrix to the generated table. An image of the generated table is below:
Once you save your document with the putdocx save command the document will be closed. If you do not want to save your document you can use -putdocx clear- to clear everything from the document. Please be aware that doing so without saving will cause you to lose everything you added to the document.