Sort/Merge Operations & Report Writing

COBOL Intermediate/Advanced

Chapter 5 · Sort/Merge Operations & Report Writing

Two genuinely distinct real features have traveled together since COBOL's own early history — real sort/merge facilities and a real, declarative report-writing facility, both introduced together in COBOL-61 Extended (1963). One puts data in order; the other turns ordered data into a formatted, paginated report with almost no manual layout code at all.

SORT: Real Ordering, USING/GIVING

A real SORT reorders an entire file by one or more keys — the leftmost is the real major key, each one after it a progressively less significant tiebreaker:

SORT SORT-WORK-FILE ON ASCENDING KEY TXN-CUSTOMER-ID USING RAW-TRANSACTION-FILE GIVING SORTED-TRANSACTION-FILE.

USING and GIVING handle the real file mechanics automatically — opening, reading every input record, and closing, then writing every sorted record to the output and closing that too. Real INPUT PROCEDURE/OUTPUT PROCEDURE phrases can replace either one when records genuinely need selecting or modifying on the way in or out — but never together with the plain phrase they'd be replacing.

MERGE: Real Combining, Not Sorting

MERGE looks similar but does a genuinely different job — it combines files that are already sorted on the same real key into one, without ever sorting anything itself:

MERGE COMBINED-WORK-FILE ON ASCENDING KEY TXN-CUSTOMER-ID USING EAST-TXN-FILE WEST-TXN-FILE GIVING COMBINED-TXN-FILE.
A Real, Load-Bearing Precondition If EAST-TXN-FILE and WEST-TXN-FILE aren't already genuinely sorted by TXN-CUSTOMER-ID before this statement runs, MERGE doesn't detect the problem or fix it — it simply produces a genuinely wrong, incorrectly ordered result, since combining already-sorted streams is the entire real algorithm MERGE relies on.

Report Writer: A Genuinely Declarative Alternative to Hand-Written Reports

Real Report Writer lets a program declare what a report should look like — the programmer specifies layout and data, not the real logic for page breaks, headings, and formatting:

FD REPORT-FILE. REPORT SECTION. RD CUSTOMER-REPORT CONTROL IS WS-BRANCH-CODE PAGE LIMIT 60 LINES. 01 TYPE IS DETAIL. 05 COLUMN 1 PIC X(30) SOURCE CUST-NAME. 05 COLUMN 35 PIC ZZZ,ZZ9.99 SOURCE CUST-BALANCE.
INITIATE CUSTOMER-REPORT. PERFORM UNTIL WS-EOF-FLAG = "Y" GENERATE CUSTOMER-REPORT READ CUSTOMER-FILE AT END MOVE "Y" TO WS-EOF-FLAG END-READ END-PERFORM. TERMINATE CUSTOMER-REPORT.

Four real statements drive the whole thing: INITIATE starts the report, GENERATE produces one real detail line (and automatically fires a real control break whenever WS-BRANCH-CODE genuinely changes value), SUPPRESS can skip a line that would otherwise print, and TERMINATE closes the report out.

A Real, Documented Decline Report Writer's own real popularity has always varied sharply by shop — some organizations lean on it heavily, others avoid it entirely, partly over genuine implementation-quality and runtime-memory concerns. It was nearly dropped from COBOL-74 before being reinstated, and by the real COBOL 2014 standard, it had become fully optional rather than a guaranteed feature.

Hands-On Exercises

Exercise 1

Using this chapter's own real SORT example, explain in your own words what USING and GIVING each genuinely handle automatically, and what would need to change if a program wanted to filter out certain real records before sorting rather than sorting the entire input file as-is.

📄 View solution
Exercise 2

Using this chapter's own real MERGE example and its warn-box, explain in your own words what would genuinely happen if EAST-TXN-FILE were accidentally left unsorted before this MERGE statement ran, and why MERGE itself wouldn't catch that mistake.

📄 View solution
Exercise 3

Using this chapter's own real Report Writer example, explain in your own words what would genuinely happen inside CUSTOMER-REPORT's own output the moment WS-BRANCH-CODE changes value from one real customer record to the next, and why the programmer never had to write that logic by hand.

📄 View solution

Chapter 5 Quick Reference

  • SORT ... ON ASCENDING/DESCENDING KEY — real reordering by one or more keys, leftmost most significant; USING/GIVING automate file mechanics, INPUT/OUTPUT PROCEDURE replace either for real record selection/modification
  • MERGE — real combining of files already sorted the same way; never sorts anything itself, and won't detect unsorted input
  • Report Writer (RD/REPORT SECTION) — a real declarative report facility, dating to COBOL-61 Extended (1963)
  • INITIATE / GENERATE / SUPPRESS / TERMINATE — the real four statements driving a Report Writer report; GENERATE triggers real automatic control breaks
  • Real status — Report Writer became optional as of COBOL 2014, after nearly being dropped from COBOL-74 entirely