»  Home »  Products »  Features »  Markdown

## Convert dynamic Markdown documents to Word or HTML

### Highlights

• Create Word documents from Stata
• Create webpages from Stata
• Intermix text, regressions, results, graphs, etc.
• Intermix the full output of Stata commands
• Intermix individual values from the results of Stata commands
• Create tables in Word or HTML from the results of Stata commands
• Changes in data or commands are automatically reflected in the document or webpage

Have you ever wanted to create Word, PDF, or HTML files that report on what you have done? Stata provides commands for doing that:

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

Here we tell you about dyndoc.

You first create a file containing text to be formatted using the Markdown text-formatting language intermixed with Stata commands that produce the output you want in your final document. Markdown is a simple, standardized text-formatting language that you can read about at Wikipedia. You mix Markdown with Stata commands that create the output you want. Think of the file you create as being a do-file on steroids.

You then run dyndoc to produce a Word document (.docx) or webpage—an HTML file. The resulting file will contain formatted text along with the Stata output and graphs that your commands produced.

The examples below on this webpage were created using that approach!

They are called dynamic documents, which means that if the data ever change, we can run the original source document back through dyndoc to create an updated webpage.

### Let's see it work

Creating a dyndoc file is as easy as creating a do-file. Here's one that creates an HTML file with only Stata output:


example1.txt      ~~~~
<<dd_do>>
webuse auto, clear
summarize price
<</dd_do>>
~~~~

The four tildes in a row, ~~~~, are Markdown syntax to start and end a code block.

Terms in <<>> are called Stata dynamic-document tags. The code block is bounded by <<dd_do >><</dd_do>>. Stata code inside <<dd_do>><</dd_do>> is executed and its output substituted into the HTML document. We merely save the file above as example1.txt, type dyndoc example1.txt in Stata, and example1.html is created for us:

. webuse auto, clear
(1978 automobile data)

. summarize price

Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
price |         74    6165.257    2949.496       3291      15906




Let's add some text around our example:


example2.txt      We will use the auto dataset. It includes variable price:
~~~~
<<dd_do>>
webuse auto, clear
summarize price
<</dd_do>>
~~~~
The mean of price is 6165. We will also ...


You can imagine what this document would look like. Here is a better version of it:


example3.txt             We will use the auto dataset. It includes variable price:
~~~~
<<dd_do>>
webuse auto, clear
summarize price
<</dd_do>>
~~~~
The mean of price is <<dd_display: %9.0g r(mean)>>. We will also ...


To create the dynamic document from example3.txt, in Stata, we type

. dyndoc example3.txt
(output omitted)


The new file example3.html will be created. And the result looks like this:

We will use the auto dataset.  It includes variable price:
. webuse auto, clear
(1978 automobile data)

. summarize price

Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
price |         74    6165.257    2949.496       3291      15906


The mean of price is  6165.257.  We will also …



Finally, we can even add a Stata graph as an SVG file and some regression output as an HTML table to our document:

We convert the dynamic document in example4.txt to HTML by typing

. dyndoc example4.txt
(output omitted)


and the result in example4.html is

This really is a dynamic document. If the data ever change, all we have to do is rerun dyndoc example4.txt to update the page.

And so ends our quick tutorial.

### Tell me more

dyndoc can substitute output, substitute results in-line, substitute graphs, and create tables from the results of some Stata commands. All of this can be saved as a Word document or an HTML file.

Read more about converting dynamic Markdown documents to Word or HTML in the Stata Reporting Reference Manual; see [RPT] dyndoc.