[USflag] The American Programmer [USflag]


Home
Books on Mainframe Programming
Mainframe Manuals and Tutorials
System Abend codes, Sqlcodes, VSAM/QSAM codes
Everything about the IBM AS/400 Midrange Computer - iSeries
Everything about CICS
Cobol programs, manuals, books
  Sample Cobol code: The Simple, Single File COBOL Program
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Sequence Check COBOL Program
  Sample Cobol code: The Record Selection COBOL Program
  Sample Cobol code: The Edit or Validate COBOL Program
  Sample Cobol code: The The One Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Three Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Sequential File, Batch Update COBOL Program
  Sample Cobol code: The COBOL Sort
  Sample Cobol code: The CASE Structure: COBOL EVALUATE
  Sample Cobol code: Direct Subscripting in COBOL
  Sample Cobol code: The Sequential, or Serial Search in COBOL
  Sample Cobol code: The Binary Search in COBOL
  Sample Cobol code: Loading a Table from a Sequential File in a COBOL program
  Sample Cobol code: The VSAM File Read Sequentially in a COBOL program
  Sample Cobol code: The VSAM KSDS, Read Randomly in a COBOL program
  Sample Cobol code: The VSAM File, Read Randomly in a COBOL program
  Sample Cobol code: VSAM Initial Load in a COBOL program
  Sample Cobol code: VSAM File Maintenance (Add, Change, Delete) in a COBOL program
  Sample Cobol code: VSAM Read Sequentially, with START, in a COBOL program
  Sample Cobol code: Creating a Variable Format File in a COBOL program
  Sample Cobol code: Reading a Variable Format File in a COBOL program
  Sample Cobol code: Creating a Variable Format File with Occurs Depending On in a COBOL program
  Sample Cobol code: COBOL Reading a Variable Format File with Occurs Depending On, in a COBOL program
  Sample Cobol code: COBOL The Table Load with Occurs Depending On
  Sample Cobol/DB2 code: Singleton Select
  Sample Cobol code: logic to compare two sorted input files
  Sample Cobol code: logic to merge two sorted input files into one
  Sample Cobol code: Illustrates how Occurs Depending On (ODO) works
  Manuals on the COBOL programming language.
  Books on Cobol
  Abend Codes from Cobol programs
Everything about DB2 and SQL
Everything about IMS
Everything about Java and JavaScript
Everything about JCL and JES
Everything about REXX
Everything about zOS, VSAM, Tivoli, Assembler
Everything about TSO, ISPF, Spufi
Site Map and Site Search

           Home   > COBOL   > COBOL Merge Program

COBOL logic to merge two sorted input files into one

Input files must be sorted! This model program uses a 5 character record key, in the first 5 characters of the record.
The logic is deceptively simple. (Change it at your own risk.)
000100 IDENTIFICATION DIVISION.
000110* MERGE TWO sorted FILES into one
      * I know there are utilities that will do the same thing,
      * but with a COBOL program you can add features that 
      * are not available with utilities.
