Let’s take an example F1 80 bytes and F2 40 bytes file.
Matched Records(Inner Join)
If the JOIN statement is not specified in the sort card, paired records from F1 and F2 are written into the output file.
//SYSIN DD *
SORT FIELDS=COPY
JOINKEYS FILES=F1,FIELDS=(1,10,A)
JOINKEYS FILES=F2,FIELDS=(1,10,A)
REFORMAT FIELDS=(F1:1,80,F2:1,40)
/*
Matched Records and Non-Matched Records from both files
Sort card to retain unpaired records from both F1 and F2 files along with paired records. This type of join is called a FULL OUTER JOIN.
//SYSIN DD *
SORT FIELDS=COPY
JOINKEYS FILES=F1,FIELDS=(1,10,A)
JOINKEYS FILES=F2,FIELDS=(1,10,A)
JOIN UNPAIRED,F1,F2
REFORMAT FIELDS=(F1:1,80,F2:1,40)
/* Matched Records and Non-Matched Records from File1
Sort card to retain unpaired records from the F1 file along with paired records. This type of join is called a LEFT OUTER JOIN.
//SYSIN DD *
SORT FIELDS=COPY
JOINKEYS FILES=F1,FIELDS=(1,10,A)
JOINKEYS FILES=F2,FIELDS=(1,10,A)
JOIN UNPAIRED,F1
REFORMAT FIELDS=(F1:1,80,F2:1,40)
/* Non Matched from File1
Sort card to retain unpaired records from F1 file, cannot use reformat in F2.
//SYSIN DD *
SORT FIELDS=COPY
JOINKEYS FILES=F1,FIELDS=(1,10,A)
JOINKEYS FILES=F2,FIELDS=(1,10,A)
JOIN UNPAIRED,F1,ONLY
REFORMAT FIELDS=(F1:1,80)
/* Matched Records and Non Matched from File2
Sort card to retain unpaired records from the F2 file along with paired records. This type of join is called RIGHT OUTER JOIN.
//SYSIN DD *
SORT FIELDS=COPY
JOINKEYS FILES=F1,FIELDS=(1,10,A)
JOINKEYS FILES=F2,FIELDS=(1,10,A)
JOIN UNPAIRED,F2
REFORMAT FIELDS=(F1:1,80,F2:1,40)
/* Non Matched from File2
Sort card to retain unpaired records from F2 file, cannot use reformat in F1.
//SYSIN DD *
SORT FIELDS=COPY
JOINKEYS FILES=F1,FIELDS=(1,10,A)
JOINKEYS FILES=F2,FIELDS=(1,10,A)
JOIN UNPAIRED,F2,ONLY
REFORMAT FIELDS=(F2:1,40)
/* Non-Matched Records from both files
Sort card to retain only unpaired records from F1 and F2 files.
//SYSIN DD *
SORT FIELDS=COPY
JOINKEYS FILES=F1,FIELDS=(1,10,A)
JOINKEYS FILES=F2,FIELDS=(1,10,A)
JOIN UNPAIRED,F1,F2,ONLY or JOIN UNPAIRED,ONLY
REFORMAT FIELDS=(F1:1,80,F2:1,40)
/* Example: JOINKEYS File Match and Comparison Examples
//STEP010 EXEC PGM=SORT
//SORTJNF1 DD DSN=FIRST.FILE,DISP=SHR
//SORTJNF2 DD DSN=SECOND.FILE,DISP=SHR
//BOTHF1F2 DD DSN=MYDATA.URMI.SAMPLE.MATCH,DISP=OLD
//ONLYF1 DD DSN=MYDATA.URMI.SAMPLE.NOMATCH1,DISP=OLD
//ONLYF2 DD DSN=MYDATA.URMI.SAMPLE.NOMATCH2,DISP=OLD
//SYSOUT DD SYSOUT=*
//SYSIN DD *
JOINKEYS FILE=F1,FIELDS=(1,10,A)
JOINKEYS FILE=F2,FIELDS=(7,10,A)
JOIN UNPAIRED,F1,F2
REFORMAT FIELDS=(?,F1:1,14,F2:1,20)
OPTION COPY
OUTFIL FNAMES=BOTHF1F2,INCLUDE=(1,1,CH,EQ,C’B'),
BUILD=(1:2,14,/,16,20)
OUTFIL FNAMES=ONLYF1,INCLUDE=(1,1,CH,EQ,C'1'),BUILD=(1:2,14)
OUTFIL FNAMES=ONLYF2,INCLUDE=(1,1,CH,EQ,C'2'),BUILD=(1:16,20)
/* Explanation:
- JOINKEYS specifies the field on which the two files are compared.
- REFORMAT FIELDS=?
- places ‘B’ (matched records),
- ‘1’ (present in file1, but not in file2), or
- ‘2’ (present in file2 but not in file1) in the 1st position of the output BUILD.
- JOIN UNPAIRED does a full outer join on the two files.
You can achieve the above result by using the FILL parameter.
//SYSIN DD *
JOINKEYS FILE=F1,FIELDS=(1,10,A)
JOINKEYS FILE=F2,FIELDS=(7,10,A)
JOIN UNPAIRED,F1,F2
REFORMAT FIELDS=(F1:1,14,F2:1,20),FILL=C'$'
OPTION COPY
OUTFIL FNAMES=BOTHF1F2,INCLUDE=(15,1,CH,NE,C’$’,
AND,1,1,CH,NE,C'$'),
BUILD=(1:1,14,/,15,20)
OUTFIL FNAMES=ONLYF1,INCLUDE=(15,1,CH,EQ,C’FILL=C'$'),
BUILD=(1:1,14)
OUTFIL FNAMES=ONLYF2,INCLUDE=(1,1,CH,EQ,C’$'),
BUILD=(1:15,20)
/* Explanation:
- JOINKEYS specifies the field on which the two files are compared.
- FILL=C’$’ in REFORMAT FIELDS will fill the spaces with $.
- $ in position 15 till 35 means (present in file1, but not in file2), or
- $ in position 1 till 14 means(present in file2 but not in file1).
- JOIN UNPAIRED does a full outer join on the two files.
JOINKEYS File Match and Comparision Examples