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 PROGRAMsignals the beginning of data definition. -
DATA LISTdefines 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 DATAspecifies that the input file contains repeating data.STARTSindicates that repeating data begin in column 6.OCCURSsets the number of repeating groups on a record equal to the value of #NUM. -
DATAdefines 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 PROGRAMsignals the end of data definition. - Data appear between
BEGIN DATAandEND DATA. - The following figure shows the output from
LIST.
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 PROGRAMsignals the beginning of data definition. -
DATA LISTreads 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). -
LOOPandEND LOOPdefine 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 LISTreads STUDENT and SCORE in a classroom record. The program begins reading these variables in the first record, in the starting column specified byREREAD 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 CASEcreates a new case for each repeating group. -
REREADcausesDATA LISTto read repeating data groups in the same record in which it last read CLASS. WithoutREREAD, each execution ofDATA LISTwould begin on a different record. -
LEAVEpreserves the value ofCLASSacross the repeating data groups on a record. Thus, the same class number is read for each student on a classroom record. -
INPUT PROGRAMsignals the beginning of data definition. -
BEGIN DATAandEND DATAindicate 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.