You are on page 1of 27

Do-le and log-les

Giuseppe De Luca

Università degli Studi di Palermo, Dipartimento SEAS

(giuseppe.deluca@unipa.it)

Corso di laurea in Economia e Finanza

Conoscenze Informatiche - Modulo Stata

Anno accademico 2022-23

(1/27)
Outline

Do-les

Log les

Nested do-les and modular programming

(2/27)
Do-les

Do-les (3/27)
Do-les

A do-le is a standard ASCII le, with typical extension .do, containing a se-
quence of Stata commands, one command for each line of the le.

To create a do-le you can click on the Do-le Editor available in the toolbar.

They provide the most eective way of working with Stata:


▶ avoid to restart a project when you exit Stata;

▶ enhance reproducibility of results (essential for scientic research);

▶ include step-by-step comments in your own codes;

▶ reuse do-les that you or others have written;

▶ organize a big project into smaller and easier steps.

Do-les (4/27)
Do-les

The sequence of commands in a do-le may be executed using the do or run


commands, whose basic syntax is

{do|run} lename [arguments] [, nostop]

where the nostop option allows the do-le to continue executing even if an error
occurs. If lename is specied without an extension, .do is assumed.

The dierence between do and run is that do displays the commands and their
output, while run is silent.

Do-les (5/27)
Do-les

Do-les may also be executed, in whole or in part, directly from the Do-le
Editor by clicking on Execute icon. Alternatively, you can use:
▶ Ctrl+d for do;

▶ Ctrl+r for run.

A do-le completes the execution when:


▶ the end of the le is reached,

▶ an exit is executed,

▶ an error occurs.

Clicking the Break icon or pressing Ctrl+Break while executing a do-le stops
the do-le.

Do-les (6/27)
Rules and recommendations for constructing do-les

Start a do-le by typing version #, where # is the Stata release under which
the le was written. This allows the do-le to run under subsequent releases.

To prevent Stata from pausing when the screen is full, include the set more
off command.

The set linesize # command set the default line size to # characters.

Blank lines and comments may be included freely. Their proper use may consid-
erably enhance understanding of a program. Comments may be included:
▶ by beginning a line with a `*' (a star),

▶ by including `//' at the beginning of the comment (only one line),

▶ by placing the comment in /* */ delimiters (even multiple lines).

Do-les (7/27)
Example: how to start and comment a do-le
/*-------------------------------------------------------------------------------------
File name : 1_Example.do
Lecture n. 3 : How to start and comment a do-file
Author : Giuseppe De Luca
Date : 2021/04/10
-------------------------------------------------------------------------------------*/

* Initial setup of the do-file


version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Conoscenze Informatiche - Stata\Lezioni\lecture 3"
clear
set more off
set linesize 150

* Read in the data


sysuse auto, clear

* Describe the data


describe, full // full avoid abbreviating name of variables

* List the first 5 observations of the variables make, price and mpg
list make-mpg if _n<=5

Do-les (8/27)
Example: how to start and comment a do-le
/*-------------------------------------------------------------------------------------
File name : 2_example.do
Lecture n. 3 : How to start and comment a do-file
Author : Giuseppe De Luca
Date : 2021/04/10
-------------------------------------------------------------------------------------*/

* Initial setup of the do-file


version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Conoscenze Informatiche - Stata\Lezioni\lecture 3"
clear
set more off
set linesize 150

* Read in the data


sysuse auto, clear

* Describe the data


describe, full // full avoid abbreviating name of variables
ssss // I made a mistake here, Stata does not know ssss

* List the first 5 observations of the variables make, price and mpg
list make-mpg if _n<=5

Do-les (9/27)
Example: how to start and comment a do-le

Try to execute the do-le 2_Example.do by typing:


do 2_Example
in the command window. Alteratively, you can type Ctrl+d directly from the
do-le.
Let also try to type:
do 2_Example, nostop
in the command window.

Do-les (10/27)
Rules and recommendations for constructing do-les

Your do-le must be readable. To avoid lines wider than the screen, the end-
of-line delimiter may be changed from carriage return to, say, `;' by using the
command
#delimit ;

Later, this may be changed back to carriage return by using the command
#delimit cr

Alternatively, you can use `///' to continue your command on the next line.

Do-les (11/27)
Examples: graphs usually requires long commands
#delimit ;

