Examples (END CASE command)

Restructuring a data file to make each data item a single case

INPUT PROGRAM.
DATA LIST /#X1 TO #X3 (3(F1,1X)).
 
VECTOR V=#X1 TO #X3.
 
LOOP #I=1 TO 3.
- COMPUTE X=V(#I).
- END CASE.
END LOOP.
END INPUT PROGRAM.
 
BEGIN DATA
2 1 1
3 5 1
END DATA.
FORMAT X(F1.0).
PRINT / X.
EXECUTE.
  • The input program encloses the commands that build cases from the input file. An input program is required because END CASE is used to create multiple cases from single input records.
  • DATA LIST defines three variables. In the format specification, the number 3 is a repetition factor that repeats the format in parentheses three times, once for each variable. The specified format is F1 and the 1X specification skips one column.
  • VECTOR creates the vector V with the original scratch variables as its three elements. The indexing expression on the LOOP command increments the variable #I three times to control the number of iterations per input case and to provide the index for the vector V.
  • COMPUTE sets X equal to each of the scratch variables. END CASE tells the program to build a case. Thus, the first loop (for the first case) sets X equal to the first element of vector V. Since V(1) references #X1, and #X1 is 2, the value of X is 2. Variable X is then formatted and printed before control returns to the command END LOOP. The loop continues, since indexing is not complete. Thus, the program then sets X to #X2, which is 1, builds the second case, and passes it to the FORMAT and PRINT commands. After the third iteration, which sets X equal to 1, the program formats and prints the case and terminates the loop. Since the end of the file has not been encountered, END INPUT PROGRAM passes control to the first command in the input program, DATA LIST, to read the next input case. After the second loop, however, the program encounters END DATA and completes building the active dataset.
  • The six new cases are shown below.
Figure 1. Outcome for multiple cases read from a single case
2
1
1
3
5
1

Restructuring a data file to create a separate case for each book order

INPUT PROGRAM.
DATA LIST  /ORDER 1-4 #X1 TO #X22 (1X,11(F3.0,F2.0,1X)).

LEAVE ORDER.
VECTOR BOOKS=#X1 TO #X22.

LOOP #I=1 TO 21 BY 2 IF NOT SYSMIS(BOOKS(#I)).
- COMPUTE ISBN=BOOKS(#I).
- COMPUTE QUANTITY=BOOKS(#I+1).
- END CASE.
END LOOP.
END INPUT PROGRAM.
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.

SORT CASES ISBN.
DO IF $CASENUM EQ 1.
- PRINT EJECT /'Order ISBN Quantity'.
- PRINT SPACE.
END IF.
 
FORMATS ISBN (F3)/ QUANTITY (F2).
PRINT /' ' ORDER ' ' ISBN '  ' QUANTITY.
 
EXECUTE.
  • Data are extracted from a file whose records store values for an invoice number and a series of book codes and quantities ordered. For example, invoice 1045 is for four different titles and a total of nine books: two copies of book 182, one copy each of 155 and 134, and five copies of book 153. The task is to break each individual book order into a record, preserving the order number on each new case.
  • The input program encloses the commands that build cases from the input file. They are required because the END CASE command is used to create multiple cases from single input records.
  • DATA LIST specifies ORDER as a permanent variable and defines 22 scratch variables to hold the book numbers and quantities (this is the maximum number of numbers and quantities that will fit in 72 columns). In the format specification, the first element skips one space after the value for the variable ORDER. The number 11 repeats the formats that follow it 11 times: once for each book number and quantity pair. The specified format is F3.0 for book numbers and F2.0 for quantities. The 1X specification skips one column after each quantity value.
  • LEAVE preserves the value of the variable ORDER across the new cases to be generated.
  • VECTOR sets up the vector BOOKS with the 22 scratch variables as its elements. The first element is #X1, the second is #X2, and so on.
  • If the element for the vector BOOKS is not system-missing, LOOP initiates the loop structure that moves through the vector BOOKS, picking off the book numbers and quantities. The indexing clause initiates the indexing variable #I at 1, to be increased by 2 to a maximum of 21.
  • The first COMPUTE command sets the variable ISBN equal to the element in the vector BOOKS indexed by #I, which is the current book number. The second COMPUTE sets the variable QUANTITY equal to the next element in the vector BOOKS, #I +1, which is the quantity associated with the book number in BOOKS(#I).
  • END CASE tells the program to write out a case with the current values of the three variables: ORDER, ISBN, and QUANTITY.
  • END LOOP terminates the loop structure and control is returned to the LOOP command, where #I is increased by 2 and looping continues until the entire input case is read or until #I exceeds the maximum value of 21.
  • SORT CASES sorts the new cases by book number.
  • The DO IF structure encloses a PRINT EJECT command and a PRINT SPACE command to set up titles for the output.
  • FORMATS establishes dictionary formats for the new variables ISBN and QUANTITY. PRINT displays the new cases.
  • EXECUTE runs the commands. The output is shown below.
Figure 2. PRINT output showing new cases
Order ISBN Quantity
 1049   74    1
 1045  134    1
 1045  153    5
 1046  153    5
 1049  153    2
 1045  155    1
 1046  155    3
 1049  155    2
 1047  161    5
 1049  161    1
 1046  163    1
 1047  163    4
 1049  163    2
 1045  182    2
 1047  182    2
 1047  186    6
 1048  186    2

Create variable that approximates a log-normal distribution

SET FORMAT=F8.0.

INPUT PROGRAM.
LOOP I=1 TO 1000.
+ COMPUTE SCORE=EXP(NORMAL(1)).
+ END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.

FREQUENCIES VARIABLES=SCORE /FORMAT=NOTABLE /HISTOGRAM
 /PERCENTILES=1 10 20 30 40 50 60 70 80 90 99
 /STATISTICS=ALL.
  • The input program creates 1,000 cases with a single variable SCORE. Values for SCORE approximate a log-normal distribution.

Restructure a data file to create a separate case for each individual

INPUT PROGRAM. 
DATA LIST  /#RECS 1 HEAD1 HEAD2 3-4(A).    /*Read header info
LEAVE  HEAD1 HEAD2.

LOOP  #I=1 TO #RECS.
DATA LIST  /INDIV 1-2(1).                  /*Read individual info
PRINT  /#RECS HEAD1 HEAD2 INDIV.
END CASE.                                  /*Create combined case
END LOOP.
END INPUT PROGRAM.
BEGIN DATA
1 AC
91
2 CC
35
43
0 XX
1 BA
34
3 BB
42
96
37
END DATA.
LIST.
  • Data are in a file with header records that indicate the type of record and the number of individual records that follow. The number of records following each header record varies. For example, the 1 in the first column of the first header record (AC) says that only one individual record (91) follows. The 2 in the first column of the second header record (CC) says that two individual records (35 and 43) follow. The next header record has no individual records, indicated by the 0 in column 1, and so on.
  • The first DATA LIST reads the expected number of individual records for each header record into temporary variable #RECS. #RECS is then used as the terminal value in the indexing variable to read the correct number of individual records using the second DATA LIST.
  • The variables HEAD1 and HEAD2 contain the information in columns 3 and 4, respectively, in the header records. The LEAVE command retains HEAD1 and HEAD2 so that this information can be spread to the individual records.
  • The variable INDIV is the information from the individual record. INDIV is combined with #RECS, HEAD1, and HEAD2 to create the new case. Notice in the output from the PRINT command below that no case is created for the header record with 0 for #RECS.
  • END CASE passes each case out of the input program to the LIST command. Without END CASE, the PRINT command would still display the cases because it is inside the loop. However, only one (the last) case per header record would pass out of the input program. The outcome for LIST will be quite different.
Figure 3. PRINT output
1 A C 9.1
2 C C 3.5
2 C C 4.3
1 B A 3.4
3 B B 4.2
3 B B 9.6
3 B B 3.7
Figure 4. LIST output when END CASE is specified
HEAD1 HEAD2 INDIV

A     C      9.1
C     C      3.5
C     C      4.3
B     A      3.4
B     B      4.2
B     B      9.6
B     B      3.7
Figure 5. LIST output when END CASE is not specified
HEAD1 HEAD2 INDIV

A     C      9.1
C     C      4.3
X     X       .
B     A      3.4
B     B      3.7