Friday, March 18, 2011

TIPS ON EXTRACTING RECORDS FROM A FILE USING SORT JCL


OBJECTIVE:
1) To through some light on Sort card.
2) To extract particular number of records from a sequential file, which involves condition like select some records, then skip few records, then again select records and so on.

SORT UTILITY: SYNCSORT, DFSORT

We will discuss above thing by taking an example,
REQUIREMENT:
From a sequential file suppose we need to extract say first 5 records, skip next 4 records,
and again select next 5 records.
(Think this condition from the following point of view says if we have 100 conditions then what?)
For simplicity only have taken only two conditions. Assume that Input file has 20 records.

SOLUTIONS:
1.Using SKIPREC, STOPAFT parameters in SORT card.
2.Using OUTFIL in sort card.
3.Using OUTFIL with SAVE.

FIRST SOLUTIONS,

JCL would go like this,

FIRST STEP IN JCL would contain following sort card
//SYSIN      DD *
    SORT FIELDS=COPY,
    STOPAFT=5
    END
/*
Above sort card will select first five records from input file.

SECOND STEP IN JCL will contain following sort card,
 a) To skip next 5 +4 =9 records,
 b) To copy next five records starting from 10 Th record,
//SYSIN      DD *
    SORT FIELDS=COPY,
    SKIPREC=9,
    STOPAFT=5
    END

THIRD STEP:
This step is there to finally merge the files, which are created in previous two steps. Output of this merge step will be our required file.

DRAWBACK:
Here the number of condition is 2, so we needed only two steps.
Think about! What will you do if we have 100 conditions? Do we write?
100 STEPS? Surely not, it would be cumbersome.

To avoid this we have second approach,


SECOND SOLUTION:
Using OUTFIL in sort card.
Objective:
1) To solve above problem with minimum number of steps.
2) To have only one sort card for all condition.
3) And one final merge step to merge intermediate files.

Sort card that can be used would be like this,
FIRST STEP IN JCL will contain following sort card
//SYSIN      DD *
    SORT FIELDS=COPY
    OUTFIL FILES=1,STARTREC=1,ENDREC=5
    OUTFIL FILES=2,STARTREC=10,ENDREC=14
    END
/*
Above sort card will do following things,
1.      1.      One file will create which will have first five records.
2.      2.      Second file will created which will have next five records starting from 10 Th record
Make sure that you give following logical name of those intermediate files, which are created as SORTOF1,SORTOF2, SORTOF3 and so on.

SECOND STEP IN JCL,
Its a merge step, it will merge all files which are created above.

CONCLUSION:
Even 100 conditions are there, we would need only 2 steps to extract desired records from file.
DRAWBACK:
Lot of intermediate files may be created. Suppose, if 100 conditions are there, one hundred intermediate files would be created. Then again headache to merge all those 100 files in MERGE STEP.
            To avoid this we have third solution,

THIRD SOLUTION:
 It’s a general solution independent of number of conditions. Also it can do in SINGLE STEP.
JCL would be like this,

//N010053A JOB (1L17L120),XXXX.III,
...
...
...
...
//STEP11       EXEC PGM=IERRCO00,REGION=650K,RD=R
//*             PARM='VSCORE=5500K,MSG=AP,BMSG'
//SORTLIB      DD   DSN=SYS1.SORTLIB,DISP=SHR
//MODLIB       DD   DSN=SYS1.LOADLIB,DISP=SHR
//SYSOUT       DD   SYSOUT=(,),OUTPUT=(*.TOPAPER,*.TOHOLDQ)
//SYSUDUMP     DD   SYSOUT=(,),OUTPUT=(*.TOPAPER,*.TOHOLDQ)
//SORTIN       DD   DSN=T145.INPUT.FILE,
//             DISP=SHR
//SORTOF1      DD   DSN=&&TEMP1
//SORTOF2      DD   DSN=&&TEMP2
//SORTOF3      DD   DSN=T145.OUTPUT.FILE,
//             DISP=(NEW,CATLG,DELETE),UNIT=SYSALLDA,
//             SPACE=(8048,(545,2178),RLSE),
//             DCB=(BLKSIZE=0,LRECL=53,RECFM=FB)
//SORTWK01     DD   SPACE=(CYL,(502,101),RLSE),UNIT=DASD
//SORTWK02     DD   SPACE=(CYL,(502,101),RLSE),UNIT=DASD
//SORTWK03     DD   SPACE=(CYL,(502,101),RLSE),UNIT=DASD
//SORTWK04     DD   SPACE=(CYL,(502,101),RLSE),UNIT=DASD
//DUMPDD       DD   SYSOUT=*
//DSPLD        DD   SYSOUT=*
//SYSIN      DD *
    SORT FIELDS=COPY
    OUTFIL FILES=1,STARTREC=6,ENDREC=9
    OUTFIL FILES=2,STARTREC=15,ENDREC=20
    OUTFIL FILES=3,SAVE
    END
/*
//*
Few points regarding this JCL,
1.      1.      Use of SAVE in sort card.
2.      2.      First temporary file will have four records after skipping first five records.
3.      3.      Second temporary file will have records starting from 15th record till the last record.
4.      4.      Here the catch is file 1 and file 2 will have all those records, which we don’t want.
5.      5.      So file 3 ( T145.OUTPUT.FILE ) will have all first five records ,skipping next four records, again extracting next five records.
6.      6.      All intermediate files will be deleted at end of JOB.

CONCLUSION:
Thus, only one step is required to extract records from file. Also no merge step is required now. One more thing, in all of the above sortcard we have mentioned so far, we can cover conditions like INCLUDE, OMIT etc also.