11.07.2015 Views

[U] User's Guide

[U] User's Guide

[U] User's Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[ U ] 16.4 Programming with do-files 1915. At the Unix prompt, you can type$ stata -s do filename &or$ stata -b do filename &to run the do-file in the background. The above two examples both involve the use of stata, notxstata. Type stata, even if you usually use the GUI version of Stata, xstata. The examplesdiffer only in that one specifies the -s option and the other, the -b option, which determinesthe kind of log that will be produced. In the above examples, Stata takes the following actions:a. Stata automatically opens a log. If you specified -s, Stata will open a SMCL log; if youspecified -b, Stata will open an ASCII text log. If your do-file is named xyz.do, the logwill be called xyz.smcl (-s) or xyz.log (-b) in the current directory (the directory fromwhich you issued the stata command).b. If your do-file explicitly opens another log, Stata will save two copies of the output.c. Stata ignores more conditions and anything else that would cause the do-file to stopwere it running interactively.To reiterate: one way to run a do-file in the background and obtain an ASCII text log is by typing$ stata -b do myfile &Another way uses standard redirection:$ stata < myfile.do > myfile.log &The first way is slightly more efficient. Either way, Stata knows it is in the background and ignoresmore conditions and anything else that would cause the do-file to stop if it were runninginteractively. However, if your do-file contains either the #delimit command or the commentcharacters (/* at the end of one line and */ at the beginning of the next), the second method willnot work. We recommend that you use the first method: stata -b do myfile &.The choice between stata -b do myfile & and stata -s do myfile & is more personal. Weprefer obtaining SMCL logs (-s) because they look better when printed, and, in any case, they canalways be converted to ASCII text format with translate; see [R] translate.16.4 Programming with do-filesThis is an advanced topic, and we are going to refer to concepts not yet explained; see [U] 18 ProgrammingStata for more information.16.4.1 Argument passingDo-files accept arguments, just as Stata programs do; this is described in [U] 18 ProgrammingStata and [U] 18.4 Program arguments. In fact, the logic Stata follows when invoking a do-fileis the same as when invoking a program: the local macros are saved, and new ones are defined.Arguments are stored in the local macros ‘1’, ‘2’, and so on. When the do-file completes, theprevious definitions are restored, just as with programs.Thus, if you wanted your do-file to1. use a dataset of your choosing,2. tabulate a variable named region, and3. summarize variables marriage rate and divorce rate,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!