I have the requirement to compare the two files and pick up
the matching records.
File 1. file2

23 32
32 13
34 15
35 36
36 35
43

Get the matching records from this 2 files to out file. how
you will do this in cobol program?

Answers were Sorted based on User's Feedback



I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / nnn

First sort the both files through JCL or internal sort.
Then follow below logic in the program.

Read file1
Read file2

perform until end-of file1 OR end-of file2
if file1 = file2
write a matched record to output
read file1
read file2
end-if
if file1 < file2
read file1
end-if
if file1 > file2
read file2
end-if
End-perform.

Is This Answer Correct ?    117 Yes 8 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / soumya santosini patnaik

first load the files in shorted order after that code a
matching program. in the procedure division check the file1
entry with file2 entry if file1 entry = file2 entry then
then write to o/p file end-evaluate esle check for file1<
file2 (if yes then the pointer is brought down to the 2nd
rec of file1 and checked with 1st rec of file2 when the
para is performed for 2nd time),if the above condition is
false then it checks if file1>file2 if yes then pointer is
brought down to the next entry of file2 and checked with
1st entry of file1 with 2nd entry of file2 in next perform
and the process is done till the end of records.

Is This Answer Correct ?    11 Yes 1 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / siri

IN COBOL
IF FILE1>FILE2
READ FILE2
IF FILE1<FILE2
READ FILE1
IF FILE1=FILE2
WRITE FILE3
READ FILE1
READ FILE2.......
IN JCL
//XSUM DD DSN=XSUM.MATCH
//SYSIN DD *
SORT FIELDS=COPY
SUM FIELDS=NONE,XSUM
/*

Is This Answer Correct ?    6 Yes 0 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / sreejith

firt read file1 move each record to table. at the end of
file1 start read file2. for each record read from file2
compare that record with all the elements from table. if it
match write that record to the output file. same procedure
follow for all the records from file2

Is This Answer Correct ?    16 Yes 12 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / kalone

First load both the files into 2 arrays and then sort them
on say ASC criteria and then take the first element from 1
file and compare with the element of other file , if
matches write into Outfile ,like wise we can keep on
reading & comparing till we get all the matching values in
the outfile.

Is This Answer Correct ?    6 Yes 5 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / bala

FILE SECTION.
FD OLD-MASTER
LABEL RECORDS ARE STANDARD.
01 OLD-MASTER-REC.
05 M-ACCT-NO PIC X(5).
05 AMOUNT-DUE PIC 9(4)V99.
05 PIC X(89).
FD TRANS-FILE
LABEL RECORDS ARE STANDARD.
01 TRANS-REC.
05 T-ACCT-NO PIC X(5).
05 AMT-TRANS-IN-CURRENT-PER PIC 9(4)V99.
05 PIC X(89).
FD NEW-MASTER
LABEL RECORDS ARE STANDARD.
01 NEW-MASTER-REC.
05 ACCT-NO-OUT PIC X(5).
05 AMOUNT-DUE-OUT PIC 9(4)V99.
05 PIC X(89).
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
100-MAIN-MODULE.
PERFORM 800-INITIALIZATION-RTN.
PERFORM 600-READ-MASTER.
PERFORM 700-READ-TRANS.
PERFORM 200-COMP-RTN
UNTIL M-ACCT-NO = HIGH-VALUES
AND
T-ACCT-NO = HIGH-VALUES
PERFORM 900-END-OF-JOB-RTN.
STOP RUN.
200-COMP-RTN.
EVALUATE TRUE
WHEN T-ACCT-NO = M-ACCT-NO
PERFORM 300-REGULAR-UPDATE
WHEN T-ACCT-NO < M-ACCT-NO
PERFORM 400-NEW-ACCOUNT
WHEN OTHER
PERFORM 500-NO-UPDATE
END-EVALUATE.
300-REGULAR-UPDATE.
MOVE OLD-MASTER-REC TO NEW-MASTER-REC
COMPUTE AMOUNT-DUE-OUT = AMT-TRANS-IN-CURRENT-PER
+ AMOUNT-DUE
WRITE NEW-MASTER-REC
PERFORM 600-READ-MASTER
PERFORM 700-READ-TRANS.
400-NEW-ACCOUNT.
MOVE SPACES TO NEW-MASTER-REC.
MOVE T-ACCT-NO TO ACCT-NO-OUT.
MOVE AMT-TRANS-IN-CURRENT-PER TO AMOUNT-DUE-OUT.
WRITE NEW-MASTER-REC.
PERFORM 700-READ-TRANS.
500-NO-UPDATE.
WRITE NEW-MASTER-REC FROM OLD-MASTER-REC.
PERFORM 600-READ-MASTER.
600-READ-MASTER.
READ OLD-MASTER
AT END
MOVE HIGH-VALUES TO M-ACCT-NO
END-READ.
700-READ-TRANS.
READ TRANS-FILE
AT END
MOVE HIGH-VALUES TO T-ACCT-NO
END-READ.
800-INITIALIZATION-RTN.
OPEN INPUT OLD-MASTER
TRANS-FILE.
OPEN OUTPUT NEW-MASTER.
900-END-OF-JOB-RTN.
CLOSE OLD-MASTER
TRANS-FILE
NEW-MASTER.

Hope this is useful.

Is This Answer Correct ?    2 Yes 2 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / ajay kumar ande

OPEN INPUT FILE1
OPEN INPUT FILE2
OPEN OUTPUT FILE3

MOVE 'N' TO EOF
PERFORM UNTIL EOF='S'
READ FILE1 AT END MOVE 'S' TO EOF
READ FILE2 AT END MOVE 'S' TO EOF

IF EMPNO1 = EMPNO2
MOVE
REC2 TO REC3
WRITE REC3
END-PERFORM.

CLOSE FILE1, FILE2, FILE3.

Is This Answer Correct ?    10 Yes 11 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / richa

from these evaluate i am able to get the matched records in
one file but if i want to write the unmatched record in the
spool area then how to track those unmatched records from
here

Is This Answer Correct ?    1 Yes 2 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / michar de papachuil

This might help you here but you have to Sort the two files
first before doing the match.

IF File1 = File2
PERFORM 2400-MATCH-BOTH-FILES THRU 2400-EXIT
PERFORM 2100-READ-FILE1 THRU 2100-EXIT
PERFORM 2200-READ-FILE2 THRU 2200-EXIT

ELSE

IF I-GROUP-SUBSYSTEM LESS THAN I-MODULE-SUBSYSTEM
PERFORM 2600-UNKNOWN-FILE1-VALUES THRU 2600-EXIT
PERFORM 2100-READ-FILE1 THRU 2100-EXIT
ELSE
PERFORM 2500-UNKNOWN-FILE2-VALUES THRU 2500-EXIT
PERFORM 2200-READ-FILE2 THRU 2200-EXIT
END-IF

Is This Answer Correct ?    0 Yes 1 No

I have the requirement to compare the two files and pick up the matching records. File 1. ..

Answer / anil t.

Using Merge

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More COBOL Interview Questions

01 a pic s9(5) value -12345, if we disply a , the sign will overpunched with last digit but i need to get the miuns sign in the result?

7 Answers  


What is the usage of comp fields in cobol?

0 Answers  


how do u indetify files succesfully executed or not ?

4 Answers   TCS,


How to use the same COBOL program in Batch and CICS on lines? explain with an example

0 Answers   IBM,


Can you REWRITE a record in an ESDS file? Can you DELETE a record from it?

6 Answers   ABC, IBM, Mphasis, Wipro,






How to read records in reverse order in flat file? I know we can do it by reading all records into an array.... Then read records in reverse order by using subscript or index but can any body give me the exact code.

5 Answers   TCS,


i need a program by giving input as a abcd in any randem order but i need a output as 1234 related to abcd. i.e,. a for 1,b=2,c.....etc..

5 Answers   IBM,


what is mainframe? what is the mainframe software ? what is use in s/w field?

7 Answers   CSE,


What are the differences bitween cobol and cobol-2?

1 Answers   Wipro,


What are differences between Static Call and Dynamic Call?

10 Answers   IBM, KBC, Keane India Ltd, Verizon,


how you will define variables length in cobol.

3 Answers   Temenos,


given the following piece of code: CALL SUB-PGM USING A, B, C. CALL SUB-PGM USING A, C, C. (a) Both CALL statements will always produce same result. (d) Both CALL statements may produce different result. (c) Compile-time error because SUB-PGM is a dataname. (d) Compile-time error because A, B, C are used twice.

2 Answers   TCS,


Categories