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.)
« Posting results: Regression | Setting the scrollbufsize »
Tuesday
Mar162010

Always starting a log

Last week we saw how you could use the set scrollbufsize command to increase the size of the scroll buffer for the Stata results window. While this is useful, it does not save a permanent record of your Stata session. This tidbit will show you how you can start a Stata log file every time you start Stata. While this tidbit focuses on how to do this in Windows, this can be generalized to other platforms.

There are two key parts to this tidbit, 1) how to execute a command every time you start Stata and 2) how to start a unique log. First, let's show how you can execute one or more commands when you start Stata. After starting my copy of Stata on Windows, I typed the command pwd and it shows me that my copy of Stata starts in the folder named c:\data.

. pwd
c:\data

I then use the do-file editor to create a file named profile.do that is stored in c:\data. Say that the file contains the following command.

display "Hello world from profile.do"

Every time I start my copy of Stata, I see the following message displayed.

running C:\data\profile.do ...
Hello world from profile.do

You can do this for yourself, by creating a do-file named profile.do that is located in the folder where your Stata starts (which you can see via the pwd command when you start Stata.)

Once you have that working, let's add additional commands to start a log file every time you start Stata. You can do that by inserting these additional commands into your profile.do

display "Hello world from profile.do"

capture log close statalog

local cdt = "`c(current_date)'"
local cdt: subinstr local cdt " " "-", all

local cti = "`c(current_time)'"
local cti: subinstr local cti ":" ".", all

local statalogname "Stata_Log_Created_on_`cdt'_at_`cti'.log"

noisily display "Saving all results in..."
noisily display "`statalogname'"
log using "`statalogname'" , text name(statalog)

After I did this, I closed Stata and when I restarted it, the following message was displayed.

Hello world from profile.do
Saving all results in...
Stata_Log_Created_on_1-Feb-2010_at_16.30.49.log

These commands started a log file named Stata_Log_Created_on_1-Feb-2010_at_16.30.49.log located in the folder c:\data. From now on, every time I start Stata, it will create a log file that contains the date and time that the log was created. This provides a record of all of my Stata sessions, like a transcript of the commands and results of everything that I have done. After a couple of days, I started Stata and typed dir *.log command and it showed me the name of the current log, as well as log files from previous sessions.

. dir *.log
2.3k 2/01/10 16:27 Stata_Log_Created_on_1-Feb-2010_at_16.26.58.log
1.4k 2/01/10 16:30 Stata_Log_Created_on_2-Feb-2010_at_12.30.20.log
0.0k 2/01/10 16:30 Stata_Log_Created_on_3-Feb-2010_at_08.15.49.log

Suppose that the last log file (the one dated Feb 3) is the current log. I could view the contents of this log file even as I am executing Stata with the view command, as shown below.

. view Stata_Log_Created_on_3-Feb-2010_at_08.15.49.log

The value of this is that I can view the entire log of the current session, no matter how large it gets, even if it exceeds the size of the scrollbufsize.

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 (2)

I received the following question...

I think that the opportunity to name a log file "on the fly" , that is to have a descriptive name rather than/ or in addition to, the date and time, would be useful to me. How would I modify the commands in profile.do to have Stata ask me for a file name at the time that profile.do is run?

This is a great question. You can use the display command with the _request() option to give you a chance to supply the name of the log file. You can put the following commands in your profile.do to do this...

display "Hello world from profile.do"
capture log close statalog
noisily display "Enter name of log file." _request(statalogname)
noisily log using "$statalogname" , text name(statalog)

Or, you can download the program called autolog (see findit autolog) that will do essentially the same thing, but use a dialog box to ask you the name for the log file. Either way, this will allow you to enter the name of the log file every time you start Stata.

Happy computing!

Michael

March 17, 2010 | Registered CommenterMichael Mitchell

I received another email with a different way to always start a log that I wanted to share with everyone.

You may want to add that for full reproducibility, it makes a lot of sense to have the "profile.do" start both a -log- and a -cmdlog- upon Stata startup. Mine has the following entries:


*********** start default log **************
set logtype smcl
log using C:/Users/`c(username)'/Documents/Stata/Log/mydefaultlog, append
cmdlog using C:/Users/`c(username)'/Documents/Stata/Log/mydefaultlog, append

This is a very cool suggestion. This creates two logs, one with the output, and one with the commands. For each log, the same file is appended to, creating something like a journal of all the output, and all of the commands. Also note how this is Windows specific follows the file structure that, I believe, begins with Windows Vista. A different structure would be used for Windows XP or other operating systems.

Michael

March 19, 2010 | Unregistered CommenterMichael
Editor Permission Required
You must have editing permission for this entry in order to post comments.