Do-file template
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 11 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 memory XXm use "path/datafilename.dta" *** your commands go here *** log close exit
Questions:
1. What does "version 11" 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. What does the "XXm" in set memory mean?
Answer.
5. What does exit mean?
Answer.
Answers:
1. Version 11 tells Stata to use its version 11 command interpreter. In the future, when you are running version 12 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.
Back to question
2. This is convoluted, but bear with me. We close
the log at the beginning just in case it is open. If it is open, the next command "log using" will fail. So, we close it first.
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.
Back to question
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.
Back to question
4. The set memory command tells Stata how much memory (RAM) to set aside for your data file. You need to look at how large the file is on disk, add a few Megabytes, and replace "XX" with the number. Remember to add together the sizes of all the files you are using if you will merge or append them in this do-file.
You can review the set memory command in the second example.
Back to question.
5. "Exit" in a do-file means: stop the do-file. Stata recommends that you end your do-files with exit
simply to make sure your last command is executed. How does this help? In order to type "exit" you need to press the Enter key after your last command, which inserts a carriage return, so Stata will see the end of the line and execute that last command. It is easy to forget that last carriage return, and typing "exit" makes sure it is there.
Back to question.
Review again?
Another topic?


