Put blank lines in between logical sections of your code, just like paragraphs in regular text. Normally a do file has one command per line—in fact Stata uses the end of the line to know when the command is complete. Use this to make long lines more readable. Graph commands can get very long very quickly:. Comments are bits of text Stata will ignore. This is important because if your do file crashes before it gets to the command to close its log at the end, it will leave the log file open.
The capture prefix tells Stata to ignore any error messages the following command produces. In this case, we use it because we want the do file to proceed whether there's an open log to close or not. Then open a new log file. We suggest giving a log file the same name as the do file whose results it records, so there's never any confusion about which log goes with which do file.
To give your do file a name, press Ctrl-s or click File , Save as and call it first. Then go back to the do file itself and type:. The replace option tells Stata it's okay to replace previous versions of that file.
Specifying the. Another key to reproducibility is always starting with a blank slate, so the next command should be:. This clears out any data or stored results from whatever you were doing before running this do file.
In general, you'll load data with the use command. However, since we're using the auto data set that comes with Stata, you'll open it with sysuse :. Every time you run this do file, it will load a fresh copy of the data from disk into memory.
This means you don't have to worry about any mistakes you might have made previously or keep track of the current state of data set in general. Do files that carry out data wrangling will change the data set, and need to save the new version of the data at the end. This do file does not change the data, but save it anyway for practice:.
The replace option again allows Stata to overwrite the output from previous attempts to run the do file. Never, ever save your output data set over your input data set. In other words, the starting use command and the ending save command should never act on the same file. If you do, the data set your do file was written to work with will no longer exist. The do file may not run at all, and if it does it most likely won't give the same results. If it turns out you made a mistake, you may have to go back to your raw data and start over.
If you don't close the do file's log, any commands you run after the do file finishes will be recorded in the the log. This includes if your do file crashes before reaching the log close command hence capture log close at the beginning. The easiest way to run a do file is to press Ctrl-d in the Do File Editor, or click the icon on the far right that looks like a "play" button over some code. If you first select just part of the do file then only that part will be run.
Running parts of your code rather than the entire do file can be useful, but code taken out of context won't always work. For example, if you run a command that creates a variable x , realize you made a mistake, and then fix it, you can't simply select the command that creates x and run it again because x already exists. Collectives on Stack Overflow. Learn more. Asked 3 years, 8 months ago.
Active 3 years, 7 months ago. Viewed 1k times. My main question is: What is the reason behind this difference? Is there anything I can do to suppress these errors? Also, this is something like a red flag for me: Are there other behaviors that differ when I run things via the command prompt rather than the do file editor? Improve this question.
Add a comment. Active Oldest Votes. The following Technical Note from the 16th Stata manual about Do-files explains: " See the following post for another unrelated example of an inconsistent behaviour: Stata axis labels off-center when broken over multiple lines Personally, after using Stata extensively for years, i have not noticed any other major differences when running code from do files and interactively. Improve this answer.
This is helpful.
0コメント