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:
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:
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:
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.
Hands-On Exercises
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 solutionUsing 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 solutionUsing 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 solutionChapter 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