Professional Documents
Culture Documents
Giuseppe De Luca
(giuseppe.deluca@unipa.it)
(1/27)
Outline
Do-les
Log les
(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.
Do-les (4/27)
Do-les
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;
▶ 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),
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
-------------------------------------------------------------------------------------*/
* 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
-------------------------------------------------------------------------------------*/
* 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
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)) ///
)
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
opens the le lename and prints a copy of the Stata session in lename.
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.
* Here you may have several intermediate commands that you want to exclude from the log file
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;
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).
* Here you could have many intermediate commands that you want to exclude from the log file
* 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)
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
qui {
/*-------------------------------------------------------------------------------------
Author : Giuseppe De Luca
Date : 2021/04/10
Notes : This is an example of modular programming
-------------------------------------------------------------------------------------*/
* Log file
cap log close
log using j_filename, replace text
* Load data
use mydata, clear
* 1 - Info exercise
noi run 1_Info_Exercise.do
* 2 - List of students
noi run 2_List_Student.do