Dynamic documents
One way to track the results of your code are log-files. A more convenient way to create nicely formatted reports are dynamic documents. Stata provides two ways of doing this: dyndoc and the put* commands (you already encountered putexcel in the previous chapters). The main difference: For dyndoc, you write text and commands in a txt file, which is then run from Stata. For the put* commands, you write the text and commands in a do-file.
According to Stata, these fairly new tools enable reproducible research. In practice, this might apply only partially: You comment on documents, track changes, cite etc. – all this is not possible when creating the document from a do-file. Still, you could think of “results” sheets which are automatically produced by Stata - for this, the new commands are very convenient. In any case, they make it very easy to create data reports which are updated frequently – for example for monitoring.
Some examples are here and below.
Example for using putdocx
Putdocx
Imagine you are monitoring a data collection. Every day, new data comes in. Every day, you need to observe how many interviews were conducted per day, how many were conducted on the last day, how the main outcome variables look like and so on and so forth. For this, a .doc file with the main results would be nice. You can create this document routinely by using the putdocx command and the procedures you have learned during this course.
use "GPS_Germany", clear
putdocx begin // start putdocx
putdocx paragraph, style(Title) // start title paragraph
putdocx text ("Enumerator report") // enter title
qui su date // summarize the date variable to obtain the latest interview date
global latest = r(max) // store the latest date in a global (this is the number of days since 01jan1960)
global latest_fmt: di %td r(max) // store a formatted version in another global
putdocx paragraph, style(Heading1) // start a paragraph formatted as heading level 1
putdocx text ("Latest data ($latest_fmt)") // enter heading with the information on the latest interview date
qui unique date // get number of interview days
local r_int = round(_N/`r(unique)',.01) // calculate mean: number of observations/day, store a rounded version
putdocx paragraph // start new (plain) paragraph
putdocx text ("On average, `r_int' interviews were conducted per day. ")
qui count if date==$latest
putdocx text ("On $latest_fmt, `r(N)' interviews were conducted. ") // still same paragraph
putdocx text ("The distribution of the level of patience looks as follows:") // still same paragraph
twoway (hist patience if date<$latest , col(blue%25) w(0.25) legend(lab(1 Old))) ///
(hist patience if date==$latest , col(red%25) w(0.25) legend(lab(2 New)))
graph export "patience.png", replace // graphs must be exported before they can be used by Word
putdocx image "patience.png" // include graph
putdocx pagebreak // pagebreak
// Loop over enumerators to create a report per enumerator
forv n = 1/5 {
putdocx paragraph, style(Heading1)
putdocx text ("Enumerator `n'")
qui capture count if enumerator==`n' & date==$latest
local i = r(N)
putdocx paragraph
if `i'>0 putdocx text ("Enumerator `n' collected `i' interviews on $latest_fmt. ")
if `i'==0 putdocx text ("Enumerator `n' collected no interviews on $latest_fmt. "), font(,,red)
putdocx text ("In comparison to others, the distribution of the level of patience as elicited by enumerator `n' looks as follows:")
twoway (hist patience if enumerator!=`n', col(blue%25) w(0.25) legend(lab(1 Others))) ///
(hist patience if enumerator==`n', col(red%25) w(0.25) legend(lab(2 "Enumerator `n'")))
graph export "patience_`n'.png", replace
putdocx paragraph, halign(center)
putdocx image "patience_`n'.png", width(10 cm)
}
putdocx save "Enumerator report", replace // exports everything to Word
You can also use the commands putexcel and putpdf to put results into Excel and PDF, respectively. The syntax and options will differ. You can use the commands docx2pdf, html2docx and markdown to convert the different file types into other file types. Further examples are available here .
Putdocx
Data
Example for using dyndoc
Dyndoc
Here, the code is written in the text files Dyndoc.txt and Dyndoc2.txt. The example is the same as for putdocx:
Imagine you are monitoring a data collection. Every day, new data comes in. Every day, you need to observe how many interviews were conducted per day, how many were conducted on the last day, how the main outcome variables look like and so on and so forth. For this, a .doc file with the main results would be nice. You can create this document routinely by using the putdocx command and the procedures you have learned during this course.
dyndoc "Dyndoc.txt", docx replace
dyndoc "Dyndoc2.txt", docx replace
Stata converts the txt files into docx files with the same filename. The contents are defined in the txt files. The dynamic tags tell Stata where to run which commands and how the output should be included in the docx. The txt file contains some examples how to use the dynamic tags, for a full list type "help dynamic tag". You can also use the dyndoc command to create HTML files and the dyntext command to create text files containing the results. You can use the commands docx2pdf, html2docx and markdown to convert the different file types into other file types. Further examples are available here .
The first example uses a txt file with Markdown formatting of headings, the second a txt file with HTML formatting of headings. You can also mix HTML and Markdown in your dynamic txt file.