Add Data Tables to your Document - The Putdocx Command

The putdocx command was added in Stata v15 and has been expanded with new features in subsequent versions. 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 four main types of tables that can be created and added to a word document, and these are: a table created using Stata’s new and improved table command and the suite of related collect commands; 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 (for example regress is an estimation command).

How to Use:

Add a table created with Stata's new table and collect commands, and/or the new Tables Builder (v17+ only):

putdocx collect

Create a table from data in memory:

putdocx table table_name = data(var1 var2 var3)

Create a table from a Stata or Mata matrix:

putdocx table table_name = matrix(matrix_name)

putdocx table table_name = mata(matrix_name)

Create a table from an estimation command, margins command or model:

putdocx table table_name = etable

Adding row/column names, table title and table notes:

putdocx table table_name = matrix(matrix_name), rownames colnames

putdocx table table_name = data(var1 var2 var3), obsno varnames

putdocx table table_name = mata(matrix_name), title(Title)

putdocx table table_name = etable, note(This is a note)

Specify table border formatting:

putdocx table table_name = matrix(matrix_name), border(all)

putdocx table table_name = etable, border(top) border(bottom)

putdocx table table_name = data(var1 var2 var3), border(start) border(end)

putdocx table table_name = mata(matrix_name), border(insideV) border(insideH)

putdocx table table_name = etable, border(top, nil)

NOTE: If you are adding a table built using the new table and collect commands or the Tables Builder, all editing can be done with the suite of collect commands prior to adding it to the document. You only need the putdocx formatting commands for tables not generated in this way.

Worked Example 1:

In this example I will generate a simple cross-tabulation using the new table command, and add it to a document using putdocx collect:

. sysuse auto, clear
(1978 automobile data)

. putdocx clear

. putdocx begin

. table rep78 foreign

------------------------------------------------
                   |          Car origin        
                   |  Domestic   Foreign   Total
-------------------+----------------------------
Repair record 1978 |                            
  1                |         2                 2
  2                |         8                 8
  3                |        27         3      30
  4                |         9         9      18
  5                |         2         9      11
  Total            |        48        21      69
------------------------------------------------

. collect style header foreign, title(hide)

. putdocx collect
(collection Table posted to putdocx)

. putdocx save C:\Users\Public\example1
successfully created "C:/Users/Public/example1.docx"

This produces a nice neat table that looks like this:

 DomesticForeignTotal
Repair record 1978
12 2
28 8
327330
49918
52911
Total482169

The putdocx collect command adds the table to the currently open document, and the putdocx save command saves that document containing the table so it can be opened and viewed.

Worked Example 2:

In this example I generate a table using the dataset in Stata’s memory:

. sysuse auto, clear
(1978 automobile data)

. putdocx clear

. putdocx begin

. putdocx table data = data(make price mpg rep78) if foreign==1, title(Foreign cars.) obsno varnames

. putdocx save C:\Users\Public\example2
successfully created "C:/Users/Public/example2.docx"

This generates a table of foreign cars from the dataset, listing the price, mpg and repair record of each. There is a small title added to the top of the table, 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 3:

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.

. sysuse auto, clear
(1978 automobile data)

. putdocx clear

. putdocx begin

. regress price rep78 mpg

      Source |       SS           df       MS      Number of obs   =        69
-------------+----------------------------------   F(2, 66)        =     11.06
       Model |   144754063         2  72377031.7   Prob > F        =    0.0001
    Residual |   432042896        66  6546104.48   R-squared       =    0.2510
-------------+----------------------------------   Adj R-squared   =    0.2283
       Total |   576796959        68  8482308.22   Root MSE        =    2558.5

------------------------------------------------------------------------------
       price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
       rep78 |   666.9568   342.3559     1.95   0.056     -16.5789    1350.492
         mpg |  -271.6425   57.77115    -4.70   0.000    -386.9864   -156.2987
       _cons |   9657.754    1346.54     7.17   0.000       6969.3    12346.21
------------------------------------------------------------------------------

. putdocx table reg = etable, note(This is a regression of price repair record and mpg)

. putdocx save C:\Users\Public\example3
successfully created "C:/Users/Public/example3.docx"

This outputs a table to the word document, similar to example 2. The table will look roughly the same as the third table in the regression output. An image of the generated table is below:

Worked Example 4:

In the previous example I added a regression table to a word document, however only one table was added. A second table containing the ANOVA statistics was not added using the etable method. Even though this table is not saved, I can use the returned results - saved when the regression model was run - to re-create the table as a matrix. This example then demonstrates how to add a matrix to your 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. The example follows.

. sysuse auto, clear
(1978 automobile data)

