A ".do" file is where you write code to tell Stata to do something like conducting statistical analysis or recoding variables.
There are multiple ways to open a blank .do file in Stata. We will walk through three options.
The first option to open a blank .do file can be done on the main window of Stata. To start, open Stata on your computer and click on the icon highlighted in red and a blank do. file will appear.
A second option is to open a blank .do file when another .do file in already open. When on an already opened .do file click on "File" --> "New" --> "Do-file", in the upper left corner of the window, and this will open a blank .do file to begin writing code. You may also type 'control+N'.
The third option to open a .do file in Stata is by using code. We can type the code below in the command box and a black .do file will appear.
Note: The command box in Stata is found on the main page toward the bottom of the screen. We can use the command box to type code without saving it on the .do file. Both the code and the command box are shown below.
To save a do. file, click on the floppy disk icon highlighted in the red circle.
We can think of a .do file as a ‘live’ word document, where we write commands that tell Stata what to do with our data from descriptive statistics to regression models.
Stata code is written with commands which are the main function or analysis you want Stata to do in a line of code.
Depending on what your kind of analysis you are doing it may require additional options to be specified, which are at the end of a code line separated by a " , ". Options are used to indicate specific statistics or options to include in the output.
By starting a line with a “ * “, we can write notes and comments that will appear green. Stata does not recognize this as code.
You can also block off a section of code by bracketing both sides with a " */ ".
Blocking off code
Stata is a statistical program you can ‘write & run’ code for analysis. To run code, that is to tell Stata what you want to do, highlight the code you want to run and click on the rectangle with a blue 'play' button highlighted in red below in a .do file or hold 'control-d' on the keyboard for a Windows computer.
To search for commands in Stata, begin by typing out the first letters and a sub-menu will automatically appear for suggestions of various commands.