In COBOl, compare two files and write matching records to a file ...

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Vipin Verma
Registered Member
Posts: 16
Joined: Tue Aug 20, 2013 10:45 am
India

In COBOl, compare two files and write matching records to a file ...

Post by Vipin Verma »

Hi,

How can we write a program in COBOL to compare two files and write matching records to a file and unmatched to another. Basically, I have two files files. File-1 and file-2. They have got a huge number of records. I would like to compare these two files and write the matching records in output file-1 and output file-2.

If any one of you can please describe the logic to this in cobol efficiently, it'll be great help. TIA!
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: In COBOl, compare two files and write matching records to a file ...

Post by Robert Sample »

Terminology is critical in IT, where similar terms may mean very different things.  On a mainframe, you deal with data sets.  The only files are Unix System Services files and tape files -- everything else is a data set.

You left a lot unspecified in your post.  Do the data sets have a matching key or are you planning on matching on the entire record?  Are the data sets sorted?  If not, you should sort them first as it becomes a much easier task then.  Can you have duplicate keys or is each key unique?  What do you want to do with unmatched records -- discard them? write to a hold file?

There are tested copies of a two-file match/merge program available on various fora, so you could use one of those instead of rolling your own.  If you insist on doing it yourself, the logic (assuming sorted data sets and no duplicate keys) is:
- read record from 1
- read record from 2
- if key1  < key 2
-   process unmatched key 1 and read 1
- if key 1 = key 2
-   process match and read 1, 2
- if key 1 > key 2
-   process unmatched key 2 and read 2

You do have to handle the cases when one data set runs out of records and the other still has records.
User avatar
Retired_Coder
New Member
Posts: 1
Joined: Tue Apr 02, 2024 10:08 pm
United States of America

Re: In COBOl, compare two files and write matching records to a file ...

Post by Retired_Coder »

I know this is a really old post but maybe someone will run across my logic and be enlightened

Try matching 2 files this way.....easy peasy
Stop using the < and > compares and NEVER worry about EOF!

Work Area
Create a field named File1-Key
Create a field named File2-Key
Create a field named Low-Tag the same size as the File Keys
Create a Temp field(numeric for single digit)

Initial:
Read File 1 (ANY TIME file 1 is EOF, move HIGH-VALUES to File1-Key)
Read File 2 (ANY TIME file 2 is EOF, move HIGH-VALUES to File2-Key)
Call or run a Procedure to populate Low-Tag with the lowest value from EITHER file key


Main-Process:
Loop UNTIL Low-Tag equal HIGH-VALUES

Zero Temp
If File 1 matches Low-Tag, add 1 to Temp
If File 2 matches Low-Tag, add 2 to Temp

If Temp = 1, process whatever for File 1 and read File 1
If Temp = 2, process whatever for File 2 and read File 2
If Temp = 3, process whatever for Matching records and read both File 1 AND File 2
Call or run a Procedure to populate Low-Tag with the lowest value from either file key



That's it

When Low-Tag equals HIGH-VALUES all records have been processed and you didn't even
have to worry about which one ended first

---------------------------------------------
Match 3 files by simply adding the File 3 Read and:
If File 3 matches Low-Tag, add 4 to Temp

Add additional Temp coding for 4, 5, 6, and 7
 
Vipin Verma
Registered Member
Posts: 16
Joined: Tue Aug 20, 2013 10:45 am
India

Re: In COBOl, compare two files and write matching records to a file ...

Post by Vipin Verma »

Thanks. Just one question about HIGH VLAUES, can a dataset never have "HIGH VALUES" as a value?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: In COBOl, compare two files and write matching records to a file ...

Post by Robert Sample »

Yes, a data set can have a record of HIGH-VALUES (which is merely X'FFFFFF...') but it can also be used as a stop value, too.
Vipin Verma
Registered Member
Posts: 16
Joined: Tue Aug 20, 2013 10:45 am
India

Re: In COBOl, compare two files and write matching records to a file ...

Post by Vipin Verma »

Thanks Robert. So if HIGH VALUES are part of data, can they always be considered as the "last record" of the dataset?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: In COBOl, compare two files and write matching records to a file ...

Post by Robert Sample »

Not necessarily.  If the data set is sequential, and if the data set is sorted by some key, and if the key includes the HIGH-VALUES then that record will be last.  However, I've seen times when the data set wasn't sorted and the HIGH-VALUES occurred in the first record or somewhere in the middle of the data set.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”