Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk5 min

DFSORT: Merge Two Files with MERGE or SORT

Use DFSORT MERGE for inputs already sorted on the same key; use concatenated SORT when either file is unsorted. Includes working JCL and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

//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

  • 1 is the key’s starting byte.
  • 4 is its length.
  • CH interprets the key as character data.
  • A requests 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sort when the inputs are unsorted

Concatenate both data sets as one logical input and let DFSORT perform a complete sort:

//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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Filtering and reformatting

DFSORT can process records during a merge with statements such as INCLUDE, OMIT, INREC, OUTREC, and OUTFIL. For example:

//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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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 SORTINnn DDs for a true merge; use one concatenated SORTIN deliberately 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 with S000 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.