Key Takeaways — Chapter 15: Sort and Merge

  1. The SORT verb is declared with SD, not FD. Sort work files use the SD (Sort Description) level indicator and must never be explicitly OPENed or CLOSEd — the SORT verb manages their lifecycle.

  2. USING/GIVING is the simplest sort form. When no filtering or transformation is needed, USING and GIVING let the sort utility handle all I/O automatically. This is often the most efficient approach.

  3. INPUT PROCEDURE + RELEASE gives you control over input. Use an INPUT PROCEDURE when you need to validate, filter, or transform records before they enter the sort. The RELEASE statement passes each record to the sort engine.

  4. OUTPUT PROCEDURE + RETURN gives you control over output. Use an OUTPUT PROCEDURE when you need to process sorted records — for control break reports, deduplication, splitting into multiple files, or aggregation. The RETURN statement retrieves records in sorted order.

  5. MERGE combines pre-sorted files. The MERGE verb interleaves records from files that are already in the correct sort order. It is faster than SORT because it skips the internal sort phase, but it requires that all input files be pre-sorted on the specified keys.

  6. Always check SORT-RETURN. The SORT-RETURN special register contains 0 for success and 16 for failure. Check it after every SORT or MERGE statement in production code.

  7. Implement error thresholds in INPUT PROCEDUREs. Setting SORT-RETURN to 16 inside an INPUT PROCEDURE gracefully aborts the sort. Use this to stop processing when data quality falls below an acceptable threshold.

  8. WITH DUPLICATES IN ORDER ensures stable sorting. When the relative order of equal-key records matters (audit trails, chronological processing), use this clause to guarantee the sort preserves original record order.

  9. FASTSRT improves USING/GIVING performance. This compiler option lets the sort utility bypass COBOL I/O routines, but only works with USING/GIVING (not INPUT/OUTPUT PROCEDUREs).

  10. Choose COBOL SORT vs. JCL SORT based on complexity. Simple reordering favors JCL SORT. Filtering, validation, transformation, or conditional logic requires COBOL SORT with INPUT/OUTPUT PROCEDUREs.