Intro To Loops - Local & Global
Intro To Loops - Local & Global
Intro To Loops - Local & Global
Introduction to
Macro and Loop in Stata
SOC 561
Programming for the Social Sciences
Hyungjun Suh
Mar. 7. 2016
Overview
1
2016-03-06
2
2016-03-06
3
2016-03-06
Using Macros
local : `macroname’
global: $macroname
Assignment
Usage
4
2016-03-06
Example
Example
10
5
2016-03-06
Example
This way, you can
truncate a long list of
variable, which helps you
debug.
11
Example
You can group different
variables to different
macros, which can
reduce mistakes.
12
6
2016-03-06
Example
13
Example
14
7
2016-03-06
15
16
8
2016-03-06
17
18
9
2016-03-06
19
List-types are
- Local
- Global
- Varlist: you should list variables
- Newlist: you should list the name of new variables
- Numlist: you should list numbers
20
10
2016-03-06
21
22
11
2016-03-06
Levelsof command
23
Example
24
12
2016-03-06
Example
Macroname. list
It can be command macroname
anything.
You assign it.
25
Example
26
13
2016-03-06
Example Alternatively, we
can use levelsof
command to specify
values in race
variable
27
Example
List can
contain
variables,
string, and
numeric.
28
14
2016-03-06
Example
Loops execute
commands
from the top to
the bottom,
and execute
them again
from the
beginning.
29
Example
30
15
2016-03-06
Example
31
Example
32
16
2016-03-06
Example
33
Example
34
17
2016-03-06
Example
35
Example
36
18
2016-03-06
Example
Example
19
2016-03-06
Example
Same with local
irow = `irow’ + 1
40
20
2016-03-06
Example
41
21
2016-03-06
Example
43
Example
44
22
2016-03-06
Sources
StataCorp. Stata Programming Reference Manual Release 14.
College Station, TX: Stata Press.
Long, J. Scott. 2009. The Workflow of Data Analysis Using
Stata. College Station, TX: Stata Press.
Macros. http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm
“B] macros”.
http://pierrefrancois.wifeo.com/documents/Intro-Stata---LSE-
III.pdf
Tempfiles. http://www.stata.com/statalist/archive/2004-
01/msg00542.html
45
46
23
2016-03-06
47
24