Вы находитесь на странице: 1из 12

Computational

Econometrics
Using Stata

Course
Outline

Computational Econometrics Using Stata

Introduction
to STATA

Matthias Seckler

Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators

Statistics, Econometrics and Quantitative Methods


Faculty of Economics and Social Sciences

WS 2014/15

Matthias Seckler

Computational Econometrics Using Stata

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

Computational Econometrics Using Stata

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

Local macros help you to work much more eciently.


A macro is a string of characters, called the macroname, that stands
for another string of characters, called the macro contents. You can
think of a macro as a container that can hold one or more objects
(number, variable name).
Macros are printable representation of things.
A macro may be either local or global in its scope, dening where its
name will be recognized. A local macro is created within a do-le and
ceases to exist when that do-le terminates. A global macro exists for
the duration of the Stata program or interactive session.

Matthias Seckler

Computational Econometrics Using Stata

WS 2014/15

3/12

Computational
Econometrics
Using Stata

Course
Outline

Macros

Introduction
to STATA

local macro

Data
Management

local lclname [=exp ]

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 ]

You obtain the value of the global macro as $macroname.

Matthias Seckler

Computational Econometrics Using Stata

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

Computational Econometrics Using Stata

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

Computational Econometrics Using Stata

WS 2014/15

6/12

Computational
Econometrics
Using Stata

forvalues

Course
Outline

forvalues

Introduction
to STATA

forvalues lname = range {


commands referring to 12 lname'
}

Data
Management
Basic
programming
Local macros
Loops
Factor
variables
Time series
operators

range is similar to a numlist. You can specify a range of numbers by


using the rules you already learned, except that you cannot list single
numbers or more than one range.
You can use forvalues to loop over variables with names that have
an integer component in order not to write a separate command for
each one.
Advantage over foreach with a numlist: foreach cannot have more
than 1,600 numbers in a list. forvalues is more ecient for Stata to
process.

Matthias Seckler

Computational Econometrics Using Stata

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'
}

Braces must be specied with foreach, and


the open brace must appear on the same line as the foreach;
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

Computational Econometrics Using Stata

WS 2014/15

8/12

Computational
Econometrics
Using Stata

forvalues and foreach

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

Computational Econometrics Using Stata

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

Using factor variables in Stata


Factor variables create indicator variables from categorical variables,
interactions of indicators of categorical variables, interactions of categorical and continuous variables, and interactions of continuous variables (polynomials).
There are four factor-variable operators: i., c., ] and ]].
Examples
Factor specication
i.group
i.group]i.sex
group]sex]arm
group]]sex

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

Computational Econometrics Using Stata

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

Using factor variables in Stata

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

Computational Econometrics Using Stata

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

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

Computational Econometrics Using Stata

WS 2014/15

12/12

Вам также может понравиться