As previously mentioned, we strongly recommend that you use do-files to structure your work with Stata.
Create a new do-file by clicking on Window\Do-file Editor\New Do-file Editor.

| Note You can also create a new do-file by using the keyboard shortcut Ctrl+9. |
This is what a do-file looks like:

| Note You can increase/decrease the font size in the do-file by clicking on View\Zoom and choose Zoom in or Zoom out. |
Add comments
It is highly recommended that you comment your do-file. You may add a heading above each command, and also make notes of interesting findings, etc.
Alternatives
Start the comment with *
* This is a comment
Start the comment with //
// This is a comment
Start the comment with ///
/// This is a comment
Enclose the comment with /* */
/* This is a comment */
| Note An advantage of the last option is that it allows you to include comments anywhere in the do-file, even in the middle of a command. The other need to be separated from the commands by using line breaks. |
| Note You can easily double-check that the comments are correctly entered, because they turn green if they are. It is also possible to mix different types of comments in the do-file. |
Example
Here is an example where we have started with an informative header for the do-file. After this, we can include the commands, along with headings and comments of the results:

Execute commands
To execute the do-file, there are two options:
- Click on the button with a “play” arrow on it – Execute (do).
- Use the keyboard shortcut Ctrl+D.
Unless you want to execute the whole do-file (which is seldom the case), you need to first highlight the specific part you want to execute.

Error messages and return codes
If you execute a command that is incorrectly specified, Stata will return an error message in the Results window.
It is not always evident why the error has occurred, even though Stata often provides some clues (sometimes, a specific return code is given, which you can learn more about through Google).
| Note In a majority of the cases, the error message is due to a missing symbol (e.g. a dot, a comma, or the inclusion a of a single equal sign instead of a double one). Review your do-file for mistakes like this. |
| Note Be careful – sometimes a command works even though it is not specified as you intended. This risk is particularly important to consider when you transform variables. |
Getting the dataset and do-file to “talk to each other”
More informationhelp use |
One tricky thing is to make sure that your dataset and do-file are correctly linked to each other. To avoid frustration, make sure that you only have one dataset and one do-file open at a time.
Below are three options for making sure that these two files are correctly linked. All options are based on you already having a dataset and a do-file that you want to use.
Option 1: Starting with the do-file and use the menu
- Double-click on the do-file that you want to use.
- An empty dataset will automatically open in the background.
- Load your dataset by going to the menu bar of that empty dataset.
- Click on File\Open.
- Browse your computer to locate the dataset that you want to use.
- Click on Open.
Open 2: Starting with the do-file and use syntax
- Double-click on the do-file that you want to use.
- Insert the following command in the do-file:
use "pathfilename.dta" |
| Note Change “pathfilename” to the full path (i.e. the folder on your computer that contains the file), and specify the file name, such as: use "C:\Users\yerik\Stata Guide\StataData2.dta" |
Option 3: Starting with the dataset and use the menu
In this example too, you already have a dataset and a do-file that you want to use.
- Double-click on the dataset that you want to use.
- Load your do-file by going to the menu bar of the dataset.
- Click on File\Open.
- Browse your computer to locate the do-file that you want to use.
- Click on Open.
Save datasets and do-files
More informationhelp save |
Save a do-file
Overwrite the do-file
- Go to the menu bar in the do-file.
- Click on File\Save.
- Keyboard shortcut: Ctrl+S.
Save the do-file with a new name
- Go to the menu bar in the do-file.
- Click on File\Save as.
- Keyboard shortcut: Ctrl+Shift+S.
| Note You must always use the menu to save a do-file – it cannot be correctly saved by a including a command in the do-file itself. |
Save a dataset
Overwrite the dataset
- Go to the menu bar in the dataset.
- Click on File\Save.
- Keyboard shortcut: Ctrl+S.
or
- Insert the following command in the do-file:
save "pathfilename.dta", replace |
Save the dataset with a new name
- Go to the menu bar in the dataset.
- Click on File\Save as.
- Keyboard shortcut: Ctrl+Shift+S.
or
- Insert the following command in the do-file:
save "pathfilename.dta" |
| Note A dataset can be saved either through the menu or through the do-file. |