Stata Help

Do Files in Stata

Stata offers an easy way to create syntax files, known as do files.

Do files have two main purposes. The first purpose is to save you from re-doing a series of tedious data manipulations. A well-kept do file can be both a lifesaver and a time-saver. Similarly, do files can execute commands that you either run frequently or might forget to run to your annoyance, thereby saving time and energy.

Beginning with the first function, you can think of a do file as a command-only log file. Thus, assuming you put every data-relevant command in a do file, and then saved the do file, even if Stata crashed and you were back to nothing but your original data set you could quickly recover all of that time and work by executing your do file.

As with any plain text, you can simply copy and paste commands into a do file as you execute them. As you do this, you can comment the command (starting each comment line with a * or //, or placing each comment within /* and */ delimiters) so that you can note what you did and why you did it for future reference. The copy/paste method is helpful if you mess up and have a lot of useless/incorrectly typed commands in your Review window. 

To add individual commands from your Review window to your do file, click the command once to highlight it and then right-click on it (control-click with a one-button mouse or trackpad). This will open a submenu specific to the Review window. Simply select Send to do-file editor from this menu. This will open a new blank do-file window and then add the indicated line. If you do want to use this option, you should probably use either the shift key to highlight a set of commands in a row (just click the first command you want in your do-file, hold down shift and click, the final command, they should all be highlighted). You can select multiple nonadjacent commands from the Review window by holding down the apple (command) key when you click each command that you want moved to the do file. By moving multiple commands at once into a do-file, you save the trouble and confusion of having sixty single command do files cluttering your workspace.

If you do end up with a variety of do-files that all pertain to a specific data set, it is probably best to combine them all into a single do-file reflecting your entire Stata session, for convenience and clarity of record. Like many things in Stata there are multiple ways to achieve this.

If all the files are open, you could simply open a new do-editor window, by clicking the and then copying and pasting all the individual files into this master file. Alternatively, if you already have a main do file and you want to add new sets of commands from another do-file, you can tell Stata to add the second do-file wherever you place your cursor by selecting Insert from the file menu while editing your do file.

Once you have a do-file open you can run commands in a variety of ways. You can highlight a subsection of the commands and just run those or you can run the whole do file.

In either case, whether you go with a highlighted section or just run the whole file, click the button to tell Stata to do those commands.

Once you click the "do" command, your Results window will show each command and its results and your Review window will show that you have just run a do file.

Back