Joseph Nathan Cohen

Department of Sociology, CUNY Queens College, New York, NY

Using Do Files in Stata

To see more about the “Teaching through YouTube” series.  For more, see this post.

Original Video Description

How to use do files in Stata

Transcription (Auto-Generated)

in this video i’ll show you how to use do files in stata do files are useful in many ways they allow you to automate repetitive tasks you can write a do file once and then use it many times over also they’re good for tracking and saving the steps of an analysis if you enter commands through a do file and execute them instead of just entering them into the command window then it’s easy for you to figure out what steps you’ve taken to arrive at the findings that you’ve gotten and to go back and change them that’s the third benefit it’s easy to correct an analysis without starting from scratch when you do an analysis on do files and you find out several minutes or even hours later that you’ve made a small mistake if you have a do file you can just go back and make that one mistake and run your steps automatically if you don’t use two files then you have to input your steps one by one and start from scratch finally do files are great because it’s easy to share code with others makes collaboration easier it makes it easier for other people to look at your work do files are written in the do file editor to open the do file editor type the word do edit in the stata command window let’s see what it’s like to open the do file editor in a stata session type do edit in the command window and the do editor will appear you write your stata code in the do file editor each line of code is like entering a command one time each carriage return is equivalent to pressing enter in the command window and executing the command remember to save your do files often let’s see what it looks like to enter commands into the do file editor in a real session save your do edit file in the file save as window i’m calling this file my do file anything you type with an asterisk will not be run by stata they’re comments i type a series of commands in my do file and i save it i can open my do file later by pressing the open button in the do file editor there are two ways to execute a do file if you do a do file it will show the commands in the result window if you run a do file it executes the do file quietly you don’t see all the commands that are being run to execute a do file enter the command do with the do file’s name in parentheses to run a do file you enter run and the do file’s name in parentheses let’s see what it looks like to execute a do file in a real stata session to run my do file i type do and then the name of the do file dot do in quotation marks and as you can see it ran the three tables let’s review a do file is a script of stata commands you can enter a series of stated commands save them in a file and execute them all at once for more information on do files type help do in this data command window