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 Command | Purpose |
---|---|
dyndoc | Create HTML from Markdown |
putdocx | Create Word documents |
putpdf | Create 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.
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 |
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 |
You can imagine what this document would look like. Here is a better version of it:
example3.txt |
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 15906The 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.
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.