The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →If both input data sets are already sorted on the same key, use DFSORT’s MERGE operation. If either file is unsorted, concatenate them under one SORTIN DD and use SORT, or sort each file first and then merge the sorted results. A DFSORT merge preserves records; it does not automatically join, deduplicate, or aggregate them.
DFSORT is IBM’s z/OS utility for sorting, merging, and copying data sets. See the IBM DFSORT overview.
As an Amazon Associate I earn from qualifying purchases.
Choose the operation first
| Requirement | Use |
|---|---|
| Both files are sorted identically by the output key | MERGE with SORTIN01, SORTIN02, and so on |
| One or both files may be unsorted | Concatenated SORTIN with SORT |
| Each input must be prepared independently | Sort each input, then run MERGE |
| Match records and combine fields by key | JOINKEYS, not a simple merge |
| Business logic and file procedures belong in an application | COBOL MERGE or SORT |
IBM’s definition of DFSORT MERGE requires previously sorted input data sets. Do not rely on the sometimes-repeated claim that only one input needs to be sorted; that is not a safe rule for DFSORT. See IBM’s guidance on merging previously sorted data sets.
Merge two already-sorted files
This example assumes fixed-format records, both files sorted ascending by a four-byte character key at position 1, and compatible record layouts.
#1 Best Overall
- Murach's Mainframe COBOL
- Mike Murach & Associates
- ABIS BOOK
//MERGE01 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN01 DD DSN=USER.FILE1,DISP=SHR
//SORTIN02 DD DSN=USER.FILE2,DISP=SHR
//SORTOUT DD DSN=USER.MERGED.FILE,
// DISP=(NEW,CATLG,DELETE),
// UNIT=SYSDA,
// SPACE=(CYL,(5,5)),
// DCB=*.SORTIN01
//SYSIN DD *
MERGE FIELDS=(1,4,CH,A)
/*
A merge job uses one numbered DD statement for each input stream: SORTIN01, SORTIN02, and, when needed, SORTIN03 or higher. Do not replace those separate inputs with one concatenated SORTIN unless you intend to run a full sort instead.
What the control statement means
1is the key’s starting byte.4is its length.CHinterprets the key as character data.Arequests ascending order.
The key definition must match every input’s actual layout and sort order. Other examples include MERGE FIELDS=(20,8,ZD,D) for an eight-byte zoned-decimal key in descending order, or a composite key such as MERGE FIELDS=(1,4,CH,A,20,8,ZD,D). Character, zoned-decimal, and packed-decimal fields are not interchangeable interpretations.
Example result
If the first file contains keys 0003, 0005, and 0008, and the second contains 0002, 0006, and 0009, the output key sequence is 0002, 0003, 0005, 0006, 0008, 0009. Records with equal keys from both files are retained; DFSORT does not promise a particular cross-file order for equal keys unless your design establishes one.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Sort when the inputs are unsorted
Concatenate both data sets as one logical input and let DFSORT perform a complete sort:
Rank #2
//SORT01 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=USER.FILE1,DISP=SHR
// DD DSN=USER.FILE2,DISP=SHR
//SORTOUT DD DSN=USER.COMBINED.SORTED,
// DISP=(NEW,CATLG,DELETE),
// UNIT=SYSDA,
// SPACE=(CYL,(5,5)),
// DCB=*.SORTIN
//SYSIN DD *
SORT FIELDS=(1,4,CH,A)
/*
This is usually the safest single-step solution when input ordering is unknown. It reads every record and sorts the combined set, rather than exploiting pre-sorted streams.
Sort each file, then merge
Use separate sort steps when inputs are independently produced, need separate validation, or are part of a larger pipeline:
//SORTA EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=USER.FILE1,DISP=SHR
//SORTOUT DD DSN=&&FILE1S,DISP=(,PASS),UNIT=SYSDA,SPACE=(CYL,(5,5))
//SYSIN DD *
SORT FIELDS=(1,4,CH,A)
/*
//SORTB EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN DD DSN=USER.FILE2,DISP=SHR
//SORTOUT DD DSN=&&FILE2S,DISP=(,PASS),UNIT=SYSDA,SPACE=(CYL,(5,5))
//SYSIN DD *
SORT FIELDS=(1,4,CH,A)
/*
//MERGE01 EXEC PGM=SORT
//SYSOUT DD SYSOUT=*
//SORTIN01 DD DSN=&&FILE1S,DISP=(OLD,DELETE)
//SORTIN02 DD DSN=&&FILE2S,DISP=(OLD,DELETE)
//SORTOUT DD DSN=USER.FINAL.MERGED,DISP=(NEW,CATLG,DELETE),
// UNIT=SYSDA,SPACE=(CYL,(5,5))
//SYSIN DD *
MERGE FIELDS=(1,4,CH,A)
/*
For two modest files, concatenated SORT is generally simpler. The three-step pattern is useful when each sorted intermediate data set is reusable or independently maintained.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallFiltering and reformatting
DFSORT can process records during a merge with statements such as INCLUDE, OMIT, INREC, OUTREC, and OUTFIL. For example:
Rank #3
//SYSIN DD *
MERGE FIELDS=(1,4,CH,A)
INCLUDE COND=(30,1,CH,EQ,C'Y')
/*
The condition must match the real record layout. If an INREC transformation changes the fields used for ordering, ensure that each input is still consistently sorted for the key DFSORT evaluates.
Duplicates, joins, and incompatible layouts
Duplicate keys
MERGE is not deduplication. If both files contain a key, both records normally appear. Suppressing duplicates or choosing a preferred record requires additional DFSORT processing or another design.
When you need a join
If the requirement is to match customer IDs, produce matched-only or file-specific records, or combine fields from both files, use DFSORT JOINKEYS rather than MERGE. IBM’s DFSORT documentation index covers join processing at this DFSORT documentation page.
Different record structures
Two files cannot be merged safely merely because each contains something that looks like an ID. Normalize layouts with INREC/OUTREC or preprocess them separately. Confirm fixed- versus variable-blocked records, logical record lengths, and the position of any variable-record data before treating positions such as byte 1 as universal.
ICETOOL alternative
ICETOOL is useful when the job also needs counts, statistics, reports, or multiple tool operations. A simple two-file merge can be written as:
//MRG EXEC PGM=ICETOOL
//TOOLMSG DD SYSOUT=*
//DFSMSG DD SYSOUT=*
//IN1 DD DSN=USER.FILE1,DISP=SHR
//IN2 DD DSN=USER.FILE2,DISP=SHR
//OUT DD DSN=USER.MERGED.FILE,DISP=(NEW,CATLG,DELETE),
// UNIT=SYSDA,SPACE=(CYL,(5,5))
//TOOLIN DD *
MERGE FROM(IN1,IN2) TO(OUT) USING(CTL1)
/*
//CTL1CNTL DD *
MERGE FIELDS=(1,4,CH,A)
/*
IBM documents this FROM/TO/USING pattern in its merge example. Keep both TOOLMSG and DFSMSG so diagnostic messages are available.
Troubleshoot a failed or incorrect merge
- Input is not ordered: sort the offending data set, sort both inputs independently, or use concatenated
SORT. - Key definition differs: verify positions, lengths, numeric formats, collating rules, and ascending/descending sequence in every producer job.
- Wrong DD names: use numbered
SORTINnnDDs for a true merge; use one concatenatedSORTINdeliberately for a full sort. - Variable records behave unexpectedly: review VB record handling and whether your positions refer to logical data or include record-control information.
- DCB attributes conflict: check
RECFM,LRECL,BLKSIZE, and character or numeric representation; reformat where necessary. - Output allocation is too small: allow for approximately the combined input volume unless filtering or transformation changes it.
- Unexpected duplicates: remember that equal keys are retained; a merge is not a unique-key operation.
- Job status is unclear: inspect the step return code,
SYSOUT, and DFSORT messages. An older tutorial associates unsorted input withS000 U0016, but abend and message behavior depends on release and context; use the messages from your installed DFSORT level rather than assuming that code is universal.
IBM documentation is organized by z/OS release, including 3.1.0 and 3.2.0 pages. Check the manuals and product level installed at your site before relying on release-specific limits or behavior. IBM’s COBOL distinction is also useful: COBOL SORT accepts unsorted input, while COBOL MERGE combines sequenced files; see Enterprise COBOL sorting and merging and COBOL request syntax.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesQuick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




