Home  /  Products  /  Stata 15  /  Create Word documents with embedded Stata results

This page announced the new features in Stata 15. Please see our Stata 18 page for the new features in Stata 18.

Create Word documents with embedded Stata results

What's this about?

Stata users often need to create Word, PDF, or HTML files to report on what they have done. Stata 15 provides three new commands for doing that:

New CommandPurpose
putdocxCreate Word documents
putpdfCreate PDF files
dyndocCreate HTML from Markdown

Here we tell you about putdocx.

Creating Word documents with statistical graphs and results can be tedious. If your data or model changes, you must update your document. Doing this by hand is time consuming and error prone.

New command putdocx lets you create Word (.docx) files more easily. You can create documents with

  • formatted paragraphs
  • tables
  • embedded output from Stata in paragraphs and tables
  • embedded Stata graphs (or any PNG, JPEG, EMF, or TIFF files)

And you can control page size, page breaks, and document orientation, and you can even append .docx files.

Highlights

  • Write paragraphs, images, and tables to Word documents
  • Add custom formatting to paragraphs, text, tables, and table cells
  • Embed results generated by Stata into Word paragraphs and tables
  • Embed Stata graphs
  • Append .docx files

Show me

Let's create this Word (.docx) document:

myreport

You can create do-files to automate the creation of files. We created the do-file samplereport.do. It contains

sysuse auto, replace

putdocx begin

// Create a paragraph
putdocx paragraph
putdocx text ("putdocx "), bold
putdocx text ("can add formatted text to a paragraph.  You can ")
putdocx text ("italicize, "), italic
putdocx text ("strikeout, "), strikeout
putdocx text ("underline"), underline
putdocx text (", sub/super script")
putdocx text ("2 "), script(sub)
putdocx text (", and ")
putdocx text ("shade"), shading("blue")
qui sum mpg
local sum : display %4.2f `r(sum)'
putdocx text (".  Also, you can easily add Stata results to your paragraph (mpg total = `sum')")

// Embed a graph
histogram rep
graph export hist.png, replace
putdocx paragraph, halign(center)
putdocx image hist.png

// Embed Stata output
putdocx paragraph
putdocx text ("Embed the output from a regression command into your docx file.")
regress mpg price
putdocx table mytable = etable

// Embed Stata dataset
putdocx paragraph
putdocx text ("Embed the data in Stata's memory into a table in your docx file.")
statsby Total=r(N) Average=r(mean) Max=r(max) Min=r(min), by(foreign): summarize mpg
rename foreign Origin
putdocx table tbl1 = data("Origin Total Average Max Min"), varnames
        border(start, nil) border(insideV, nil) border(end, nil)

putdocx save myreport.docx, replace

To make the report, we could have typed the commands interactively, but we put them in a do-file so that it would be easy to reproduce or even update our document.

To produce the document, we typed

. do samplereport

Tell me more

Learn more about Stata's programming features.

Read more about creating Word documents in the Stata Reporting Reference Manual.


Word is a registered trademark of Microsoft.