Chapter 3 Executing do-files and making log files

From Mitchell 10.3, you should always use a do file when you work with Stata. I find log files helpful, but do files are essential. Do files provide transparency to show your work, and it help provide replication. One key feature of do files are comments. Comments are an essential part for two reasons. It will help yourself when you go back to your own code to describe what is going one. Second, it helps with replication, since someone should be able to run your code top to bottom and get the same results that you have in your paper, report, or document.

Never use point and click. It is available in Stata, but don’t lower yourself to a SPSS standard.

In case you are new to do files. We’ll start off with a simple example. We’ll pull some data, summarize some data, and tabulate some data.

use wws.dta, clear
summarize age wage hours
tabulate married
/Users/Sam/Desktop/Econ 645/Data/Mitchell

(Working Women Survey)

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
         age |      2,246    36.25111    5.437983         21         83
        wage |      2,246    288.2885    9595.692          0     380000
       hours |      2,242    37.21811    10.50914          1         80

    married |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        804       35.80       35.80
          1 |      1,442       64.20      100.00
------------+-----------------------------------
      Total |      2,246      100.00

We can look at a similar file called example1.do

    type example1.do

We can actually run a do file within a do file

do example1.do
(Working Women Survey w/fixes)

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
         age |      2,246    36.22707    5.337859         21         48
        wage |      2,244    7.796781     5.82459          0   40.74659
       hours |      2,242    37.21811    10.50914          1         80

    married |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        804       35.80       35.80
          1 |      1,442       64.20      100.00
------------+-----------------------------------
      Total |      2,246      100.00

You can use the doedit command if you want to open the Do-file Editor, but you can just click to open the Do-File (this is an exception from what I said above).

Since we already have the Do-File Editor open, running the doedit will just open a new blank do file.

doedit 

Note: you can write do files in Notepad or TextEdit, but you don’t get any of the benefits, such as highlighted text.

Let’s look at a do file that uses the log command

type example2.do

Let’s run it.

log using example2
use wws2, clear
summarize age wage hours
tabulate married
log close example2

Note: It is important to note that when opening a log it must end with a “log close” command. If your do files bombs out (fails to finish) before reaching the log close command, your log will remain open.

Even if we close the log file, we’ll get an error if we try to run the do file again. So we need to add the replace option.

log using example2, replace
use wws2, clear
summarize age wage hours
tabulate married
log close
      name:  <unnamed>
       log:  /Users/Sam/Desktop/Econ 645/Data/Mitchell/example2.smcl
  log type:  smcl
 opened on:  10 Aug 2025, 09:40:21

(Working Women Survey w/fixes)

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
         age |      2,246    36.22707    5.337859         21         48
        wage |      2,244    7.796781     5.82459          0   40.74659
       hours |      2,242    37.21811    10.50914          1         80

    married |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |        804       35.80       35.80
          1 |      1,442       64.20      100.00
------------+-----------------------------------
      Total |      2,246      100.00

      name:  <unnamed>
       log:  /Users/Sam/Desktop/Econ 645/Data/Mitchell/example2.smcl
  log type:  smcl
 closed on:  10 Aug 2025, 09:40:21
------------------------------------------------------------------------------------------------------------------