The Putdocx Command – Adding a summarize Table
The putdocx command was added in Stata 15, and expanded with new features in Stata 16. This command allows you to create a word document entirely from within Stata. The command includes options for adding a table to the word document. There are three methods for adding a table, either using the data currently loaded in memory, a matrix or an estimation result. For more information on how to add tables using these methods check out this Tech Tip.
Unfortunately the putdocx command doesn’t include an option to add simple summary tables generated using commands such as summarize, describe or tabulate. However, for each of these commands there is a way to add the information to a word document using putdocx. In this post we are going to show you how to add output from the summarize command, using another command called tabstat. The tabstat command has an option to save, and using this option will save the output to a matrix. The table can then be added as a matrix using the putdocx command.
There are several extra steps involved, beyond the putdocx commands, to be able to successfully add a summarize table using putdocx with tabstat. This is due to some differences in how the tabstat command handles both variables and presentation. First, the command requires that you specify which variables you want to display statistics for. You can use the “*” character to select all variables, however if you have any string variables present in your dataset the command will fail.
The easiest way to fix this problem is to simply drop all string variables from your dataset. You can use the preserve and restore commands to bring back your string variables once you’ve created your tabstat table. Also, the table generated by tabstat puts variables as columns and statistics as rows, the opposite of what you get with the summarize command. If keeping the summarize presentation is important to you, then you can easily swap columns and rows around by transposing the matrix.
How to Use:
In this example I create a putdocx table using the Stata auto example dataset. The table should look the same as a table generated with summarize, however the “Obs” column will be titled “N” which is Stata’s internal scalar for total number of observations. In the command pane, I type the following:
This generates the following table, saved in tabstat.docx:
Which is a good representation of summarize, as shown below: