You can use stmd
to process documents with different inputs - your document is used as a template. In general there are two ways you can accomplish this.
First, it is important to realize that when you process a dynamic document, stmd
and dyndoc
have access to your current Stata session - data, matrices, return values, programs, and global macros. Dynamic code within your document can refer to these without having to set them up inside your document. In this respect, a dynamic document is not necessarily a reproducible document.
So you could write a do file that changes your data in between two stmd
calls to process the same document, and produce two different reports. The third example below makes use of this method.
The second method for providing different inputs to your dynamic document is to supply optional arguments to the stmd
or dyndoc
command. See help dyndoc
and especially the Stata User’s Guide, section 16.4.1 for more explanation. This method is used in all three of the following examples.
While each of these methods makes use of passing arguments in the stmd
command, the repetition is accomplished by putting stmd
inside a loop.
Suppose this is the content of our Stata Markdown, in a file name “repeated subsets.stmd”.
#### Descriptive Statistics where rep78 == <<dd_display: `1' >>
```stata, nocommands
summarize mpg weight if rep78==`1'
```
#### Regressing mpg on weight (rep78==<<dd_display: `1' >>)
```stata, quietly
twoway (scatter mpg weight) (lfit mpg weight) if rep78==`1'
```
We process this file for each subset of the data with the following commands (in a separate do file):
clear
sysuse auto
levelsof rep78, local(rep78levels)
foreach level of local rep78levels {
stmd "repeated subsets.stmd" `level', ///
saving("rep78 eq `level'.html") ///
replace embedimage
}
The last of the five reports looks like this:
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
mpg | 11 27.36364 8.732385 17 41
weight | 11 2322.727 410.5628 1800 3170
We can modify our Markdown file and the set up within our loop over the stmd
command to make use of Stata’s frames
. The key element is once again passing an argument with the stmd
command.
Suppose this is the Stata Markdown, in a file named “repeated frames.stmd”.
#### Descriptive Statistics where rep78 == <<dd_display: `1' >>
```stata, nocommands
frame rep78_`1': summarize mpg weight
```
#### Regressing mpg on weight (rep78==<<dd_display: `1' >>)
```stata, quietly
frame rep78_`1': twoway (scatter mpg weight) (lfit mpg weight)
```
We process this for each subset of the data with the following commands:
clear frames
sysuse auto
levelsof rep78, local(rep78levels)
foreach level of local rep78levels {
frame put if rep78==`level', into(rep78_`level')
stmd "repeated frames.stmd" `level', ///
saving("rep78_`level'.html") ///
replace embedimage
}
The third of these five reports looks like this:
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
mpg | 30 19.43333 4.141325 12 29
weight | 30 3299 752.4184 1830 4840
Actually, we can repeat this report just by swapping out data sets - no argument passing is required for that. But we will make use of a passed argument to write the headings for our report sections.
Now this is the content of our Stata Markdown, in a file named “repeated datasets.stmd”.
#### Descriptive Statistics where rep78 == <<dd_display: `1' >>
```stata, nocommands
summarize mpg weight
```
#### Regressing mpg on weight (rep78==<<dd_display: `1' >>)
```stata, quietly
twoway (scatter mpg weight) (lfit mpg weight)
```
We process this for each subset of the data with the following commands:
clear
sysuse auto
levelsof rep78, local(rep78levels)
foreach level of local rep78levels {
preserve
keep if rep78==`level'
stmd "repeated datasets.stmd" `level', ///
saving("rep78_data_`level'.html") ///
replace embedimage
restore
}
The first of these five reports looks like this:
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
mpg | 2 21 4.242641 18 24
weight | 2 3100 523.259 2730 3470