Using Stata dynamic tags in a text file with the dyndoc command

Let us consider an example where we study the mpg and weight variables in auto.dta. In our examples below, we will first write the commands so that they will be displayed in our target HTML file. Then, we will write the commands so that Stata will process the Stata dynamic tags, displaying the results of the Stata commands in the target HTML file.

We first use the sysuse command to load the dataset and then describe the data using the describe command.

```{stata}
<<dd_do>>
sysuse auto, clear
describe
<</dd_do>>
```
<<dd_do: quietly>>
capture graph describe Graph
local checkdate = "`r(command_date)' `r(command_time)'" 
<</dd_do>>
<<dd_skip_if: ="``gdate''"~="" & "``gdate''"=="`checkdate'">>
<<dd_graph>>
<<dd_skip_end>>
<<dd_do: quietly>>
local `gdate' = "`r(command_date)' `r(command_time)'"
<</dd_do>>

This produces the following Stata results:

. sysuse auto, clear
(1978 Automobile Data)

. describe

Contains data from D:\Programs\Stata15\ado\base/a/auto.dta
  obs:            74                          1978 Automobile Data
 vars:            12                          13 Apr 2016 17:45
 size:         3,182                          (_dta has notes)
------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
------------------------------------------------------------------------------------
make            str18   %-18s                 Make and Model
price           int     %8.0gc                Price
mpg             int     %8.0g                 Mileage (mpg)
rep78           int     %8.0g                 Repair Record 1978
headroom        float   %6.1f                 Headroom (in.)
trunk           int     %8.0g                 Trunk space (cu. ft.)
weight          int     %8.0gc                Weight (lbs.)
length          int     %8.0g                 Length (in.)
turn            int     %8.0g                 Turn Circle (ft.)
displacement    int     %8.0g                 Displacement (cu. in.)
gear_ratio      float   %6.2f                 Gear Ratio
foreign         byte    %8.0g      origin     Car type
------------------------------------------------------------------------------------
Sorted by: foreign

Now, we want to check if mpg is always greater than 0 and less than 100. We use the assert command to perform the check. In this case, we do not want to include any output in the target HTML file, so we use the quietly attribute to modify the behavior of the dd_do Stata dynamic tag.

~~~{stata}
<<dd_do>>
assert mpg > 0 & mpg < 100
<</dd_do>>
~~~
<<dd_do: quietly>>
capture graph describe Graph
local checkdate = "`r(command_date)' `r(command_time)'" 
<</dd_do>>
<<dd_skip_if: ="``gdate''"~="" & "``gdate''"=="`checkdate'">>
<<dd_graph>>
<<dd_skip_end>>
<<dd_do: quietly>>
local `gdate' = "`r(command_date)' `r(command_time)'"
<</dd_do>>
. assert mpg > 0 & mpg < 100

If the data do not satisfy the conditions, dyndoc will fail with an error message, which will occur if we run the same assert command in a do-file.

Next, we want to summarize the weight variable:

<<dd_do>>
summarize weight
<</dd_do>>

This produces the following in the target HTML file:

. summarize weight

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
      weight |         74    3019.459    777.1936       1760       4840