. putdocx clear

. putdocx begin

. quietly regress price rep78 mpg

. local ms = (e(mss)+e(rss))/(e(df_m)+e(df_r))

. local ms_m = e(mss)/e(df_m)

. local ms_r = e(rss)/e(df_r)

. matrix stats = (e(mss),e(df_m),`ms_m'\e(rss),e(df_r),`ms_r'\e(mss)+e(rss),e(df_m)+e(df_r),`ms')

. matrix colnames stats = SS df MS

. matrix rownames stats = Model Residual Total

. matrix list stats

stats[3,3]
                 SS         df         MS
   Model  1.448e+08          2   72377032
Residual  4.320e+08         66  6546104.5
   Total  5.768e+08         68  8482308.2

. putdocx table reg_stats = matrix(stats), rownames colnames

. putdocx save C:\Users\Public\example4
successfully created "C:/Users/Public/example4.docx"

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:

Worked Example 5:

In this final example I am going to create all three tables from a standard regression output using Stata’s new collect commands.

. sysuse auto, clear
(1978 automobile data)

. putdocx clear

. putdocx begin

  . collect p=Ftail(e(df_m),e(df_r),e(F)) ms=(e(mss)+e(rss)/e(df_m)+e(df_r)) mms=e(mss)/e(df_m) rms=e(rss)/e(df_r) df=e(df_m)+e(df_r) ss=e(mss)+e(rss): regress price mpg weight length

. collect layout (colname) (result[_r_b _r_se _r_z _r_p _r_ci])

Collection: Table
      Rows: colname
   Columns: result[_r_b _r_se _r_z _r_p _r_ci]
   Table 1: 4 x 5

-----------------------------------------------------------------------------------
              |  Coefficient   Std. error       t   p-value           95% CI       
--------------+--------------------------------------------------------------------
Mileage (mpg) |    -86.78928     83.94335   -1.03     0.305    -254.209    80.63046
Weight (lbs.) |     4.364798     1.167455    3.74     0.000    2.036383    6.693213
Length (in.)  |    -104.8682     39.72154   -2.64     0.010   -184.0903   -25.64607
Intercept     |     14542.43     5890.632    2.47     0.016     2793.94    26290.93
-----------------------------------------------------------------------------------

. collect style header colname, level(value)

. collect label levels result _r_ci "[__LEVEL__% conf. interval]", modify

. collect label levels result _r_p "P>|t|", modify

. collect label levels result _r_se "Std. err.", modify

. collect style cell cell_type[column-header], halign(center)

. putdocx collect
(collection Table posted to putdocx)


. collect addtags source[Model], fortags(result[df_m mss mms])
(3 items changed in collection Table)

. collect addtags source[Residual], fortags(result[df_r rss rms])
(3 items changed in collection Table)

. collect addtags source[Total], fortags(result[df ss ms])
(3 items changed in collection Table)

. collect addtags squares[SS], fortags(result[mss rss ss])
(3 items changed in collection Table)

. collect addtags squares[df], fortags(result[df_m df_r df])
(3 items changed in collection Table)

. collect addtags squares[MS], fortags(result[mms rms ms])
(3 items changed in collection Table)

. collect layout (source) (squares)

Collection: Table
      Rows: source
   Columns: squares
   Table 1: 4 x 3

--------------------------------------
           |           squares        
           |     SS      df      MS   
-----------+--------------------------
source     |                          
  Model    |  2.27e+08    3   7.57e+07
  Residual |  4.08e+08   70    5830114
  Total    |  6.35e+08   73   3.63e+08
--------------------------------------

. collect style cell, nformat(%11.0g)

. collect style cell cell_type[column-header], halign(center)

. putdocx collect
(collection Table posted to putdocx)


. collect layout (result[N F p r2 r2_a rmse])

Collection: Table
      Rows: result[N F p r2 r2_a rmse]
   Table 1: 6 x 1

------------------------------------
Number of observations |          74
F statistic            |  12.9761562
p                      |  7.8454e-07
R-squared              |  .357376442
Adjusted R-squared     |  .329835433
RMSE                   |  2414.56291
------------------------------------

. collect label levels result N "Number of obs", modify

. collect label levels result F "F(3, 70)", modify

. collect label levels result p "Prob > F", modify

. collect label levels result r2_a "Adj R-squared", modify

. collect label levels result rmse "Root MSE", modify

. collect style cell, nformat(%6.0g)

. collect style cell, minimum(.00001, label(`".0000"'))

. putdocx collect
(collection Table posted to putdocx)

. putdocx save C:\Users\Public\example5
successfully created "C:/Users/Public/example5.docx"

This produces the following tables in Word:


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.