COBOL Interview Question – How To Deal With Two Files

One of the best COBOL interview question that has been askind in recent interview is as follows.

Two Input Files

The following way records are there in 2 files. My requirement is matching records, I need to write into 3rd file.

File1

1
2
3
4
5
6
7

File2

0
1
4
5
9
1
3
4
6
7

Solution1

Writing COBOL Program

To write a COBOL program for this takes longer time. So you need to use JOIN KEYS concept which is available in DFSORT. In JCL you need to give two files as input and the 3rd file as output

It validates both keys and write paired or matching key records into 3rd file.

Solution2

Using DFSORT

Here’s an example of a match operation using JOINKEYS. It creates the following output data sets:

OUT12: input records with keys that appear in input file1 and input file2.
OUT1: input records with keys that only appear in input file1.
OUT2: input records with keys that only appear in input file2.

//MATCH EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//IN1 DD DSN=... input File1 (FB/10)
//IN2 DD DSN=... input File2 (FB/10)
//OUT12 DD SYSOUT=* keys in File1 and File2
//OUT1 DD SYSOUT=* keys in File1 only
//OUT2 DD SYSOUT=* kesy in File2 only
//SYSIN DD *
  JOINKEYS F1=IN1,FIELDS=(1,10,A)
  JOINKEYS F2=IN2,FIELDS=(1,10,A)
  JOIN UNPAIRED,F1,F2
  REFORMAT FIELDS=(F1:1,10,F2:1,10,?)
  OPTION COPY
  OUTFIL FNAMES=OUT12,INCLUDE=(21,1,CH,EQ,C'B'),
  BUILD=(1,10)
  OUTFIL FNAMES=OUT1,INCLUDE=(21,1,CH,EQ,C'1'),
  BUILD=(1,10)
  OUTFIL FNAMES=OUT2,INCLUDE=(21,1,CH,EQ,C'2'),
  BUILD=(11,10)
/*

By using DFSORT’s JOINKEYS, JOIN and REFORMAT statements in various ways, you can do many different¬†types of join and match operations to “slice and dice” your data.

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.