This do-file template combines several of the commands explained in earlier examples, and it adds a few. You might want to copy and paste it as a starting point for your programs, at least until you develop your own programming style.
version 13 capture log close log using "path/do-filename.log", replace /* What project this is */
/* What this do-file does */
/* Who wrote it */
/* When you wrote it */
/* Where it is */ clear all set more off use "path/datafilename.dta" *** your commands go here *** log close
1. What does "version 13" mean? Answer.
2. Why close the log at the beginning of the do-file? And what does "capture" do? Answer.
3. What are those comments, like "What this do-file does" doing here? Answer.
4. Why set more off? Answer.
1. Version 13 tells Stata to use its version 13 command interpreter. In the future, when you are running version 14 or later. Even if a command has become obsolete, this do-file will still work, because Stata will switch to the version it was written for.
Why might it be open? If there is an error in the do-file, it will stop running before reaching the end. The last line, log close, will not be executed, and the log will still be open. So, we close it before reopening it.
Capture is there to intercept an error message and allow the do-file to continue. If the log did close, or you closed it outside the do-file, the log close command would cause an error. Capture gets past any error the log close command might generate.
Why do this? We do not have to. We could just remember to close the log each time our do-file ends prematurely. But, I would rather not have to think about it, so I put "capture log close" at the beginning.
The log command is covered in the first example.
3. The comments are my not-very-subtle plea for documentation. If you document your do-files, you or the person who inherits your do-files on a long-term project, will have a good chance of understanding what they do. The more information you put here, the better chance everyone will have of understanding them later.
4. The set more off command tells Stata not to stop scrolling and display --more--. You typically want to execute a do-file, once you've debugged it, straight through from start to finish. If you set more off, it will keep scrolling even if a command fills a screen and would normally pause.