• Laura Whiting

The Putdocx Command - Add a Tabulate Table to Your Document

The putdocx command is a new addition in Stata 15 and updated for Stata 16, which 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 Putdocx Command blog post.


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 the tabulate command there are two options, one called matcell() which allows you to save the tabulate frequencies as a matrix, and one called matrow() which allows you to save the unique values of your variable as a matrix (the first column in tabulate’s output). Once saved as a matrix you can then add that matrix to a word document using the putdocx command.


How to Use:

For one-way tables:

For two-way tables:

Worked Example 1 - Oneway Tabulate:

For this example I am going to use the Stata example dataset auto.dta, and I am going to generate a one-way table of frequencies for the “rep78” variable. This variable has 5 repair record ratings, with 1 being the worst and 5 being the best. In the command pane I type the following:

This generates the following table saved to the MS Word document "oneway.docx" in my current working directory:


Worked Example 2 - Twoway Tabulate:

For this example I am going to generate a two-way frequency table of repair record (1-5) by country of manufacture (foreign or domestic) using the rep78 and foreign variables from the auto dataset. In the command pane I type the following:

This generates the following table in MS Word document "twoway.docx" saved in my current working directory:


Worked Example 3 - Percent and Cumulative Percent for Oneway Tabulate:

As you can see from example 1 the percent and the cumulative percent do not carry across when you use this matrix method. You can have these added to your table, but it requires a few extra steps which I will detail below.


In this section I make use of macros. If you do not know about Stata macros I suggest you read the help file, which you can access using the command help macro. I will not cover them here, except to say that when calling a macro you use the opening quote or grave (located on the tilde ~ key on your keyboard) followed by the macro name and then a normal apostrophe single-quote (on the double-quote “ key). If the code below does not work for you make sure you are calling the macros correctly.


I will demonstrate the steps necessary for a oneway tabulate complete with all columns. Please pay careful attention to the slashes used in this code. For matrices in Stata a backwards slash is used to append two matrices together, while a forward slash is for division. If you use the wrong slash your code will fail.


In the command pane I type the following:

This produces the following table in MS Word document "oneway2.docx":


Worked Example 4 - Total for Twoway Tabulate:

As you can see from example 2 the totals for a twoway table do not carry across when you use this matrix method. You can have the totals added to your table, but it requires a few extra steps which I will detail below.

This section also uses macros. Refer to Example 3 which explains how to use them. I will demonstrate the steps necessary for a twoway tabulate complete with all columns. Please pay careful attention to the slashes used in this code. For matrices in Stata a backwards slash is used to append two matrices together, while a forward slash is for division. If you use the wrong slash your code will fail.

In the command pane I type the following:

This gives the following table in MS Word document "twoway2.docx":


I have designed the code in this example to work with unknowns. It would be faster to simply specify everything for this table, but in the long term I have created a code I can apply to any tabulate I run and only have to change the variable names. If I turned this into a do-file with arguments I wouldn't need to change anything. The only change that may need to be made is if your secondary variable (foreign in this case) does not have value labels. If there are no value labels and just numbers, remove the line local valuelabel : value label foreign. Additionally, change the line within the foreach loop from local lab : label `valuelabel' `v' to just local lab = `v'.

While this may seem a bit involved, if you set it up as a do-file or personal ado-file you can run this whenever you like without having to constantly rewrite the code. If you would like this in an ado-file form let me know by emailing me at sales@surveydesign.com.au and I can create one for you.

51 views

© 2020 by Survey Design and Analysis Services. 

  • LinkedIn
  • Facebook
  • Twitter
  • YouTube