Stata for Students: Doing Your Work Using Do Files

This article is part of the Stata for Students series. If you are new to Stata we strongly recommend reading all the articles in the Stata Basics section.

A do file is is just a list of Stata commands. When you tell Stata to "do" the do file, it will carry out all the commands in order. By putting all your commands in a do file, you can re-run them at any time. You can do part of a homework assignment on one day and then pick right up where you left off on the next. If it turns out you made a mistake, all you need to do is fix the part of the do file that's wrong and run it again—no need to start over. We strongly recommend you do all your work using do files. This is especially true if you plan on using Stata for research in the future.

One thing that often confuses new Stata users is that Stata works with three things at the same time: your data, your commands, and your results. A properly written do file will manage all three: it will create a .log file to store its results, load a .dta file containing the relevant data, and then run the commands that do the actual work.

Creating a Do File

If you are doing our example assignment, use Windows Explorer to open the U:\SFS folder you created in Managing Stata Files. If you're going straight to your homework, open the folder you created for it, making sure any data files you need are there.

Right-click on a blank spot inside the folder and choose New, Text Document. This will create a file in that folder called New Text Document.txt. (If Text Document is not an option on your computer, click Microsoft Word Document instead and you'll get New Word Document.docx.) If you're doing our example, change the name to example1.do. If you're doing a homework assignment, call it something logical like homework1.do. Make sure you change the .txt (or .docx) part of the filename to .do, so Windows knows this will be a do file. If you don't see .txt or .docx at the end of the file name, revisit the instructions for making file extensions visible in Managing Stata Files.

Editing a Do File

To edit a do file, go to the folder it is stored in and double-click on it. That will open the do file in Stata's do file editor and set Stata's working folder to the folder that it is stored in. If your data file(s) and log file(s) are or will be in the same folder, then you can just refer to them by name without specifying a location and Stata will find them automatically.

Writing a Do File

Almost all do files carry out the same basic steps.

Create a Log File to Store Results

The first thing your do file should do is set up a log file which will store its results. Make sure that no previous log files are still open with:

capture log close

Then open a new log file. We suggest giving a log file the same name as the do file it records, so either:

log using example1.log, replace

or something like:

log using homework1.log, replace

The replace option tells Stata it's okay to replace previous versions of that file.

Clear Stata's Memory

You always want to start a do file with a blank slate, so the next command should be:

clear all

This clears out any data or stored results from whatever you were doing before running this do file.

Open a Data Set

The command to open a Stata data set is use. If you're doing our example, type:

use gss_sample

If you're doing a homework assignment, replace gss_sample with the data set you're using. You don't need to type .dta at the end of the file name, but it won't hurt.

If the data set you want to work with is not in Stata format, you'll need the import command instead of use. You can click on File, Import in the main Stata window and then use the graphical user interface to set up the right options for reading in your data set, but be sure to copy the resulting command into your do file.

Do Your Work

You're now ready to do your work. If you're doing our example, type:

sum age

This will give you the mean age of the 2014 GSS respondents, along with other summary statistics. If you're doing homework, add whatever commands you need to do your assignment. Do files written for research may have hundreds of commands at this point.

Finish Up

Most homework assignments do not require you to save any changes to your data set. But if you have made changes to the data, like creating a new variable, and you want those changes to be available in the future, use the save command to save the modified data set:

save gss_sample2, replace

Again, the replace option means Stata can replace old versions of that file. However, note that the file name in the save command is not the same as in the earlier use command. Never save a modified data set over your original data file. If you do, and it turns out you made a mistake, you will have to get fresh copy of the data from its source. But if you keep the original data set intact, all you have to do is correct the mistake in your do file and run it again.

When you're all done, close your log file:

log close

That will normally be the last command in your do file.

A Complete Do File

Here is the complete do file described in the previous steps:

capture log close
log using example1.log, replace

clear all

use gss_sample

sum age

log close

Running a Do File

You can run a do file by pressing Ctrl-D or by clicking the button on the far right of the top menu that looks like a "play" button. If you select part of the do file, pressing Ctrl-D or clicking play will only run that part.

Using Your Log

The results of your do file will appear in the Results window, but they'll also be stored in your log file. Go back to the window showing your project's folder and the log file should have appeared there. Double-click on it and it will open in Notepad. Your professor may want you to send them this file directly or copy some or all of its contents into a Word document.

If you copy Stata results into Word, change the font for those results to Courier or something similar. Courier uses the same amount of space for each letter (like this) so the columns will line up properly.

Last Revised: 4/30/2019