Do Files FAQ
Why am I reading this?
Because by now, you’ve learned how hard it is to give Stata a set of commands that run properly. And you might be wondering… Can I turn all this work into a roadmap that I can re-use? Can I ask Stata to “re-do” the commands I’ve written? Can I send someone else a play-by-play to run these commands on their computer?
The answer is yes. And this roadmap is called a “Do File.”
How do you create a do file?
You have several options. The first is to click on the do file icon at the top of your screen, the second is to select “Window,” “Do-File Editor,” and “New Do-File Editor.” You can also simply hold “Ctrl” & “9” on your keyboard. A window will appear!
You can use the familiar “save” and “save as” functions to name the do file. It will default automatically to a “.do” and this is what you want.
As you enter commands into the command window, you will also log them in your Do File. You can manually type them or copy and paste them, but be sure to eliminate the “.” that is listed at the beginning of each command after they run in Stata.
List the commands. Save the do file. Now you are able to re-run the set of commands without manually re-entering them.
(The Do File is where you log your clean, fully-edited, fully-runnable roadmap. This is unlike a log file which includes everything you attempt, successful or unsuccessful. You do not want to include error-producing commands in your “Do File.” It should run clean. That’s its purpose.)
How to you execute commands using a Do File?
Once you have opened a saved “Do File,” you can re-run the commands by selecting the “Execute” button at the top of the Do File. It will re-run all of the listed commands. You can also execute it manually by giving Stata the command:
do “[FILETITLE].do”
So, for example, if you called the Do File “Week1Activity” in your Stats 2020 folder in your L drive, you would say:
do: “L:\stats2020\Week1Activity.do”
A few tips and tricks
Run Part of a Do File:
If you want to run only part of the Do File, simply highlight the commands you want with your mouse, and select “execute.”
You can also temporarily hide part of your do file from Stata by sectioning off part of the file that you’d like Stata to “skip.” You do this by bookending the section you want skipped with the commands “/*” and “*/”. So if you had a Do File representing the entirety of the Week 4 Activity, it would look like this:
use “L:\stats2020.dta”
log using “Week4Activity.log”
tab1 sex partners
tab sex partners
tab partners
tab partners, nol
recode partners (0=0)(1=1)(2/7=3)(9=9), gen(partnersrecoded)
tab partnersrecoded
tab sex partnersrecoded
recode partners (0=0)(1/9=1),gen(sexactiverecoded)
tab sex sexactiverecoded, row
sum partners, det
scatter sex partners
scatter age partners
log close
Let’s imagine that you wanted to execute the same commands, but “skip” the scatterplots:
use “L:\stats2020.dta”
log using “Week4Activity.log”
tab1 sex partners
tab sex partners
tab partners
tab partners, nol
recode partners (0=0)(1=1)(2/7=3)(9=9), gen(partnersrecoded)
tab partnersrecoded
tab sex partnersrecoded
recode partners (0=0)(1/9=1),gen(sexactiverecoded)
tab sex sexactiverecoded, row
sum partners, det
/* scatter sex partners
scatter age partners
*/log close
You’ll see that Stata runs the entire set of commands but skips the commands between the bookends.
Include a Note in the Do File:
If you’d like to include a note in your do file (a comment on or reminder of what you’re doing), simply begin your note with an asterisk “*” and it will be understood by Stata as a comment, not a command. For example if you had tabulated a few variables of interest, but wanted to remind yourself that maybe you didn’t need to include the variable “sex” because it might be redundant, your do file might look like this:
tab1 race sex age
tab sex age
* Remember to remove the descriptive statistics for “race” if I don't end up including "race" in the model
sum age
Condense a Long Command
Some commands are very long, and difficult to view in the Do File Editor. The Editor doesn’t wrap text, so long lines will stretch horizontally indefinitely. Because including an “Enter” after a command signals to Stata that this is the complete command you’d like to run, you cannot simple break up a long command into two lines. If you’d like to do this, you need to tell Stata this. You do this by splitting the command into two lines of text in the editor, and including a “break” (“///”) at the end of the first line of text. For example… if you were tabulating many variables related to opinions on homosexuality at once, it might look like this:
tab1 colhomo homosex libhomo marhomo spkhomo
But it could also look like this:
tab1 colhomo homosex libhomo ///
marhomo spkhomo