BY Keyword (LOOP-END LOOP command)

By default, the program increases the indexing variable by 1 for each iteration. The keyword BY overrides this increment.

  • The increment value can be a numeric expression and can therefore be non-integer or negative. Zero causes a warning and results in a zero-trip loop.
  • If the initial value is greater than the terminal value and the increment is positive, the loop is never entered. #I=1 TO 0 BY 2 results in a zero-trip loop.
  • If the initial value is less than the terminal value and the increment is negative, the loop is never entered. #I=1 TO 2 BY –1 also results in a zero-trip loop.
  • Order is unimportant: 2 BY 2 TO 10 is equivalent to 2 TO 10 BY 2.

Example

LOOP #I=2 TO 10 BY 2. /*Loop five times by 2'S
COMPUTE X=X+1.
END LOOP.
  • The scratch variable #I starts at 2 and increases by 2 for each of five iterations until it equals 10 for the last iteration.

Example

LOOP #I=1 TO Y BY Z. /*Loop to Y incrementing by Z
COMPUTE X=X+1.
END LOOP.
  • The loop is executed once for a case with Y equal to 2 and Z equal to 2 but twice for a case with Y equal to 3 and Z equal to 2.

Example

* Repeating data using LOOP.
 
INPUT PROGRAM.
DATA LIST      NOTABLE/ ORDER 1-4(N) #BKINFO 6-71(A).
LEAVE ORDER.
LOOP           #I = 1 TO 66 BY 6 IF SUBSTR(#BKINFO,#I,6) <> ' '.
+  REREAD         COLUMN = #I+5.
+  DATA LIST      NOTABLE/ ISBN 1-3(N) QUANTITY 4-5.
+  END CASE.
END LOOP.
END INPUT PROGRAM.
SORT CASES     BY ISBN ORDER.
BEGIN DATA
1045 182 2 155 1 134 1 153 5
1046 155 3 153 5 163 1
1047 161 5 182 2 163 4 186 6
1048 186 2
1049 155 2 163 2 153 2 074 1 161 1
END DATA.
 
DO IF          $CASENUM = 1.
+  PRINT EJECT    /'Order' 1  'ISBN' 7  'Quantity' 13.
END IF.
PRINT          /ORDER 2-5(N) ISBN 8-10(N) QUANTITY 13-17.
EXECUTE.
  • This example uses LOOP to simulate a REPEATING DATA command.
  • DATA LIST specifies the scratch variable #BKINFO as a string variable (format A) to allow blanks in the data.
  • LOOP is executed if the SUBSTR function returns anything other than a blank or null value. SUBSTR returns a six-character substring of #BKINFO, beginning with the character in the position specified by the value of the indexing variable #I. As specified on the indexing clause, #I begins with a value of 1 and is increased by 6 for each iteration of LOOP, up to a maximum #I value of 61 (1 + 10 × 6 = 61). The next iteration would exceed the maximum #I value (1 + 11 × 6 = 67).