Examples (REPEATING DATA command)

Basic Example

* Build a file with each case representing one vehicle and
  spread information about the household to each case.
 
INPUT PROGRAM.
DATA LIST / SEQNUM 2-4 NUMPERS 6-7 NUMVEH 9-10.
REPEATING DATA STARTS=12 /OCCURS=NUMVEH
 /DATA=MAKE 1-8 (A) MODEL 9 (A) NUMCYL 10.
END INPUT PROGRAM.
 
BEGIN DATA
1001 02 02 FORD    T8PONTIAC C6
1002 04 01 CHEVY   C4
1003 02 03 CADILAC C8FORD    T6VW      C4
END DATA.
LIST.
  • Data are extracted from a file representing household records. Each input case is recorded on a single record; there are no continuation records.
  • The total number of persons living in the house and number of vehicles owned by the household is recorded on each record. The first field of numbers (columns 1–4) for each record is an identification number unique to each record. The next two fields of numbers are number of persons in household and number of vehicles. The remainder of the record contains repeating groups of information about each vehicle: the make of vehicle, model, and number of cylinders.
  • INPUT PROGRAM indicates the beginning of the input program and END INPUT PROGRAM indicates the end of the input program.
  • DATA LIST reads the variables from the household portion of the record. All fixed-format variables are defined on DATA LIST.
  • REPEATING DATA reads the information from the repeating groups and builds the output cases. Repeating groups start in column 12. The number of repeating groups for each input case is given by the value of variable NUMVEH. Three variables are defined for each repeating group: MAKE, MODEL, and NUMCYL.
  • The first input record contains two repeating groups, producing two output cases in the active dataset. One output case is built from the second input record which contains information on one vehicle, and three output cases are built from the third record. The values of the fixed-format variables defined on DATA LIST are spread to every new case built in the active dataset. Six cases result, as shown below.
SEQNUM NUMPERS NUMVEH MAKE     MODEL NUMCYL
 
    1      2      2   FORD     T        8
    1      2      2   PONTIAC  C        6
    2      4      1   CHEVY    C        4
    3      2      3   CADILAC  C        8
    3      2      3   FORD     T        6
    3      2      3   VW       C        4
 
 NUMBER OF CASES READ =       6    NUMBER OF CASES LISTED =       6

Using REPEATING DATA With Mixed File Types

* Use REPEATING DATA with FILE TYPE MIXED: read only type 3 records. 

FILE TYPE  MIXED RECORD=#SEQNUM 2-4.
RECORD TYPE 003.
REPEATING DATA STARTS=12 /OCCURS=3
 /DATA=MAKE 1-8(A) MODEL 9(A) NUMCYL 10.
END FILE.
END FILE TYPE.
 
BEGIN DATA
1001 02 02 FORD    T8PONTIAC C6
1002 04 01 CHEVY   C4
1003 02 03 CADILAC C8FORD    T6VW      C4
END DATA.
LIST.
  • The task in this example is to read only the repeating data for records with value 003 for variable #SEQNUM.
  • REPEATING DATA is used within a FILE TYPE structure, which specifies a mixed file type. The record identification variable #SEQNUM is located in columns 2–4.
  • RECORD TYPE specifies that only records with value 003 for #SEQNUM are copied into the active dataset. All other records are skipped.
  • REPEATING DATA indicates that the repeating groups start in column 12. The OCCURS subcommand indicates there are three repeating groups on each input case, and the DATA subcommand specifies names, locations, and formats for the variables in the repeating groups.
  • The DATA LIST command is not required in this example, since none of the information on the input case is being spread to the output cases. However, if there were multiple input cases with value 003 for #SEQNUM and they did not all have three repeating groups, DATA LIST would be required to define a variable whose value for each input case indicated the number of repeating groups for that case. This variable would then be specified on the OCCURS subcommand.

Using Transformations With REPEATING DATA

INPUT PROGRAM.
DATA LIST / PARENTID 1 DATE 3-6 NCHILD 8.
REPEATING DATA STARTS=9 /OCCURS=NCHILD
 /DATA=BIRTHDAY 2-5 VACDATE 7-10.
END INPUT PROGRAM.
 
COMPUTE AGE=DATE - BIRTHDAY.
COMPUTE VACAGE=VACDATE - BIRTHDAY.
 
DO IF PARENTID NE LAG(PARENTID,1) OR $CASENUM EQ 1.
COMPUTE CHILD=1.
ELSE.
COMPUTE CHILD=LAG(CHILD,1)+1.
END IF.
FORMAT AGE VACAGE CHILD (F2).
 
BEGIN DATA
1 1987 2 1981 1983 1982 1984
2 1988 1 1979 1984
3 1988 3 1978 1981 1981 1986 1983 1986
4 1988 1 1984 1987
END DATA.
LIST.
  • Data are from a file that contains information on parents within a school district. Each input case is recorded on a single record; there are no continuation records.
  • Each record identifies the parents by a number and indicates how many children they have. The repeating groups give the year of birth and year of vaccination for each child.
  • REPEATING DATA indicates that the repeating groups begin in column 9. The value of NCHILD indicates how many repeating groups there are for each record.
  • The first two COMPUTE commands compute the age for each child and age at vaccination. These transformation commands are specified outside the input program.
  • Because the repeating groups do not have descriptive values, the DO IF structure computes variable CHILD to distinguish between the first-born child, second-born child, and so forth. The value for CHILD will be 1 for the first-born, 2 for the second-born, and so forth. The LIST output is shown below.
PARENTID DATE NCHILD BIRTHDAY VACDATE AGE VACAGE CHILD
 
    1    1987    2     1981     1983    6    2      1
    1    1987    2     1982     1984    5    2      2
    2    1988    1     1979     1984    9    5      1
    3    1988    3     1978     1981   10    3      1
    3    1988    3     1981     1986    7    5      2
    3    1988    3     1983     1986    5    3      3
    4    1988    1     1984     1987    4    3      1
 
NUMBER OF CASES READ =       7    NUMBER OF CASES LISTED =       7