DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Overview
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Programming and Loops
Macros
Conditional statements
Loops
Functions and ado les
Thomas Scheike
May 9, 2017
Stata programming
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Stata programming
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Start
Basic Stata programs contains just sequences of Stata commands
analysis.do:
4
// important analysis
use indata
regress y x
scatter y x
1
do analysis1.do
1
2
3
Do Task
Do Task
...
Stop
Organizing an analysis in a script (do le) is a requirement for
conducting reproducible research.
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Stata programming
Stata is a fully capable programming language. By learning basic
ow control operations you can write programs that
are shorter, easier to maintain, and understand
have reduced error rate
Start
Programming and do les
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Reproducibility
Statistical analysis is often an iterative process involving
multiple steps from data processing to publication
When this process has to be revisited (data is updated)
reproducibility is critical and can be a very error-prone exercise
with too many
Do Task
No
Condition?
Yes
Programming and do les
Stop
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Reproducibility
Statistical analysis is often an iterative process involving
multiple steps from data processing to publication
When this process has to be revisited (data is updated)
reproducibility is critical and can be a very error-prone exercise
with too many
Key principle: avoid manual intervention. Generate tables and
gures automatically.
Learn good programming practices. Avoid redundancy. Test
your programs.
Comment your code. Give every do le a header and comment
important sections (as a minimum).
1
2
3
/*
This is a comment
*/
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Stata syntax
Basic Stata programming involves just sequences of Stata
commands
Stata command syntax
[ by varlist: ] command [ varlist ] [ = exp ]
if exp [ in range ] [ weight ] [, options ]
by:
command:
varlist:
=:
if
in
weight
options
**
repeat command over strata (sorted)
regress, summarize, generate, display, . . .
list of variables on which to act
specify result to be assigned to variable
ltering, restrict command to subset
ltering (by range in the form #1/#2)
weight attached to each observation (regression)
additional arguments
Example: command syntax
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Macros
Macros are the "variables" of Stata.
building blocks for structured programming
placeholder for results/information (or think of them as an
alias)
In stata variables/macros are untyped
May be dened as local vs global (scope of macro)
local macroname macrocontent
To evaluate the right-hand-side we must use the '='-syntax
local macroname = macrocontent
Reference the local macro using left-single-quote and
right-single-quote, or maybe easier $_name, i.e.
1
2
display `macroname'
display $_macroname
set more off
Macros
Meta-programming with macros
1
2
3
4
5
1
2
3
4
capture drop x1 x2
input x1 x2
1 3
2 4
end
local idx 1 2
foreach i in `idx' {
list x`i' in 1/2
}
+----+
| x1 |
|----|
1. | 1 |
2. | 2 |
+----+
+----+
| x2 |
|----|
1. | 3 |
2. | 4 |
1
2
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
local var1 Hello, World
display "`var1'" /* double quotes to treat as string */
display "`var1'"
Hello, World
1
2
3
Local macros syntax
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Macros
local i 1
local i = `i'+1 /* Alternative: local ++i */
display "i=`i'"
local i = `i'+1
i=2
1
2
3
4
5
clear
sysuse uslifeexp
local gopt ytitle(Life expectancy in years) xlabel
(1875(25)2025) title(U.S. Life Expectancy)
twoway (line le_male year) (line le_female year),`gopt'
(U.S. life expectancy, 1900-1999)
ife Expectancy)
Macros
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Glocal macros syntax
global macroname macrocontent
Reference the global macro using dollar-sign:
1
display $macroname
**
We can list and remove macros with the macro command
1
2
3
macro list
macro drop macroname
macro drop _all
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Macro scoping
local macros lives within this scope where they were dened,
i.e., the do-le or program/function.
They are removed when leaving the
do-function/program/loop/. . .
1
2
3
4
5
6
Global macros are as the name implies accesible from everywhere
1
2
3
program hilocal
local a = "Hi local"
macro list _a
end
hilocal
macro list _a
4
5
6
a:
1. local a = "Hi local"
2. macro list _a
3. end
_a:
Hi local
local macro `a' not found
r(111);
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Macro scoping
program higlobal
capture macro drop a
global a = "Hi global"
end
higlobal
macro list a
1. capture macro drop a
2. global a = "Hi global"
3. end
Hi global
Example: scoping
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Conditional expressions
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Logical expressions
Comparison operators:
Start
Task A
yes
Decision
no
Task B
a
a
a
a
a
a
== b
!= b
>b
>= b
<b
<= b
true
true
true
true
true
true
if
if
if
if
if
if
a
a
a
a
a
a
equals b
not equal to b
greater than b
greater than or equal to b
less than b
less than or equal to b
Logical expressions can be combined with AND, OR, NEG:
and
or
negation
Stop
Example: logical expressions
expr1 & expr2
expr1 | expr2
! expr
Conditional expressions
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Conditional expressions
Syntax: conditional expressions in Stata
if exp command [else command]
1
2
or with curly brackets allowing for multiple commands
1
2
3
4
5
6
if ((*expression*)) {
...
}
else {
...
}
3
4
5
6
7
8
9
10
11
The else statement is optional
With brackets following the if or else:
12
1
the open bracket must appear on the same line as the if or else
2
nothing may follow the open bracket except comments
3
the closing bracket must appear on a line by itself
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Macros
Remember importantly that
locals are really local
local cond test /* Define condition */
local cond = strtrim(strlower("`cond'"))
if ("`cond'"=="good" | "`cond'"=="normal") {
noisily di "Conditions are OK"
}
else if ("`cond'"=="poor") {
noisily di "Conditions are not good"
}
else {
noisily di ("Warning, unknown condition: " + "'`cond
''")
}
Example: conditional expressions
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Loops
Start
within each do-call
so each time you send from do-editor
can add macro's
1
2
3
4
5
6
7
8
use ../data/iris, clear
local plnames ""
forvalues bot = 0(3)6 {
local top = $_bot + 3
local plnames $_plnames PL$_bot
display "`plnames'"
gen PL`bot' = Petal_Length >= $_top & Petal_Length
<= $_top
}
For debugging
1
2
set trace on
set trace off
Do Task
No
Condition?
Yes
Stop
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
While
While loops created with the body of the loop within curly brackets
2
3
while ((*exp*)) {
...
}
forvalues is a specialized while loop used to loop over series of
numbers
1
2
3
Iterations:
1
2
3
4
5
di "i=`i'"
local ++i
Forvalues
1
2
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Suppose we have variable x3 up to x10, and we wish to replace
these variables with mean centered variables
1
2
3
4
5
6
forvalues (*macroname*)=(*range*) {
...
}
with range given in the form
l/r: from l to r in steps of 1
l(∆)r: from l to r in steps of ∆
The continue function can be used to prematurely exit a loop
(while, forvalues, foreach)
local i = 0
while (`i'<3) {
di "i=`i'"
local ++i
}
2.
3.
4. }
i=0
i=1
i=2
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
forvalues
Syntax: while loops in Stata
1
Forvalues
clear
set obs 10
forvalues i=3/10 {
gen x`i' = rnormal(1) /* Generate some data with
mean 1 */
}
summarize
obs was 0, now 10
2.
gen x`i' = rnormal(1)
3. }
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+-------------------------------------------------------x3 |
10
.5558682
1.122792 -1.022099 2.112015
x4 |
10
.6997332
.7299637 -.9405259 1.524521
x5 |
10
.8642076
1.466137 -1.885089
2.60591
x6 |
10
1.19383
1.198178 -.6709589 2.612096
x7 |
10
.8626354
1.00536 -1.073315 2.022131
-------------+--------------------------------------------------------
continue /* skip remaining commands of current
iteration but continue loop */
continue, break /* exit loop */
Forvalues
1
2
3
4
5
6
7
8
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
forvalues i=3/10 {
quietly {
summarize x`i'
local m = r(mean)
replace x`i' = x`i'-`m'
}
}
summarize
2.
3.
4.
5.
6.
7. }
quietly {
summarize x`i'
local m = r(mean)
replace x`i' = x`i'-`m'
}
Variable |
Obs
Mean
Std. Dev.
Min
Max
-------------+-------------------------------------------------------x3 |
10
2.53e-08
1.122792 -1.577967 1.556146
x4 |
10
6.71e-09
.7299637 -1.640259 .8247882
x5 |
10
3.58e-08
1.466137 -2.749297 1.741703
x6 |
10
0
1.198178 -1.864789 1.418266
Forvalues
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Foreach
foreach loops over elements of a local or global macro or variables
in a varlist.
1
2
3
To remove all missing data
1
2
3
4
5
foreach v of var * {
drop if missing(`v')
}
6
7
8
9
10
11
Loops overview
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
clear
quietly use data/iris
local myvar Sepal Petal
foreach x in `myvar' {
quietly summarize `x'_Length /* see: return list */
local ml = r(mean)
quietly summarize `x'_Width
local mw = r(mean)
di "`x' length/width ratio = `: di %5.0g `ml'/`mw''"
}
2.
quietly summarize `x'_Length
3.
local ml = r(mean)
4.
quietly summarize `x'_Width
5.
local mw = r(mean)
6.
di "`x' length/width ratio = `: di %5.0g `ml'/`mw''"
7. }
Sepal length/width ratio = 1.91
DEPARTMENT OF BIOSTATISTICS
Petal length/width ratio = 3.13
UNIVERSITY OF COPENHAGEN
Loops example
1
forvalues i 1/10 { . . . }
forvalues i 0(2)10 { . . . }
foreach x in dog cat { . . . }
foreach x in 1 3 5 { . . . }
foreach x of varlist Sepal* { . . . }
foreach x of numlist 100 200 1 4 { . . . }
while () { . . . }
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
2
3
4
5
6
7
8
9
10
11
12
clear
quietly use data/iris
foreach x of numlist -2 -1 0 0.333 0.5 1 2 4 {
capture drop xt
gen xt= Sepal_Length^($_x)
quietly reg Sepal_Width xt
local loglike= e(ll)
display "$_x = $_loglike"
}
ereturn list
© Copyright 2026 Paperzz