Stata Nested Do Files for Larger Projects

Updated: Aug 27, 2020

When you are running a large analysis in Stata with multiple steps, it can be helpful to save the steps in separate do-files. You can then nest these do-files within a master do-file that can then run the whole analysis. For example – I have a small analysis that has two steps: data management and summary statistics. I keep these steps in separate do-files, which are then nested into a master do-file that will run both steps. You can also add arguments when the do-files are run, which allows you to set up an analysis that can be used multiple times. The way arguments work, when you run the master do-file you give the arguments one at a time, and they can then be referenced within the master do-file as numbered macros. This means the first argument you give is saved in macro `1’, the second is saved in macro `2’, and so on. For example, I run my master do file with the following arguments:

And then, within the file you could have the following lines of commands:

And finally, within each of the do-files:

By nesting your do-files in this way, you can keep larger projects in smaller pieces. This makes it easier to identify any errors, and allows you to re-use parts of the project in other projects.

To learn more about do-file programming in Stata, check out this introductory book at our bookstore: An Introduction to Stata Programming, 2nd Edition.

220 views0 comments

Recent Posts

See All