The idea is to start with the first file, read each record, write the record you have read to the output file. Do the same for the second file. then close all files.
I assume that all the 3 files have the same record length and same fields and same field sequence.
Questions for thought and test cases:
Q1 - What if one input file is empty?
Q2 - What if both input files are empty? COBOL has no way of telling you how many records are in each file. Your logic should determine this, but I think it is a bit advanced in this case. In fact, the presented approach below will create an empty output file in this case, which may not be what you want. Here comes the role of business rules.
It is recommended that you learn and use the special value "STATUS CODE" - This can help you know if the file opened correctly or not.
Example:
select eastern-transaction-file
assign to "east-transact-sample.dat"
organization is line sequential
status code is file1status.
...
01 file1Status pic xx value "00".
...
* check for I/O error:
if file1status not equal "00" then
.... I/O error has occurred.
end-if
Below is outline of the code - not the full code, rather, I am trying to demo the concept.
OPEN INPUT eastern-transaction-file
If open-error THEN stop-run.
OPEN INPUT western-transaction-file
If open-error THEN
CLOSE eastern-transaction-file
stop-run.
OPEN OUTPUT merged-transactions
If open-error THEN
CLOSE eastern-transaction-file
CLOSE western-transaction-file
stop-run.
MOVE ZERO TO EOF-Eastern-Trans, EOF-Western-Trans.
PERFORM 100-READ-WRITE-EasternTrans UNTIL EOF-Eastern-Trans=1.
PERFORM 200-READ-WRITE-WesternTrans UNTIL EOF-Western-Trans=1.
CLOSE eastern-transaction-file
CLOSE western-transaction-file
CLOSE merged-transactions
stop-run.
100-READ-WRITE-EasternTrans.
* read 1 record from input and write it, when end of file
* set the eof switch to 1
READ eastern-transaction-record
AT END MOVE 1 TO EOF-Eastern-Trans
NOT AT END
MOVE eastern-transaction-record to merged-transactions-record
WRITE merged-transactions from merged-transactions-record.
100-READ-WRITE-EasternTrans-Exit. EXIT.
200-READ-WRITE-WesternTrans.
* read 1 record from input and write it, when end of file
* set the eof switch to 1
...
200-READ-WRITE-WesternTrans-Exit. EXIT.