,

Piecewise execution of do-files in Stata

Do-files in Stata easily get a bit lengthy. Of course, you can try to shorten do-files and distribute code onto several do-files and have one master file that runs all of the respective sub-do-files (which are included by do dosubfile1.do). Alternatively, you can leave the do-file longish but write your code such that you only…

Do-files in Stata easily get a bit lengthy. Of course, you can try to shorten do-files and distribute code onto several do-files and have one master file that runs all of the respective sub-do-files (which are included by do dosubfile1.do). Alternatively, you can leave the do-file longish but write your code such that you only run parts of the code at once:

This is in fact quite simple and need only two things: (1) a “table of contents” in the beginning of your do-file; and (2) parentheses around each part of the code you want to run separately. Suppose you have three parts of your do-file (e.g., code for cleaning, code for descriptives, and code for regression results), you may want to write:

global part_cleaning = 0 // code for cleaning
global part_descriptives = 0 // code for descriptives
global part_regressions = 0 // code for producing regressions

Below, you can write the code for each section as:

if ${part_cleaning} {
// Write code for cleaning your data here (use, modify, save)
}
if ${part_descriptives} {
// Write code for producing and exporting descriptives here
}
if ${part_regressions} {
// Write code for producing and exporting regressions results here
}

As you can see, these are globals that are currently set to 0. If you would run the current code, none of the code within the curly brackets would be run. If you want one of those sections to run, simply change the respective 0 to 1 and run the entire do-file. That’s it!

Note that you can of course use any other condition instead of 0/1. An alternative would be the strings “on” and “off”. Note that you then would need to write for example:

global part_cleaning = "on" // code for cleaning (off for not running)
if "${part_cleaning}"=="on" {
// Write code for cleaning your data here (use, modify, save)
}

Tags:

Leave a Reply