Home  /  Products  /  Stata 15  /  Markdown

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

Convert dynamic Markdown documents to HTML with dyndoc

What's this about?

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

New CommandPurpose
dyndocCreate 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 webpage—an HTML file. The resulting HTML 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.

Highlights

  • 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 HTML tables from the results of Stata commands
  • Changes in data or commands are automatically reflected on the 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:

code in Do-file Editor

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

. dyndoc example4.txt 
  (output omitted)

and the result in example4.html is

Result in browser

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 HTML tables from the results of some Stata commands.

Read more about converting dynamic Markdown documents to HTML in the Stata Programming Reference Manual; see [P] dyndoc.

Learn more about Stata's programming features.