4.2.1 Looping Over Sequences of Numbers (forvalue)
The basic looping command takes the form
forvalues number = sequence {
... body of loop using `number' ...
}
Here forvalues is a keyword, number is the name of a local macro that will be set to each number in the sequence, and sequence is a range of values which can have the form
min/max to indicate a sequence of numbers from min to max in steps of one, for example 1/3 yields 1, 2 and 3, or
first(step)last which yields a sequence from first to last in steps of size step. For example 15(5)50 yields 15,20,25,30,35,40,45 and 50.
(There are two other ways of specifying the second type of sequence, but I find the one listed here the clearest, see help forvalues for the alternatives.)
The opening left brace must be the last thing on the first line (other than comments), and the loop must be closed by a matching right brace on a line all by itself. The loop is executed once for each value in the sequence with your local macro number (or whatever you called it) holding the value.
4.2.2 Looping Over Elements in a List (foreach)
The second looping command is foreach and comes in six flavors, dealing with different types of lists. I will start with the generic list:
foreach item in a-list-of-things {
... body of loop using `item' ...
}
Here foreach is a keyword, item is a local macro name of your own choosing, in is another keyword, and what comes after is a list of blank-separated words. Try this example
foreach animal in cats and dogs {
display "`animal'"
}
This loop will print "cats", "and", and "dogs", as the local macro animal is set to each of the words in the list. Stata doesn't know "and" is not an animal, but even if it did, it wouldn't care because the list is generic.
If you wanted to loop over an irregular sequence of numbers for example you needed to do something with the Coale-Demeny regional model life tables for levels 2, 6 and 12-- you could write
foreach level in 2 6 12 {
... do something with `level' ...
}
That's it. This is probably all you need to know about looping.
4.2.4 Looping for a While (While)
In common with many programming languages, Stata also has a while loop, which has the following structure
while condition {
... do something ...
}
where condition is an expression. The loop executes as long as the condition is true (nonzero). Usually something happens inside the loop to make the condition false, otherwise the code would run forever.
A typical use of while is in iterative estimation procedures, where you may loop while the difference in successive estimates exceeds a predefined tolerance. Usually an iteration count is used to detect lack of convergence.
The continue [,break] command allows breaking out of any loop, including while, forvalues and foreach. The command stops the current iteration and continues with the next, unless break is specified in which case it exits the loop. 4.2.5 Conditional Execution (if)
Stata also has an if programming command, not to be confused with the ifqualifier that can be used to restrict any command to a subset of the data, as in summarize mpg if foreign. The ifcommand has the following structure
if expression {
... commands to be executed if expression is true ...
}
else {
... optional block to be executed if expression is false ...
}
Here if and the optional else are keywords, type help exp for an explanation of expressions. The opening brace { must be the last thing on a line (other than comments) and the closing brace } must be on a new line by itself.
If the if or else parts consist of a single command they can go on the same line without braces, as in if expression command. But if expression { command } is not legal. You could use the braces by spreading the code into three lines and this often improves readability of the code.
So here we have a silly loop where we break out after five of the possible ten iterations:
forvalues iter=1/10 {
display "`iter'"
if `iter' >= 5 continue, break
}
And with that, we break out of looping.