Example 5 - Paired and unpaired F1/F2 records (indicator method)
//JKE5 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTJNF1 DD DSN=FIRST.FILE,DISP=SHR
//SORTJNF2 DD DSN=SECOND.FILE,DISP=SHR
//F1ONLY DD SYSOUT=*
//F2ONLY DD SYSOUT=*
//BOTH DD SYSOUT=*
//SYSIN DD *
* Control statements for JOINKEYS application
JOINKEYS FILE=F1,FIELDS=(1,10,A),SORTED,NOSEQCK
JOINKEYS FILE=F2,FIELDS=(7,10,A),SORTED,NOSEQCK
JOIN UNPAIRED,F1,F2
REFORMAT FIELDS=(F1:1,14,F2:1,20,?)
* Control statements for main task (joined records)
OPTION COPY
OUTFIL FNAMES=F1ONLY,INCLUDE=(35,1,CH,EQ,C'1'),
BUILD=(1,14)
OUTFIL FNAMES=F2ONLY,INCLUDE=(35,1,CH,EQ,C'2'),
BUILD=(15,20)
OUTFIL FNAMES=BOTH,INCLUDE=(35,1,CH,EQ,C'B'),
BUILD=(1,14,/,15,20)
/*
- F1 records which do not have a match in F2 on the specified keys (for example, key=David) will be written to the F1ONLY output file.
- F2 records which do not have a match in F1 on the specified keys (for example, key=Karen) will be written to the F2ONLY output file.
- F1 and F2 records which have a match in F1 and F2 on the specified keys (for example, key=Carrie) will be written to the BOTH output file.
Carrie F101
David F102
Frank F103
Holly F104
Vicky F105
No Carrie F201
Yes Holly F202
Yes Karen F203
No Sri Hari F204
Yes Vicky F205
David F102
Frank F103
Yes Karen F203
No Sri Hari F204
Carrie F101
No Carrie F201
Holly F104
Yes Holly F202
Vicky F105
Yes Vicky F205
The first JOINKEYS statement defines the ddname and key for the F1 file. FILE=F1 tells DFSORT that the ddname for the F1 file is SORTJNF1. FIELDS=(1,10,A) tells DFSORT that the key is in positions 1-10 ascending. Since SORTED is specified, indicating that the records are already in order by the specified binary key, DFSORT will copy the SORTJNF1 records. Since NOSEQCK is specified, DFSORT will not check that the records are in order by the key. (Only use NOSEQCK if you know for sure that the records are in order by the key.)
The second JOINKEYS statement defines the ddname and key for the F2 file. FILE=F2 tells DFSORT that the ddname for the F2 file is SORTJNF2. FIELDS=(7,10,A) tells DFSORT that the key is in positions 7-16 ascending. Since SORTED is specified, indicating that the records are already in order by the specified binary key, DFSORT will copy the SORTJNF2 records. Since NOSEQCK is specified, DFSORT will not check that the records are in order by the key. (Only use NOSEQCK if you know for sure that the records are in order by the key.)
The JOIN statement tells DFSORT that the joined records should include the unpaired F1 and F2 records as well as the paired F1/F2 records.
Joined Record Positions Extracted from
----------------------- -----------------
1-14 F1 positions 1-14
15-34 F2 positions 1-20
35 Indicator of where key was found
The OPTION COPY statement tells DFSORT to copy the joined records. The OUTFIL statements use the indicator in position 35 to determine where to find the F1 or F2 fields in the joined records and where to write the fields (F1ONLY, F2ONLY or BOTH).
- Subtask1 copies the SORTJNF1 (F1) records as directed by the JOINKEYS statement. As a result, it copies the unchanged SORTJNF1 records to the main task.
- Subtask2 copies the SORTJNF2 (F2) records as directed by the JOINKEYS statement. As a result, it copies the unchanged SORTJNF2 records to the main task.
- The main task joins the records passed from subtask1 and subtask2
as directed by the specified JOINKEYS, JOIN and REFORMAT statements,
resulting in the following joined records (paired and unpaired):
Carrie F101No Carrie F201B David F102 1 Frank F103 1 Holly F104Yes Holly F202B Yes Karen F2032 No Sri Hari F2042 Vicky F105Yes Vicky F205B
For F1 records without a match in F2 (for example, the F102 record), the indicator in position 35 has a '1'. For F2 records without a match in F1 (for example, the F203 record), the indicator in position 35 has a '2'. For F1 records with a match in F2 (for example, the F101 and F201 records), the indicator in position 35 has a 'B'.
- The first OUTFIL statement finds records with a '1' in position
35. These are the F1 records without a match in F2. The F1 field
is in positions 1-14 of the joined record, so those positions are
written to the F1ONLY file. Thus, F1ONLY contains these records:
David F102 Frank F103
- The second OUTFIL statement finds records with a '2' in position
35. These are the F2 records without a match in F1. The F2 field
is in positions 15-34 of the joined record, so those positions are
written to the F2ONLY file. Thus, F2ONLY contains these records:
Yes Karen F203 No Sri Hari F204
- The third OUTFIL statement finds records with 'B' in position
35. These are the F1 and F2 records with a match. The F1 field is
in positions 1-14 of the joined record and the F2 field is in positions
15-34 of the joined record, so each joined record is split into those
two records and written to the BOTH file. The shorter F1 record is
padded with blanks on the right to the length of the F2 record. Thus,
BOTH contains these records:
Carrie F101 No Carrie F201 Holly F104 Yes Holly F202 Vicky F105 Yes Vicky F205