twoway
(scatter TestScore Avg_Inc, m(o) mc(gs6))
(scatter Hat_TS_1 Avg_Inc, m(i) c(l) lc(red))
(scatter Hat_TS_2 Avg_Inc, m(i) c(l) lp(-) lc(blue))
,
graphr(c(white))
xtitle("Avg_Inc") ytitle("TestScore")
xlab(5(10)55)
legend(
order(
2 "r=1"
3 "r=2"
)
col(1) size(*0.9) symxsize(5) rowg(*.4)
position(5) ring(0) region(lc(white))
);
#delimit cr

The command twoway ends at `;'. After `#delimit cr' the end-of-line delimiter
is again carriage return.

Do-les (12/27)
Examples: graphs usually requires long commands

twoway ///
(scatter TestScore Avg_Inc, m(o) mc(gs6)) ///
(scatter Hat_TS_1 Avg_Inc, m(i) c(l) lc(red)) ///
(scatter Hat_TS_2 Avg_Inc, m(i) c(l) lp(-) lc(blue)) ///
, ///
graphr(c(white)) ///
xtitle("Avg_Inc") ytitle("TestScore") ///
xlab(5(10)55) ///
legend( ///
order( ///
2 "r=1" ///
3 "r=2" ///
) ///
col(1) size(*0.9) symxsize(5) rowg(*.4) ///
position(5) ring(0) region(lc(white)) ///
)

Again this is a single Stata command (with many options)!!

Do-les (13/27)
Examples: output of twoway graph
The output of our twoway command is like this and it could be further improved
by including additional options...
750
700
TestScore
650

r=1
r=2
600

5 15 25 35 45 55
Avg_Inc

Do-les (14/27)
Log les

Log les (15/27)


Log les

The output of a do-le may be print in a log le. The command:

log using lename [, options]

opens the le lename and prints a copy of the Stata session in lename.

