Writing Dynamic Stata Code Blocks

When you write a document in the standard dynamic Markdown format, it is processed in two stages. First the Stata code blocks and in-line references are processed, and the results are substituted into your document. Then the Markdown elements of your document are processed, producing a final HTML document.

Specify the Stata language

The standard approach to including dynamic content in a Markdown document is to place some code-to-be-processed in a fenced code block (demarcated with three or more backticks or tildes). This fenced code block has an additional information tag, the first word of which is reserved for the name of the programming language to be invoked.

This usually looks like

~~~stata
  -- code --
~~~

Similarly, dynamic in-line code (demarcated with single backticks) has an additional info tag for the programming language. It typically looks like

some text `stata --code--` more text

In Stata Markdown, the in-line code is limited to display commands (not arbitrary Stata commands).

The stmd command only processes code in Stata (this could change in the future, to include Mata or other languages). The language for each code block may be specified in a number of alternative ways.

That is, you may use the keyword "stata" or the keyword "s", with or without curly braces. The first option, the simple keyword "stata", gives you HTML with a class="language-stata" declaration, useful if your documents make use of style sheets. The "{stata}" with braces, is compatible with knitr documents in R. The last two options, "{s}" and "s" are compatible with markstat documents.

Examples

Code Blocks

Markdown Resulting Document

Using "{stata}"

```{stata}
sysuse auto, clear
tabstat weight, stat(mean sd)
```
. sysuse auto, clear
(1978 Automobile Data)

. tabstat weight, stat(mean sd)

    variable |      mean        sd
-------------+--------------------
      weight |  3019.459  777.1936
----------------------------------

Using "{s}"

```{s}
tabstat price, stat(mean sd)
```
. tabstat price, stat(mean sd)

    variable |      mean        sd
-------------+--------------------
       price |  6165.257  2949.496
----------------------------------

Using "stata" (no braces)

```stata
tabstat mpg, stat(mean sd)
```
. tabstat mpg, stat(mean sd)

    variable |      mean        sd
-------------+--------------------
         mpg |   21.2973  5.785503
----------------------------------

Using "s" (no braces)

```s
tabstat disp, stat(mean sd)
```
. tabstat disp, stat(mean sd)

    variable |      mean        sd
-------------+--------------------
displacement |  197.2973  91.83722
----------------------------------

In-line Code

In-line dynamic code begins with a backtick followed by a language keyword, followed by a space, followed by a Stata display directive (a display command without the keyword “display”).

Markdown Resulting Document

Using "{stata}" in-line

```{stata, quietly}
summarize price
```

Mean price `{stata} %9.1f r(mean)`

Mean price: 6165.3

Using "{s}" in-line

Standard deviation of price: `{s} %9.1f r(sd)`

Standard deviation of price: 2949.5

Using "stata" (no braces)

Minimum price: `stata %9.1f r(min)`

Minimum price: 3291.0

Using "s" (no braces)

Maximum price: `stata %9.1f r(max)`

Maximum price: 15906.0