Examples (INPUT PROGRAM-END INPUT PROGRAM command)

Select Cases with an Input Program

INPUT PROGRAM.
DATA LIST FILE=PRICES /YEAR 1-4 QUARTER 6 PRICE 8-12(2).

DO IF (YEAR GE 1881).  /*Stop reading when reaching 1881
END FILE.
END IF.
END INPUT PROGRAM.

LIST.
  • The input program is defined between the INPUT PROGRAM and END INPUT PROGRAM commands.
  • This example assumes that data records are entered chronologically by year. The DO IF-END IF structure specifies an end of file when the first case with a value of 1881 or later for YEAR is reached.
  • LIST executes the input program and lists cases in the active dataset. The case that causes the end of the file is not included in the active dataset generated by the input program.
  • As an alternative to this input program, you can use N OF CASES to select cases if you know the exact number of cases. Another alternative is to use SELECT IF to select cases before 1881, but then the program would unnecessarily read the entire input file.

Skip the First n Records in a File

INPUT PROGRAM.
NUMERIC        #INIT.        
DO IF          NOT (#INIT).
+  LOOP           #I = 1 TO 5.
+     DATA LIST      NOTABLE/.   /* No data - just skip record
+  END LOOP.
+  COMPUTE        #INIT = 1.
END IF.
DATA LIST      NOTABLE/ X 1.
END INPUT PROGRAM.
 
BEGIN DATA
A                           /* The first 5 records are skipped
B
C
D
E
1
2
3
4
5
END DATA.
LIST.
  • NUMERIC declares the scratch variable #INIT, which is initialized to system-missing.
  • The DO IF structure is executed as long as #INIT does not equal 1.
  • LOOP is executed five times. Within the loop, DATA LIST is specified without variable names, causing the program to read records in the data file without copying them into the active dataset. LOOP is executed five times, so the program reads five records in this manner. END LOOP terminates this loop.
  • COMPUTE creates the scratch variable #INIT and sets it equal to 1. The DO IF structure is therefore not executed again.
  • END IF terminates the DO IF structure.
  • The second DATA LIST specifies numeric variable X, which is located in column 1 of each record. Because the program has already read five records, the first value for X that is copied into the active dataset is read from record 6.