Saving and printing results by using logs in Stata

1. Using logs in Stata

When you work on an analysis, it is worthwhile to behave like a bench scientist and keep a lab notebook of your actions so that your work can be easily replicated. Everyone has a feeling of complete omniscience while working intensely—this feeling is wonderful but fleeting. The next day, the exact small details needed for perfect duplication have become obscure. Stata has a lab notebook at hand: the log file.

A log file is simply a record of your Results window. It records all commands and all textual output as it happens. Thus it keeps your lab notebook for you as you work. Because it writes the file to disk while it writes the Results window, it also protects you from disastrous failures, be they power failures or computer crashes. We recommend that you start a log file whenever you begin any serious work in Stata.

2. Logging output

All the output that appears in the Results window can be captured in a log file. Stata can save the file in one of two different formats. By default, Stata will save the file in its Stata Markup and Control Language (SMCL) format, which preserves all the formatting and links from the Results window. You can open these results in the Viewer, and they will behave as though they were in the Results window. If you would rather have plain-text files without any formatting, you can save the file as a plain log file. We recommend using the SMCL format because SMCL files can be translated into a variety of formats readable by applications other than Stata with the File > Log > Translate… menu (see [R] translate).

To start a log file, click on the Log button, . This will open a standard file dialog that allows you to specify a directory and filename for your log. If you do not specify a file extension, the extension .smcl will be added to the filename. If you specify a file that already exists, you will be asked whether you want to append the new log to the file or overwrite the file with the new log.

3. Working with logs

Log files are best viewed using Stata’s Viewer. Select File > Log > View…. If there is a log file open (as shown by the status bar), it will be the default log file to view; otherwise, you need to either type the name of the log file into the dialog or click on the Browse… button to find the file with a standard file dialog.

Once you are in the Viewer window, everything behaves as expected: you can copy text and paste between the Viewer and anything else that uses text, such as word processors or text editors. You can even paste into the Command window or the Do-file Editor, but you should take care to copy only commands, not their output. It is okay to copy the prompt (“. ”) at the start of the echoed command because Stata is smart enough to ignore it in the Command window. When working with a word processor, what you paste will be unformatted text; it will look best if you use a fixed-width font, like Courier, to display it.

Viewing your current log file is a good way to keep a reminder of something you have already done or a view of a previous result. The Viewer window takes a snapshot of your log file and hence will not scroll as you keep working in Stata. If you need to see more recent results in the Viewer, click on the Reload page button.

For more detailed information about logs, see [U] 15 Saving and printing output—log files and [R] log. For more information about the Viewer, see [GSW] 3 Using the Viewer.

4. Printing logs

To print a standard SMCL log file, you need to have the log file open in a Viewer window. Once the log file is in the Viewer, you can click on the Print button, right-click on the Viewer window, and select Print…, or select File > Print…. A Print dialog will appear. After you click on Print, a Print settings dialog will appear.

  • You can fill in none of, any of, or all the items Header, Name, and You can check or uncheck options to Print line numbers, Print header, and Print logo. These items are saved and will appear again in the print sheet Print settings (in this and in future Stata sessions).
  • You can set the font, margins, and color scheme that the printer will use by clicking on .. in the Print settings dialog to open the Printer preferences dialog. Monochrome is for black-and-white printing, Color is for default color printing, and Custom 1 and Custom 2 are for customized color printing. You can set the font by clicking on the Font… button. The resulting Font dialog will list only the fixed-width “typewriter” fonts (for example, Courier) available for your printer.

You could also use the translate command to generate a PostScript or PDF version of the log file. See [R] translate for more information.

If your log file is a plain-text file (.log instead of .smcl), you can open it in a text editor, such as Notepad, in the Do-file Editor or in your favorite word processor. You can then edit the log file—add headings, comments, etc.—format it, and print it. If you bring the log file into a word processor, it will be displayed and printed with its default font. The log file will not be easily readable when printed in a proportionally spaced font (for example, Times New Roman or Georgia). It will look much better printed in a fixed-width font (for example, Courier New).

You may wish to associate the .log extension with a text editor (such as Notepad or WordPad) in Windows. You can then edit and print the logs from those Windows applications if you like.

5. Rerunning commands as do-files

Stata also can log just the commands from a session without recording the output. This feature is a convenient way to make a do-file interactively. Such a file is called a cmdlog file by Stata. You can start a cmdlog file by typing

cmdlog using filename.do and you can close the cmdlog file by typing cmdlog close

Here, for example, is what a cmdlog of the previous session would look like. It contains only commands and comments and hence could be used as a do-file.

If you start working and then wish you had started a cmdlog file, you can save yourself heartache by saving the contents of the History window. The History window stores the last 5,000 commands you have typed. Simply right-click on the History window and select Save all… from the menu. This will work best if you first filter out all the commands that resulted in errors as was shown in The History window in [GSW] 2 The Stata user interface. If you would like to move the commands directly to the Do-file Editor, select Select all followed by Send selected to Do-file Editor. You may find this method a more convenient way to create a text file containing only the commands that you typed during your session.

See [GSW] 13 Using the Do-file Editor—automating Stata, [U] 16 Do-files, and [U] 15 Saving and printing output—log files for more information.

Source: STATA (2021), Getting Started with Stata for Windows, Stata Press Publication.

1 thoughts on “Saving and printing results by using logs in Stata

Leave a Reply

Your email address will not be published. Required fields are marked *