Loop control variants

Count loop

The simplest form of loop control is a direct specification of the number of times the loop should be repeated. We refer to this as a "count loop". The number of repetitions may be a numerical constant, as in loop 1000, or may be read from a variable, as in loop replics.

In the case where the loop count is given by a variable, say replics, in concept replics is an integer scalar. If it is in fact a series, its first value is read. If the value is not integral, it is converted to an integer by truncation. Note that replics is evaluated only once, when the loop is initially compiled.

While loop

A second sort of control expression takes the form of the keyword while followed by an inequality: the left-hand term should be the name of a predefined variable; the right-hand side may be either a numerical constant or the name of another predefined variable. For example,

loop while essdiff > .00001

Execution of the commands within the loop will continue so long as the specified condition evaluates as true. If right-hand term of the inequality is a variable, it is evaluated at the top of the loop at each iteration.

Index loop

A third form of loop control uses the special internal index variable i. In this case you specify starting and ending values for i, which is incremented by one each time round the loop. The syntax looks like this: loop i=1..20.

The index variable may be used within the loop body in one or both of two ways: you can access the value of i (see Example 10-4) or you can use its string representation, $i (see Example 10-5).

The starting and ending values for the index can be given in numerical form, or by reference to predefined variables. In the latter case the variables are evaluated once, when the loop is set up. In addition, with time series data you can give the starting and ending values in the form of dates, as in loop i=1950:1..1999:4.

For each loop

The fourth form of loop control also uses the internal variable i, but in this case the variable ranges over a specified list of strings. The loop is executed once for each string in the list. This can be useful for performing repetitive operations on a list of variables. Here is an example of the syntax:


	loop foreach i peach pear plum
	   print "$i"
	endloop

This loop will execute three times, printing out "peach", "pear" and "plum" on the respective iterations.

If you wish to loop across a list of variables that are contiguous in the dataset, you can give the names of the first and last variables in the list, separated by "..", rather than having to type all the names. For example, say we have 50 variables AK, AL, …, WY, containing income levels for the states of the US. To run a regression of income on time for each of the states we could do:


	genr time
	loop foreach i AL..WY
	   ols $i const time
	endloop

For loop

The final form of loop control uses a simplified version of the for statement in the C programming language. The expression is composed of three parts, separated by semicolons. The first part specifies an initial condition, expressed in terms of a control variable; the second part gives a continuation condition (in terms of the same control variable); and the third part specifies an increment (or decrement) for the control variable, to be applied each time round the loop. The entire expression is enclosed in parentheses. For example:

loop for (r=0.01; r<.991; r+=.01)

In this example the variable r will take on the values 0.01, 0.02, …, 0.99 across the 99 iterations. Note that due to the finite precision of floating point arithmetic on computers it may be necessary to use a continuation condition such as the above, r<.991, rather than the more "natural" r<=.99. (Using double-precision numbers on an x86 processor, at the point where you would expect r to equal 0.99 it may in fact have value 0.990000000000001.)

To expand on the rules for the three components of the control expression: (1) the initial condition must take the form LHS1 = RHS1. RHS1 must be a numeric constant or a predefined variable. If the LHS1 variable does not exist already, it is automatically created. (2) The continuation condition must be of the form LHS1 op RHS2, where op can be <, >, <= or >= and RHS2 must be a numeric constant or a predefined variable. If RHS2 is a variable it is evaluated each time round the loop. (3) The increment or decrement expression must be of the form LHS1 += DELTA or LHS1 -= DELTA, where DELTA is a numeric constant or a predefined variable. If DELTA is a variable, it is evaluated only once, when the loop is set up.