Thomas Mahaffey, Jr. Business Library
L001D Mendoza College of Business
University of Notre Dame
Notre Dame, IN 46556
Unless you are doing something for which a Stata command does not already exist, it is entirely possible to use Stata without ever coding a loop. That being said, judicious use of loops can make life easier for you. This tutorial introduces you to the use of loops.
foreach is used to loop through essentially a list of words. Load the example dataset auto.dta using the sysuse command:
sysuse auto, clear
Suppose you want to rename the variables price and mpg to price_78 and mpg_78 respectively. You could of course type the rename command as many times as you need (in this case it would be twice), but you can automate this task using a foreach loop:
foreach v in price mpg {
rename `v' `v'_78
}
The utility of looping becomes more obvious if we suppose that you need to rename ALL the variables in this dataset. If there are 100 variables, entering the rename command 100 times quickly becomes tedious. Here's how to add the suffix "_78" to all the variables in this dataset using a loop:
foreach v in * {
rename `v' `v'_78
}
forvalues is used to loop over consecutive values. For example, suppose you want to count the number of observations/rows in auto.dta.
local counter = 0
local N = _N
forvalues i = 1 / `N' {
local counter = `counter' + 1
}
display `counter'
Note: this is exactly what's done by the count command.
while executes the code within the braces as long as the expression that it's evaluating is true. Here's Example 2 redone using a while loop.
local counter = 0
local N = _N
while `counter' < `N' {
local counter = `counter' + 1
}
display `counter'