Updated: Aug 10, 2021
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.
To add simple summary tables for commands such as summarize, describe or tabulate we use Stata macros and returned results. 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 a couple of 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. Since this will not work for strings, you need to get a variable list that does not contain string variables. You can use the ds command to do this. Second, 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: