By the end of this chapter, students will be able to:
Statistical analysis is only as valuable as its documentation and presentation. Stata provides multiple pathways for capturing, formatting, and presenting results. This chapter introduces three fundamental approaches:
Log files serve as permanent records of your Stata session, capturing both commands and their output. This is essential for reproducible research and debugging.
Basic Syntax:
log using filename.log, replace
// Your analysis commands here
log closeKey Points:
replace option overwrites existing log filesConsider this analysis of transplant diagnoses by gender:
log using chi2.log, replace
use transplants
tab dx gender, row chi2Interpreting the Output:
After running statistical commands, Stata stores results in memory that can be accessed programmatically.
Viewing Return Values:
return listCommon Return Values:
r(N) - Number of observationsr(chi2) - Chi-square statisticr(p) - P-valuer(r) - Number of rowsr(c) - Number of columnsAcademic publications require specific p-value formatting conventions. Here's a professional approach:
return list
qui {
if r(p) < 0.01 {
local p: di "p < 0.01"
}
else if inrange(r(p),0.01,0.05) {
local p: di %3.2f r(p)
}
else {
local p: di %2.1f r(p)
}
noi di "p = `p'"
}Formatting Rules:
The postfile system allows you to create datasets programmatically, which is useful for:
cls
clear
postutil clear
postfile pp str80 a float(b c) using output.dta, replace
post pp ("1") (24.4) (123)
post pp ("2") (31.5) (164)
post pp ("3") (29.0) (118)
postclose pp
use output, clear
listCommand Breakdown:
postfile - Define the structure and filenamepost - Add observations to the datasetpostclose - Finalize and save the datasetuse - Load the created dataset for verificationVariable Types:
str80 - String variable with maximum 80 charactersfloat - Numeric variable with floating-point precisionThe putdocx command enables creation of Microsoft Word documents with embedded Stata results, facilitating professional report generation.
putdocx clear
putdocx begin
putdocx paragraph
putdocx text ("Document Title")
putdocx paragraph
putdocx text ("Author Information")
// Add content...
putdocx save document.docx, replaceThe example demonstrates creating a research abstract with embedded results:
Document Sections:
Key Features:
HTML output enables web-based publication and sharing of results. The dyndoc command processes markdown-formatted do-files into HTML documents.
Workflow:
dyndoc filename.do, saving(filename.html) replaceSpecial Markup Elements:
<<dd_version: 2>> - Document version specification<<dd_do:nooutput>> - Execute code without showing output<</dd_do>> - End code block<<dd_display: c(N)>> - Display system valuesHTML documents support LaTeX mathematical notation:
.log for session records.dta for data storage.docx for traditional manuscripts.html for web publicationThese techniques can be extended to:
This chapter introduced three fundamental approaches to managing Stata output:
These tools form the foundation for reproducible research and professional statistical reporting. Master these techniques to enhance your analytical workflow and improve the quality of your statistical communications.