Академический Документы
Профессиональный Документы
Культура Документы
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Matthias Seckler
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
WS 2014/15
Matthias Seckler
WS 2014/15
1/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
1. Course Outline
2. Introduction to STATA
3. Data Management
4. Basic programming
4.3 Local macros
4.4 Loops
4.5 Factor variables
4.6 Time series operators
Matthias Seckler
WS 2014/15
2/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
Macros
Matthias Seckler
WS 2014/15
3/12
Computational
Econometrics
Using Stata
Course
Outline
Macros
Introduction
to STATA
local macro
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
To access the value of the local macro, dereference its name using
`macroname'. Everywhere `macroname' appears in a command the
contents are substituted for the macro name.
global macro
global mname [=exp ]
Matthias Seckler
WS 2014/15
4/12
Computational
Econometrics
Using Stata
Loops
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
In everyday data analysis you will often need to retype similar commands over and over again. To avoid retyping similar commands, you
can benet from several tools such as the by-prex or loops.
While the by prex is used to repeat a single command for dierent
observations, loops are used to loop over elements of lists or number
sequences.
The most powerful loop constructs available in Stata are forvalues
and foreach.
Matthias Seckler
WS 2014/15
5/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
forvalues
In the forvalues command, we dene a local macro lname as the
loop index. Following an equal sign is the range of values that lname
will take on as a numlist. The body of the loop (in {}) contains one
or more statements to be executed for each value in the list.
forvalues
forvalues lname = range {
commands referring to 12 lname'
}
the open brace must appear on the same line as the forvalues;
nothing may follow the open brace except, of course, comments;
the rst command to be executed must appear on a new line;
the close brace must appear on a line by itself.
Matthias Seckler
WS 2014/15
6/12
Computational
Econometrics
Using Stata
forvalues
Course
Outline
forvalues
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
Matthias Seckler
WS 2014/15
7/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
foreach
Use foreach to handle several dierent specications of the variable
list.
syntax
foreach lname {in|of listtype} list {
commands referring to 12 lname'
}
Matthias Seckler
WS 2014/15
8/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
Allowed are
foreach lname in any list
foreach lname of local lmacname
foreach lname of global gmacname
foreach lname of varlist varlist
foreach lname of newlist newvarlist
foreach lname of numlist numlist
Matthias Seckler
WS 2014/15
9/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
Matthias Seckler
Result
indicators for levels of group
indicators for each combination of levels of group
and sex, a two-way interaction
indicators for each combination of levels of group,
sex, and arm, a three-way interaction
same as i.group i.sex group]sex
WS 2014/15
10/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
Examples continued
Factor specication
group]]sex]]arm
sex]c.age
sex] ]c.age
c.age
c.age]c.age
c.age]c.age]c.age
Matthias Seckler
Result
same as i.group i.sex i.arm
group]sex group]arm sex]arm group]sex]arm
two variables: age for males and 0 elsewhere,
and age for females and 0 elsewhere;
if age is also in the model, one of the two virtual
variables will be treated as a base
same as i.sex age sex]c.age
same as age
age squared
age cubed
WS 2014/15
11/12
Computational
Econometrics
Using Stata
Course
Outline
Introduction
to STATA
Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators
When a command allows a time-series varlist, you may include timeseries operators.
Operator
L.
L2.
...
F.
F2.
...
D.
D2.
Matthias Seckler
Meaning
lag xt
2-period lag xt
1
lead xt+
2-period lead xt+
1
dierencext xt
dierence of dierence xt xt (xt xt )
1
WS 2014/15
12/12