Stata Tidbits

These tidbits contain bits and pieces of information I hope you find helpful to use Stata more effectively. You can receive notifications of new tidbits as they are added (via email) by clicking on the subscribe box at the left. (Every email has an unsubscribe link, making it a snap to unsubscribe.)
« Command log files | log files and do-files »
Tuesday
Feb152011

Creating multiple log files

The tidbit from last week described a started set of commands for creating a log file from within a do-file. This week we take that basic structure and augment it with the ability to have more than one log file open at once.

To recap from last week, we saw a do-file that looked like the following.

. capture log close . log using mergefiles, replace * Some commands here. . log close

Now, imagine that we wanted to write a do-file that would analyze our data. It would include a long set of tabulations, a long set crosstabulations, and then a long set of regressions. Say the do-file was named analyze.do. The contents of the do-file might look like this.

. capture log close . log using analyze, replace * Some commands here. . log close

But suppose we also wanted to separate the output into three separate log files, the tabulations, the crosstabs, and the regressions. We could do so like this.

. capture log close . log using analyze, replace capture log close logtab log using tab, replace name(logtab) * Tabulation commands here. log close logtab capture log close logxtab log using xtab, replace name(logxtab) * Cross tabulation commands here. log close logxtab capture log close logreg log using reg, replace name(logreg) * Regression commands here. log close logreg . log close

Running this do-file would create the overall log file named analyze.smcl that would contain the commands and output from the entire do-file. The log file tab.smcl would contain just the output of the tabulations, xtab.smcl would contain the output of the cross tabulations, and reg.smcl would contain the output of the regression analyses. The log file for the tabulations includes the name(logtab) option that specifies that the logname is logtab. The capture log close logtab and log close logtab commands specifically refer to that log file, letting Stata know that we are referring to the log file with the logname of logtab

At any one time, this do-file has up to two log files active at once. You can have up to five SMCL logs and five text (.log) files active at one time, probably more than you would ever need. For more information, see help log.

You can download the example data files from this tidbit (as well as all of the other tidbits) as shown below. These will download all of the example data files into the current folder on your computer. (If you have done this before, then you may need to specify net get stowdata, replace to overwrite the existing files.

net from http://www.MichaelNormanMitchell.com/storage/stowdata net get stowdata
If you have thoughts on this Stata Tidbit of the Week, you can post a comment. You can also send me an email at MichaelNormanMitchell and then the at sign and gmail dot com. If you are receiving this tidbit via email, you can find the web version at http://www.MichaelNormanMitchell.com/ .

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (3)

A related question.. the first time I run a log the program stops if it contains ",replace". Any solutions? Thanks

February 16, 2011 | Unregistered CommenterPeter

Good and clear advice as always, but for the specific application of creating a results log it seems like you'd often be better off using estout with the append option as this let's you not only put the results in a separate file but also get them a bit more compact and journal-like in appearance.

February 16, 2011 | Unregistered CommenterGabriel

To Peter: My thought is that the , replace is a red herring and that you might need the capture log close command to precede the log using command to ensure that the log is indeed closed. If you try and open a log file when the log is already open, you can get an error.

To Gabriel: Good suggestion indeed. It can take some extra effort to use estout, but it can also lead to a great payoff. For more information about estout, readers can visit http://repec.org/bocode/e/estout/.

February 16, 2011 | Registered CommenterMichael Mitchell
Editor Permission Required
You must have editing permission for this entry in order to post comments.