If lename is specied without an extension, Stata assumes .smcl (SMCL is


Stata's output language). Recommended extensions for log les are .log or
.txt, corresponding to ASCII.

The available options include append (species that results be appended to an


existing le), replace (species that lename, if it already exists, be overwrit-
ten), and text (species that the log be recorded in plain text format).

Log les (16/27)


Log les
You can have up to ve SMCL and ve text logs open at a time, but usually one
log le is enough. Controlling the structure of the log le is more dicult. The
command:
log close

stops logging the session and closes the log le, while the commands
log off

log on

can be used to temporarily stop logging the session (leaving the log le open)
and to resume logging to the le.

The idea is to create the log le at the beginning of a do-le, stop logging, and
then start-and-stop logging to print in the log le only the results of interest.

Log les (17/27)


Example: do-le and log-le
/*-------------------------------------------------------------------------------------
File name : 3_Example.do Lecture n. 3 : do-file and log file
Author : Giuseppe De Luca Date : 2021/04/10
-------------------------------------------------------------------------------------*/
* Initial setup of the do-file
version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Conoscenze Informatiche - Stata\Lezioni\lecture 3"
clear
set more off
set linesize 150

* Create and then close the log file


log using 3_Example, replace text
log off

* Load in the data


sysuse auto, clear

* Describe the data


describe, full // full avoid abbreviating name of variables

* Here you may have several intermediate commands that you want to exclude from the log file

* To print results of interest in the log file


log on
list make-mpg if _n<=5
log off

* Close the log file


log close

Log les (18/27)


Example: Log le

I usually give the same name to the do-le and its log le. In this way, I know
immediately which is the log le associated with a given do-le.

To further improve your log le, you can use the prex commands:
▶ quietly - executes a command without showing its output;

▶ noisly - the opposite of quietly;

▶ capture - tries to execute a command suppressing all its output


(including error messages, if any)

Comments may appear also in the log le by using the command display. Below
I provide few examples: 4_Example.do, 5_Example.do, and 6_Example.do (my
preferred structure).

Log les (19/27)


Example: do-le and log-le - 4_Example.do
/*-------------------------------------------------------------------------------------
File name : 3_Example.do Lecture n. 3 : quietly and log file
Author : Giuseppe De Luca Date : 2021/04/10
-------------------------------------------------------------------------------------*/
* Initial setup of the do-file
version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Conoscenze Informatiche - Stata\Lezioni\lecture 3"
clear
set more off
set linesize 150

* Create and then close the log file


quietly log using 4_Example, replace text
qui log off

* Load in the data


sysuse auto, clear

* Describe the data


describe, full // full avoid abbreviating name of variables

* Here you could have many intermediate commands that you want to exclude from the log file

* To print results of interest in the log file


qui log on
list make-mpg if _n<=5
qui log off

* Close the log file


qui log close

Log les (20/27)


Example: do-le and log-le - 5_Example.do
qui {
/*-------------------------------------------------------------------------------------
File name : 5_Example.do
Lecture n. 3 : quietly all commands in the do-file except those which produce the
output of interest...Discuss advantages and disadvantages
Author : Giuseppe De Luca Date : 2021/04/10
-------------------------------------------------------------------------------------*/
* Initial setup of the do-file
version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Conoscenze Informatiche - Stata\Lezioni\lecture 3"
clear
set more off
set linesize 150

* Create and then close the log file


log using 5_Example, replace text

* Load in the data


sysuse auto, clear

* Describe the data


describe, full // full avoid abbreviating name of variables
* Here you could have many intermediate commands that you want to exclude from the log file

* To print results of interest in the log file


noi list make-mpg if _n<=5

* Close the log file


log close
}

Log les (21/27)


Example: do-le and log-le - 6_Example.do
qui {
/*--------------------------------------------------------------------------------------------------------
File name : 6_Example.do Lecture n. 3 : my favorite structure of do-file and log-file
Author : Giuseppe De Luca Date : 2021/04/10
---------------------------------------------------------------------------------------------------------*/
* Initial setup of the do-file
version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Conoscenze Informatiche - Stata\Lezioni\lecture 3"
clear
set more off
set linesize 150

* Create and then close the log file


capture log close // if a log file is open, it is ok to close it
log using 6_Example, replace text

* Load data and print results of interest in the log file


sysuse auto, clear
noi di in gr "I now have full control of my log file"
noi di _n in gr "List of the first 5 observations of the first 3 variables"
noi list make-mpg if _n<=5

* The command display can also be used as a substitute for a hand-held calculator
noi di in gr _n "Let try some easy calculations:" _n
noi di in gr "(5+6) =" in ye 5+6
noi di in gr "(5+6)*4+12^2=" in ye (5+6)*4+12^2
noi di in gr "ln(1) =" in ye ln(1)
noi di in gr "exp(0) =" in ye exp(0)

* Close the log file


log close
}

Log les (22/27)


Nested do-les and modular programming

Nested do-les and modular programming (23/27)


Nested do-les and modular programming

Do-les may be nested, i.e., they may call other do-les.

An example is a master do-le that runs other do-les in sequence. Stata allows
do-les to be nested 64 deep.

This leads very naturally to modular programming, that is the fact of organizing
your programs in a logical sequence of small tasks

This is extremely important for a successful research (such as dissertation thesis)


which involves a set of complicated steps.

Nested do-les and modular programming (24/27)


Example: 1_Master.do

qui {
/*-------------------------------------------------------------------------------------
Author : Giuseppe De Luca
Date : 2021/04/10
Notes : This is an example of modular programming
-------------------------------------------------------------------------------------*/

* Initial setup of Stata


version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Conoscenze Informatiche - Stata\Lezioni\lecture 3"
clear
set more off

* Step 1: short comment here


noi do 1_filename.do

* Step 2: short comment here


noi do 2_filename.do

* Step 3: short comment here


noi do 3_filename.do
}

Nested do-les and modular programming (25/27)


Example: j_lename.do
qui {
/*-------------------------------------------------------------------------------------
Date : 2021/04/10
Notes : overall description of the file here...
-------------------------------------------------------------------------------------*/

* Log file
cap log close
log using j_filename, replace text

* Additional commands to control the output of the log file


set linesize 150

* Load data
use mydata, clear

* Additional commands here...


* Additional commands here...
* Additional commands here...

* Results to be printed in the log file


noi di "List of the first 5 observations"
noi list if _n<=5

* Close log file


cap log close
}

Nested do-les and modular programming (26/27)


Example: Master.do - Online written exams Economics
qui {
/*-------------------------------------------------------------------------------------
Authors : Giuseppe De Luca & Salvatore Modica
Date : 2021/04/10
Notes : online written exams Economics
-------------------------------------------------------------------------------------*/

* Initial setup of Stata


version 16.1
cd "D:\De Luca\Teaching\2021-22_Palermo\Economia Politica"
clear
set more off

* 1 - Info exercise
noi run 1_Info_Exercise.do

* 2 - List of students
noi run 2_List_Student.do

* 3 - Assign (randomly) and solve the exercises given to each student


noi run 3_Exercises.do

* 4 - Tests with multiple answers


noi run 4_1_RM_Micro.do
noi run 4_2_RM_Macro.do

* 5 - Correct and evaluate answers given by the students


noi run 5_1_Corrections.do
noi run 5_2_Marks.do
}

Nested do-les and modular programming (27/27)

You might also like