Comments and Other Tools for Making Do Files Readable

Do files need to be readable by humans as well as Stata. In this article we'll talk about a few tools that can make it much easier for humans, like yourself, to read your do files.

Blank Lines

Stata doesn't care about blank lines in your code, but they can make it easier for you to read. Put blank lines in between logical sections of your code, just like paragraphs in regular text.

Compare this do file:

capture log close
log using example1.log, replace
clear all
set more off
use gss_sample
sum age
log close

To this one:

capture log close
log using example1.log, replace

clear all
set more off

use gss_sample

sum age

log close

Breaking up Long Lines

Normally a do file has one command per line—in fact Stata uses the end of the line to know when the command is complete. However, if you put three slashes at the end of a line (///) then the command can continue on the next line. Use this to make long lines more readable. Graph commands can get very long very quickly:

graph hbar, over(life) blabel(bar, format(%5.1f)) ytitle(% of Respondents) title(Life is...) note(N=181)

This will be easier to read if you turn it into:

graph hbar, over(life) blabel(bar, format(%5.1f)) ///
ytitle(% of Respondents) title(Life is...) note(N=181)

Indenting the second line makes it visually obvious that it's part of the previous line.

Comments

Comments are bits of text Stata will ignore. You can use them to explain your do file to other readers, or for notes to yourself. Your instructor might ask you to put your name in a comment so it appears in your log file.

Two slashes (//) means "the rest of this line is a comment." Alternatively, you can start a comment with /* and then it will continue until you type */. Here are some examples of ways to use comments:

/* Create a set of indicator variables for whether the respondent
answered the scientific knowledge questions correctly. */

gen bigbang_right=(bigbang==1) if bigbang<.
gen electron_right=(electron==1) if electron<.
// Correct answer for laser is 2, not 1
gen laser_right=(laser==2) if laser<.

Last Revised: 7/1/2016