Nothing Special   »   [go: up one dir, main page]

Intro To Loops - Local & Global

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

2016-03-06

Introduction to
Macro and Loop in Stata
SOC 561
Programming for the Social Sciences
Hyungjun Suh
Mar. 7. 2016

Overview

Macros (local and global macros)


Loops (foreach and forvalues)
Tempvar
Tempfile

1
2016-03-06

Macro: What is it and what does it do?

Macros are abbreviations for a string of characters or a


number (Long 2009:83). They also represent expressions,
so as to make programming efficient.

Local and Global Macro

Local macros are ephemeral macros. When you work


with do-file editor, local macros are valid only in a single
execution of commands in do-files or ado-files. Yet, when
you work interactively, they persist until you delete them
or the session is ended.
Global macros are persisting macros. Once defined
whether in do-file editor or in interactive command
window, they persist until you delete them or the session
is ended.

2
2016-03-06

Local and Global Macro

In general, using local macros are recommended. This is


because when you work with many do-files or ado-files at
the same time, global macros can cause conflicts across
do-files or ado-files, which is error-prone.

Syntax of Macro Assignment

local macroname “string”


local macroname = expression
global macroname “string”
global macroname = expression
6

3
2016-03-06

Using Macros

back quote single quote

local : `macroname’
global: $macroname

Example It works without


quotation marks

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

Macros can be used when you want to specify long


options as well (Long 2009:88-89).
e.g.) local opt_tab “cell miss nolabel chi2”
tabulate south smsa, `opt_tab’
local opt_linF “lpattern(solid) lwidth(medthick)
lcolor(black) msymbol(i)”
graph twoway (connected wage hours, `opt_linF’)

14

7
2016-03-06

Loop: What is it and what does it do?

Loops refer to commands which execute a group of


commands multiple times (Long 2009:92). Think about
the situation where you should replace some values of
1,000 variables the same way. You can do it manually, but
it takes a lot of time and it is error-prone. Also, it can be
the case that you need to do it again for some reasons,
like false calculation or different theoretical
consideration. Loops are useful in situations above.

15

Loop: What is it and what does it do?

Loops are useful in following tasks (Long 2009:95-96):


- Listing variable and value labels
- Creating interaction variables
- Fitting models with alternative measures of education
- Recoding multiple variables the same way
- Creating a macro that holds accumulated information
- Retrieving information returned by Stata

16

8
2016-03-06

foreach and forvalues

Foreach is a more general loop. String, numeric, and


variables are allowed as list, and lists do not have to have
a pattern.
Forvalues is a more specific loop. Only numeric is allowed
as lists, and lists should have a clear pattern.

17

Syntax of foreach (in) command

foreach macroname in list {


commands referring to `macroname’
}

18

9
2016-03-06

Syntax of foreach (of) command

foreach macroname of list-type {


commands referring to `macroname’
}

19

Syntax of foreach (of) command

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

Syntax of forvalues command

forvalues macroname = range {


commands referring to `macroname’
}

21

Syntax of forvalues command

Range can have three forms (Long 2009:95):


- #1(#d)#2: from #1 to #2 in steps of #d
e.g.) 1(2)10 -> 1, 3, 5, 7, 9
- #1/#2: fron #1 to #2 in steps of 1
e.g.) 1/10 -> 1, 2, 3, 4, …, 10
- #1 #t to #2: from #1 to #2 in steps of (#t-#1)
e.g.) 1 3 to 10 -> 1, 3, 5, 7, 9

22

11
2016-03-06

Levelsof command

Levelsof command identify all values in a variable and put


those values in a macro
(http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm).
Syntax: levelsof variable, local (macro)
It is useful when the variable of interest has many values,
like hundreds or thousands values.

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

This way, you can check as many variables’


name and label as what you want at once.

32

16
2016-03-06

Example

You can quickly make variables with a loop.

33

Example

Results are omitted. A researcher can see difference effects


of grade on wage by race quickly. For example, if you want
to see different effects of a certain variable on the
dependent variable in 100 countries, loops would be
helpful.

34

17
2016-03-06

Example

See the next slide


for the explanation

35

Example

This command tries to get mean wages by respondents’ age and


assign the derived value to each respondent.
Alternatively, we can consider the following command:
generate meanwage=.
summarize wage if age==34
replace meanwage=r(mean) if age==34
(same commands for all age values from 35 to 46)
Forvalues loop is more efficient than the commands above.

36

18
2016-03-06

Example

If you want to assign numbers to each result, using


counters would be helpful.
37

Example

Loops allow a researcher to save results for future


analysis. Suppose we want to see different effect of
grade on wage by race. First, I make a matrix as
above in which results will be saved.
38

19
2016-03-06

Example
Same with local
irow = `irow’ + 1

In this way, we can make a


graph of coefficients standard
errors or further analysis.
39

Tempvar: What is it and what does it do?

Tempvar creates a temporary variable which is valid only


in a single execution of commands in do-files or ado-files.
Alternatively, we can make variables and then erase
them manually. Yet, using tempvar is more efficient.
Syntax step1: tempvar var1 (declaring variable name)
Step2: commands referring to `var1’

40

20
2016-03-06

Example

Execution ends here.

‘myvar’ has been deleted


automatically.

41

Tempfile: What is it and what does it do?

Tempfile creates a temporary dataset which is valid only


in a single execution of commands in do-files or ado-files.
Alternatively, we can save a dataset and use it, and erase
it. Compared to this, tempfile is more efficient.
Or, one can consider preserve/restore commands, but
preserve command stores only one dataset.
Syntax step1: tempfile data1 (declaring data name)
Step2: commands referring to `data1’
42

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

Questions and Comments to


suhhyungjun@email.arizona.edu

46

23
2016-03-06

47

24

You might also like