000200 PROGRAM-ID.      'MERGE'.
000300 ENVIRONMENT DIVISION.
000400 CONFIGURATION SECTION.
000500 INPUT-OUTPUT SECTION.
000600 FILE-CONTROL.
000700     SELECT PREV-IN                ASSIGN PREV.
000800     SELECT CURR-IN                ASSIGN CURR.
001600     SELECT FULL-OUT               ASSIGN FULL.
001700
001800 DATA DIVISION.
001900 FILE SECTION.
002000
002100 FD  PREV-IN
002200     BLOCK CONTAINS 0 RECORDS
002300     LABEL RECORDS ARE STANDARD
002400     DATA RECORD IS PREV-REC.
002500
002600 01  PREV-REC.
002700     05 PREV-KEY PIC X(05).
002800     05 FILLER   PIC X(75).
002900
003000 FD  CURR-IN
003100     BLOCK CONTAINS 0 RECORDS
003200     LABEL RECORDS ARE STANDARD
003300     DATA RECORD IS CURR-REC.
003400
003500 01  CURR-REC.
003600     05 CURR-KEY PIC X(05).
003700     05 FILLER   PIC X(75).
006000 FD  FULL-OUT
006100     BLOCK CONTAINS 0 RECORDS
006200     LABEL RECORDS ARE STANDARD
006300     DATA RECORD IS FULL-REC.
006400
006500 01  FULL-REC                      PIC X(80).
006600
006700 WORKING-STORAGE SECTION.
007100 01  FULL-REC-WS PIC X(80).
007200 01  COUNTERS.
007300     05 PREV-CNT           COMP-3  PIC 9(07) VALUE 0.
007400     05 CURR-CNT           COMP-3  PIC 9(07) VALUE 0.
007800     05 FULL-CNT           COMP-3  PIC 9(07) VALUE 0.
007900
008600 PROCEDURE DIVISION.
008700
008800 P0000-MAIN-PROCESS.
008900     PERFORM P0100-OPEN-FILES
009000
009100     PERFORM P0210-READ-PREV-INPUT
009200     PERFORM P0220-READ-CURR-INPUT
009300
009400     PERFORM P0300-COMPARE-INPUT
009600             UNTIL (PREV-KEY     EQUAL  HIGH-VALUES) AND
009700                   (CURR-KEY     EQUAL  HIGH-VALUES).
009800
009900     PERFORM P0999-NORMAL-EOJ
010000
010100     GOBACK.
010200
010300 P0100-OPEN-FILES.
010400     OPEN INPUT   PREV-IN
010500                  CURR-IN
010600          OUTPUT
010900                  FULL-OUT.
011000
011200
011300 P0210-READ-PREV-INPUT.
011400     READ PREV-IN
011500          AT END
011600             MOVE HIGH-VALUES       TO PREV-KEY
011700             GO TO P0210-EXIT.
011800     ADD +1                         TO PREV-CNT.
011900
012100
012200 P0220-READ-CURR-INPUT.
012300     READ CURR-IN
012400          AT END
012500             MOVE HIGH-VALUES       TO CURR-KEY
012600             GO TO P0220-EXIT.
012700
012800     ADD +1                         TO CURR-CNT.
012900
013100
013200 P0300-COMPARE-INPUT.
013300
013400*** DELETED
013500     IF CURR-KEY     GREATER THAN  PREV-KEY
013600        PERFORM P0910-WRITE-DELETE-RECORD
013610*       PERFORM P0935-WRITE-FULLUP-RECORD
013700        IF PREV-KEY     NOT EQUAL  HIGH-VALUES
013800           PERFORM P0210-READ-PREV-INPUT
013900           GO TO P0300-EXIT
014000        ELSE
014100           GO TO P0300-EXIT.
014200
014300*** ADDED
014400     IF CURR-KEY     LESS THAN  PREV-KEY
014500        PERFORM P0920-WRITE-ADD-RECORD
014600        IF CURR-KEY     NOT EQUAL  HIGH-VALUES
014700           PERFORM P0220-READ-CURR-INPUT
014800           GO TO P0300-EXIT
014900        ELSE
015000           GO TO P0300-EXIT.
015100
015200*** MATCHED
015300     IF CURR-REC     NOT EQUAL  PREV-REC
015400        PERFORM P0930-WRITE-CHANGE-RECORD
015500     ELSE
015510        WRITE FULL-REC     FROM PREV-REC
015600        PERFORM P0935-WRITE-FULLUP-RECORD
015700
015800     IF PREV-KEY     NOT EQUAL  HIGH-VALUES
015900        PERFORM P0210-READ-PREV-INPUT
016000
016100     IF CURR-KEY     NOT EQUAL  HIGH-VALUES
016200        PERFORM P0220-READ-CURR-INPUT
016300
016500
016600 P0910-WRITE-DELETE-RECORD.
016710     WRITE FULL-REC     FROM PREV-REC
016800     ADD +1                         TO DELS-CNT.
016900
017100
017200 P0920-WRITE-ADD-RECORD.
017310     WRITE FULL-REC     FROM CURR-REC
017400     ADD +1                         TO FULL-CNT.
017500
017700
017800 P0930-WRITE-CHANGE-RECORD.
017910     WRITE FULL-REC     FROM PREV-REC
017920     WRITE FULL-REC     FROM CURR-REC
018000     ADD +1                         TO FULL-CNT.
018100
018200 P0930-EXIT. EXIT.
018300
018400 P0935-WRITE-FULLUP-RECORD.
018500     WRITE FULL-REC     FROM CURR-REC
018600     ADD +1                               TO    FULL-CNT.
018700
018800 P0935-EXIT. EXIT.
018900
019000 P0940-WRITE-COUNTS-RECORD.
019500     DISPLAY 'FULL-CNT' FULL-CNT.
019600
019610 P0940-EXIT. EXIT.
019620
019700 P0999-NORMAL-EOJ.
019800
019900     PERFORM P0940-WRITE-COUNTS-RECORD
020000
020100     CLOSE PREV-IN
020200           CURR-IN
020600           FULL-OUT.
020700
020800 P0999-EXIT. EXIT.       
Learn how to use all the features of COBOL: COBOL Programming Books

[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |