RETURN Statement
RETURN Statement
- RETURN statement transfers records from the final phase of a sorting or merging operation to an OUTPUT PROCEDURE.
- It is used only within the OUTPUT PROCEDURE associated with a SORT or MERGE statement.
- Atleast one RETURN statement should be coded in an OUTPUT PROCEDURE.
Syntax -
RETURN work-file-1 INTO record-1
AT END statements-block-1
NOT AT END statements-block-2
END-RETURN
Note! All statements coded in [ ] are optional.
Parameters -
- work-file-1 - This is the work file that will hold the sorted records.
- INTO record-1 - The retrieved record is placed into this data item.
- AT END ... - Specifies the statements to be executed when there are no more sorted records.
- NOT AT END ... - Specifies the statements to be executed when a record is successfully retrieved.
- END-RETURN - Specifies the end the of the RETURN statement and is not required when RETURN statement ended with period.
Practical Example -
Scenario - Let us assume we have a file with the employee information and need to sort the file based on the employee number (1-5 bytes). But, we need to skip the DIR (having employee number E0001) record after the sort and before writing it to output file.
Input -
MATEPK.EMPFILE.NSINPUT1
----+----1----+----2----+----3----+----4----+-- E0004EMPLOYEE4 TL DEPT1LOC1 0000050000 E0002EMPLOYEE2 MGR DEPT1LOC1 0000080000 E0006EMPLOYEE6 SE DEPT1LOC1 0000034000 E0001EMPLOYEE1 DIR LOC1 0000100000
MATEPK.EMPFILE.INPUT2
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPFILE1 ASSIGN TO INPUT1.
SELECT EMPFILEO ASSIGN TO OUTPUT1.
SELECT WORKFILE ASSIGN TO WORK1.
...
DATA DIVISION.
FILE SECTION.
* Use declarations from SORT example
...
WORKING-STORAGE SECTION.
01 WS-SORTEOF-SW PIC X(01) VALUE 'N'.
88 SORT-EOF VALUE 'Y'.
...
PROCEDURE DIVISION
* SORT using OUTPUT PROCEDURE
SORT WORKFILE
ON ASCENDING KEY WORK-EMP-NUM
USING EMPFILE1
OUTPUT PROCEDURE IS 1000-WRITE-RECORDS.
STOP RUN.
...
1000-WRITE-RECORDS.
OPEN OUTPUT EMPFILEO.
* RETURN statement with LOOP to process all records
PERFORM UNTIL SORT-EOF
RETURN WORKFILE
AT END SET SORT-EOF TO TRUE
NOT AT END PERFORM 2000-WRITE-OUTPUT
END-RETURN
END-PERFORM.
CLOSE EMPFILEO.
...
2000-WRITE-OUTPUT.
* Skipping E0001 record while writing
IF WORK-EMP-NUM EQUAL 'E0001'
CONTINUE
ELSE
MOVE WORK-REC TO EMP-RECO
WRITE EMP-RECO
END-IF.
JCL -
//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //STEP01 EXEC PGM=RETURNSR //STEPLIB DD DSN=MATEPK.COBOL.LOADLIB,DISP=SHR //INPUT1 DD DSN=MATEPK.EMPFILE.NSINPUT1,DISP=SHR //OUTPUT1 DD DSN=MATEPK.EMPFILE.RETURNOP, // DISP=(NEW,CATLG,DELETE), // SPACE=(TRK,(3,2),RLSE), // UNIT=SYSDA, // DCB=(DSORG=PS,RECFM=FB,LRECL=47,BLKSIZE=470) //WORK1 DD DSN=&&TEMP, // DISP=(NEW,DELETE,DELETE), // SPACE=(CYL,(10,5),RLSE), // UNIT=SYSDA, // DCB=(DSORG=PS,RECFM=FB,LRECL=47,BLKSIZE=470) //SYSOUT DD SYSOUT=*
Output (MATEPK.EMPFILE.RETURNOP) -
----+----1----+----2----+----3----+----4----+-- E0002EMPLOYEE2 MGR DEPT1LOC1 0000080000 E0004EMPLOYEE4 TL DEPT1LOC1 0000050000 E0006EMPLOYEE6 SE DEPT1LOC1 0000034000