List-Processing Loop (DEFINE-!ENDDEFINE command)
The syntax of a list-processing loop is as follows:
!DO !var !IN (list)
statements
!BREAK
!DOEND
- The !DO and !DOEND statements begin and end the loop. !BREAK is used to exit the loop.
- The !IN function requires one argument, which must be a list of items. The number of items on the list determines the number of iterations. At each iteration, the index variable !var is set to each item on the list.
- The list can be any expression, although it is usually a string. Only one list can be specified in each list-processing loop.
Example
DEFINE macdef (!POS !CHAREND('/'))
!DO !i !IN (!1)
frequencies variables = !i.
!DOEND
!ENDDEFINE.
macdef VAR1 VAR2 VAR3 /.
- The macro call assigns three variables, VAR1, VAR2, and VAR3, to the positional argument !1. Thus, the loop completes three iterations.
- In the first iteration, !i is set to value VAR1. In the second and third iterations, !I is set to VAR2 and VAR3, respectively. Thus, FREQUENCIES is executed three times, respectively with VAR1, VAR2, and VAR3.
Example
DEFINE macdef (!POS !CHAREND('/'))
!DO !i !IN (!1)
sort cases by !i.
report var = earnings
/break = !i
/summary = mean.
!DOEND
!ENDDEFINE.
macdef SALESMAN REGION MONTH /.
- The positional argument !1 is assigned the three variables SALESMAN, REGION, and MONTH. The loop is executed three times and the index variable !i is set to each of the variables in succession. The macro creates three reports.