Index Loop (DEFINE-!ENDDEFINE command)
The syntax of an index loop is as follows:
!DO !var = start !TO finish [ !BY step ]
statements
!BREAK
!DOEND
- The indexing variable is !var and must begin with an exclamation point.
- The start, finish, and step values must be numbers or expressions that evaluate to numbers.
- The loop begins at the start value and continues
until it reaches the finish value (unless a
!BREAK
statement is encountered). The step value is optional and can be used to specify a subset of iterations. If start is set to 1, finish to 10, and step to 3, the loop will be executed four times with the index variable assigned values 1, 4, 7, and 10. - The statements can be any valid commands or macro
keywords.
!DOEND
specifies the end of the loop. -
!BREAK
is an optional specification. It can be used in conjunction with conditional processing to exit the loop.
Example
DEFINE macdef (arg1 = !TOKENS(1)
/arg2 = !TOKENS(1))
!DO !i = !arg1 !TO !arg2.
frequencies variables = !CONCAT(var,!i).
!DOEND
!ENDDEFINE.
macdef arg1 = 1 arg2 = 3.
- The variable !i is initially assigned the value 1 (
arg1
) and is incremented until it equals 3 (arg2
), at which point the loop ends. - The first loop concatenates
var
and the value for !I, which is 1 in the first loop. The second loop concatenatesvar
and 2, and the third concatenatesvar
and 3. The result is thatFREQUENCIES
is executed three times, with variables VAR1, VAR2, and VAR3, respectively.