Do files
Rather than launching every command via the command window, you can save routines in dofiles and run single or multiple lines of code directly from this file.
Advantages of dofiles:
- Allows you to write comments and explain what you are doing (documentation for you and others)
- Allows you to go back in your analysis and make changes without compromising the latest code you worked on
- You can share your analysis for replication
- Some operations (e.g. loops) require several lines of command. This is much easier done using dofiles.
- A do file that turns out to be very helpful can be turned into an 'ado file’, a user-written Stata command.
Do files
Comments in dofiles
One-line comments:
* One comment
// another comment
Multiple line comments:
/* one comment
is continued
in another line*/
Running commands from the dofile:
- If you hit the “run” button, all commands are executed in the order that is in the dofile.
- If there is an error, the execution of the code is stopped and all following commands are not run. Try to have dofiles that run from top to bottom without an error message.
- Once you highlight only a subset of rows before hitting “run”, only the commands in these lines are executed. You have to highlight at least one character to run the entire line.
- The keyboard shortcut for “run” on Windows is “Ctrl” & “d”
Repeat the calculator exercise in one dofile rather than through the command line, add a heading and some explanations on the steps as comments
- Calculate the square root of three times the sum of thirty four and eight
- Check whether minus three to the power of four is smaller than four to the power of three