INPUT and OUTPUT PROCEDURE

7.5 INPUT and OUTPUT PROCEDURE

The SORT statement above sorted all the records in the file into a new file. But if you wanted to produce a sorted file that only contained, for example, product numbers which begin with a '1', you would use an INPUT PROCEDURE.

The record FD might be:


FD UNSORTED-FILE. 01 UNSORTED-RECORD. 03 1ST-DIGIT-OF-CODE PIC 9. 03 PIC X(20).

The description gives the minimum detail required. Now some procedure division:


PROCEDURE DIVISION. SORT-SELECT. SORT WORK-FILE ON DESCENDING KEY PRODUCT-NO INPUT PROCEDURE SELECT-PROD-CODE GIVING SORTED-CODES-FILE STOP RUN.
The INPUT PROCEDURE clause acts like a PERFORM, indicating the logic to go to a different paragraph (i.e. procedure).

So the paragraph SELECT-PROD-CODE might be like this:


SELECT-PROD-CODE. OPEN INPUT UNSORTED-DATA-FILE PERFORM UNTIL END-OF-FILE READ UNSORTED-DATA-FILE AT END MOVE 'Y' TO EOF-FLAG NOT AT END IF 1ST-DIGIT-OF-CODE = 1 THEN MOVE UNSORTED-RECORD TO WORK-REC RELEASE WORK-REC END-IF END-READ END-PERFORM CLOSE UNSORTED-DATA-FILE

When the if condition is true, the record is moved to the work-file (WORK-REC is the level 01 name) by the RELEASE verb, even though the MOVE verb appears first (I dunno why..!). Unlike a simple SORT, you DO have to OPEN the unsorted file prior to an input procedure.

 

&nbsp OUTPUT PROCEDURE

If you just want to print specific sorted fields you would use an OUTPUT PROCEDURE. Based on the above example:


PROCEDURE DIVISION. PRINT-SORT-REC. SORT WORK-FILE ON DESCENDING KEY PRODUCT-NO USING UNSORTED-RECORD OUTPUT PROCEDURE PRINT-SELECT-PROD-CODE STOP RUN.
The INPUT PROCEDURE clause acts like a PERFORM, indicating the logic to go to a different paragraph (i.e. procedure).

So the paragraph SELECT-PROD-CODE might be like this:


SELECT-PROD-CODE. OPEN OUTPUT PRINT-FILE PERFORM UNTIL END-OF-FILE RETURN UNSORTED-DATA-FILE AT END MOVE 'Y' TO EOF-FLAG NOT AT END {move fields in SD sort group to print fields}... WRITE PRINT-RECORD FROM {print group} END-RETURN END-PERFORM CLOSE PRINT-FILE.


Instead of READ you use RETURN and then WRITE the record to the printer rather than RELEASE the record to a file.

You can combine INPUT and OUTPUT procedures into the same sort statement by replacing both the USING and GIVING statements:


SORT WORK-FILE ON DESCENDING KEY PRODUCT-NO INPUT PROCEDURE SELECT-PROD-CODE OUTPUT PROCEDURE PRINT-SELECT-PROD-CODE STOP RUN.