help forvalues
-------------------------------------------------------------------------------
Title
[P] forvalues -- Loop over consecutive values
Syntax
forvalues lname = range {
commands referring to `lname'
}
where range is
#1(#d)#2 meaning #1 to #2 in steps of #d
#1/#2 meaning #1 to #2 in steps of 1
#1 #t to #2 meaning #1 to #2 in steps of #t - #1
#1 #t : #2 meaning #1 to #2 in steps of #t - #1
The loop is executed as long as calculated values of `lname' are < #2,
assuming that #d > 0.
Braces must be specified with forvalues, and
1. the open brace must appear on the same line as forvalues;
2. nothing may follow the open brace except, of course, comments;
the first command to be executed must appear on a new line;
3. the close brace must appear on a line by itself.
Description
forvalues repeatedly sets local macro lname to each element of range and
executes the commands enclosed in braces. The loop is executed zero or
more times.
forvalues is the fastest way to execute a block of code for different
numeric values of lname.
Examples
Generate 100 uniform random variables named x1, x2, ..., x100.
. forvalues i = 1(1)100 {
2. generate x`i' = runiform()
3. }
For variables var5, var6, ..., var13 output the number of observations
greater than 10.
. forval num = 5/13 {
2. count if var`num' > 10
3. }
Produce individual summarize commands for variables x5, x10, ..., x300.
. forvalues k = 5 10 to 300 {
2. summarize x`k'
3. }
A loop over noninteger values that includes more than one command.
. forval x = 31.3 31.6 : 38 {
2. count if var1 < `x' & var2 < `x'
3. summarize myvar if var1 < `x'
4. }
Also see
Manual: [P] forvalues
Help: [P] continue, [P] foreach, [P] while; [P] if