Duplicate keys are not duplicate mistakes—they are multiple rows that share the same sort key value. A nightly transaction file may contain three postings for one account on one day. Easytrieve SORT keeps all three when keys match; it only places them adjacent in the output file. Deduplication, first-only processing, and last-wins updates happen later in JOB when you test DUPLICATE, FIRST-DUP, LAST-DUP, or explicit key-change logic. Confusing sort retention with VSAM duplicate-key WRITE status causes beginners to search for a SORT DEDUP parameter that does not exist. This page explains what SORT does and does not do with equal keys, how sorting enables group detection, how BEFORE SELECT behaves with repeated selection, patterns for keeping first or last per key, summing duplicate groups, and reporting duplicate clusters. Read the DUPLICATE constants tutorial for full condition semantics; this page ties them to sorted workflows.
1234SORT TRANS TO SORTWRK USING (ACCT-NO, POST-DATE) * Input: three rows ACCT 100 same date different amounts * Output: three adjacent rows — not one merged row
The sort utility orders by ACCT-NO major and POST-DATE minor. Rows with identical keys on both fields appear consecutively but all survive. To drop extras, filter in JOB or BEFORE—not by omitting USING.
| File state | DUPLICATE condition | JOB complexity |
|---|---|---|
| Random key order | True sporadically; groups missed | Needs tables or multi-pass |
| Sorted by business key | True within adjacent groups | Single pass with FIRST/LAST-DUP |
Sort keys should include the business identity you use for duplicate tests—often ACCT-NO alone or ACCT-NO plus POST-DATE if duplicates are defined at that grain.
1234JOB INPUT SORTWRK IF FIRST-DUP SORTWRK OR NOT DUPLICATE SORTWRK PRINT DETAIL-RPT END-IF
FIRST-DUP OR NOT DUPLICATE is the classic new-key test: print the first row of each duplicate group and any unique row. FIRST-DUP alone omits unique singles depending on logic—study Broadcom truth tables for your release. LAST-DUP keeps the final row in a group—useful after sorting date descending within account so last equals newest.
123456SORT MASTER TO SORTWRK USING (CUST-ID, EFF-DATE D) JOB INPUT SORTWRK IF LAST-DUP SORTWRK OR NOT DUPLICATE SORTWRK PUT OUTFILE END-IF
CUST-ID ascending groups customers. EFF-DATE D puts newest effective date first within customer—but LAST-DUP after that sort picks the first physical row in the group which is the newest date. Verify with sample data; some shops prefer FIRST-DUP after ascending date to keep oldest. Sort direction and FIRST/LAST choice must agree on business meaning.
123456789JOB INPUT SORTWRK IF FIRST-DUP SORTWRK OR NOT DUPLICATE SORTWRK WS-TOTAL = AMT ELSE WS-TOTAL = WS-TOTAL + AMT END-IF IF LAST-DUP SORTWRK OR NOT DUPLICATE SORTWRK PRINT SUM-RPT END-IF
Accumulate amounts between FIRST-DUP and LAST-DUP inclusive, then PRINT one summary line per key group. Control reports with SUM may alternatively total detail if you PRINT every row—choose one pattern to avoid double counting.
BEFORE can drop duplicate input before sort by tracking keys in working storage—heavy for large files. SELECT executed twice on the same record in one prescreen pass still includes that record once in sort output per Broadcom. SELECT then STOP on the same pass excludes the record. Use BEFORE to filter bad rows, not as a substitute for sort-key design.
Indexed WRITE with existing key may set DUPLICATE file status—that is keyed I/O. Record-relational DUPLICATE compares sequential adjacent records in JOB after SORT. KSDS generic duplicate reads are yet another topic. Keep vocabulary separate in design documents.
1234JOB INPUT SORTWRK IF DUPLICATE SORTWRK PRINT DUP-AUDIT END-IF
Audit reports flag rows participating in duplicate groups—every row after the first in a group satisfies DUPLICATE when sorted. Pair with FIRST-DUP counts on SYSPRINT via DISPLAY for operational metrics.
Sorting duplicate keys is lining up kids with the same birthday in a row. SORT does not send extras home—it only makes them stand together. FIRST-DUP points to the first kid in the birthday group; LAST-DUP points to the last. You decide who gets a prize—not the sort teacher.
1. Easytrieve SORT with equal keys:
2. IF DUPLICATE file-name is true when:
3. FIRST-DUP identifies:
4. SELECT twice on same record in BEFORE:
5. To process only one row per key after sort you typically: