Varying Number of Repeating Groups

To use REPEATING DATA to define a file in which the number of repeating data groups varies across records, your data must contain a variable indicating the number of repeating data groups on a record. The following commands define such a file:

INPUT PROGRAM. 
DATA LIST /  #NUM 1 CLASS 3-5.
REPEATING DATA STARTS=6 / OCCURS=#NUM 
 /DATA STUDENT 1-4 SCORE 5-8.
END INPUT PROGRAM. 

BEGIN DATA
3 101 182  12 134  53 199  30
2 102  99 112 200 150
1 103  15  87
END DATA.

LIST.
  • INPUT PROGRAM signals the beginning of data definition.
  • DATA LIST defines variables CLASS in columns 3 through 5 and #NUM, a scratch variable in column 1 that contains the number of repeating data groups in a record.
  • REPEATING DATA specifies that the input file contains repeating data. STARTS indicates that repeating data begin in column 6. OCCURS sets the number of repeating groups on a record equal to the value of #NUM.
  • DATA defines variables that are repeated. Since #NUM is 3 in the first and third records, the program reads three sets of STUDENT and SCORE variables in these records. STUDENT and SCORE are read twice in record 2.
  • END INPUT PROGRAM signals the end of data definition.
  • Data appear between BEGIN DATA and END DATA.
  • The following figure shows the output from LIST.
Figure 1. LIST output
CLASS STUDENT SCORE
 101     182     12
 101     134     53
 101     199     30
 102      99    112
 103      15     87

If your data file does not have a variable indicating the number of repeating data groups per record, you can use the LOOP and REREAD commands to read the data, as in:

INPUT PROGRAM.
DATA LIST /   CLASS 3-5 #ALL 6-29 (A). 
LEAVE CLASS.

LOOP #I = 1 TO 17  BY 8 IF SUBSTR(#ALL, #I, 8) NE ''.
-  REREAD COLUMN = #I + 5. 
-  DATA LIST / STUDENT 1-4 SCORE 5-8. 
-  END CASE.
END LOOP. 
END INPUT PROGRAM. 

BEGIN DATA
  101 182  12 134  53 199  30
  102  99 112 200 150
  103  15  87 
END DATA.

LIST.
  • INPUT PROGRAM signals the beginning of data definition.
  • DATA LIST reads CLASS and #ALL, a temporary string variable that contains all of the repeating data groups for a classroom record. The column specifications for #ALL (6 through 29) are wide enough to accommodate the classroom record with the most repeating data groups (record 1).
  • LOOP and END LOOP define an index loop. As the loop iterates, the program successively reads eight-character segments of #ALL, each of which contains a repeating data group or an empty field. The program reads the first eight characters of #ALL in the first iteration, the second eight characters in the second iteration, and so forth. The loop terminates when the program encounters an empty segment, which means that there are no more repeating data groups on a record.
  • In each iteration of the loop in which an #ALL segment is not empty, DATA LIST reads STUDENT and SCORE in a classroom record. The program begins reading these variables in the first record, in the starting column specified by REREAD COLUMN. For example, in the first iteration, the program reads STUDENT and SCORE beginning in column 6. In the second iteration, the program reads STUDENT and SCORE starting in column 14 of the same record. When all repeating groups have been read for a record, loop processing begins on the following record.
  • END CASE creates a new case for each repeating group.
  • REREAD causes DATA LIST to read repeating data groups in the same record in which it last read CLASS. Without REREAD, each execution of DATA LIST would begin on a different record.
  • LEAVE preserves the value of CLASS across the repeating data groups on a record. Thus, the same class number is read for each student on a classroom record.
  • INPUT PROGRAM signals the beginning of data definition.
  • BEGIN DATA and END DATA indicate that the data are inline. The data are identical to those in the previous example except that they do not contain a variable indicating the number of repeating groups per record.
  • These commands generate the same output as shown in the figure above.