MERGE Files


The MERGE operation is used to combine multiple sorted datasets into a single sorted dataset. Unlike SORT, MERGE assumes that the input datasets are already sorted and simply merges them into a final output in sorted order based on specified key fields.

The purpose of SORT merge -

  • Combines multiple sorted datasets
  • Maintains sorting order

Syntax -

//SYSIN DD * 
    SORT FIELDS=(field1_starting_position, field1_length, field1_format,
		 A or D, ...)
/*
field1_starting_positionSpecifies the starting position of field1 in the file
field1_lengthSpecifies the length of the field1 used for sorting.
field1_formatSpecifies the format of the field1.
A or DSpecifies the sorting order. A for ascending and D for descending.

Rules to Remember -

  • All input datasets must be pre-sorted on the key fields that you specify in the MERGE operation. If the input datasets are not sorted, the MERGE will not work correctly.
  • Each dataset should have similar record formats.

Examples -


Scenario1 - Simple MERGE on a Character Field.

//STEP01  EXEC PGM=SORT
//SORTIN01 DD DSN=MATEPK.INPUT.FILE1,DISP=SHR
//SORTIN02 DD DSN=MATEPK.INPUT.FILE2,DISP=SHR
//SORTOUT DD  DSN=MATEPK.OUTPUT.FILE,
//             DISP=(NEW,CATLG,DELETE),
//             SPACE=(CYL,(100,10)),UNIT=SYSDA
//SYSIN    DD *
  SORT FIELDS=(1,10,CH,A)
/*

This merges two sorted datasets based on a 10-character field starting at position 1, in ascending order. The input datasets MATEPK.INPUT.FILE1 and MATEPK.INPUT.FILE2 must already be sorted on the same key fields. The merged output is written to MATEPK.OUTPUT.FILE.

Scenario2 - MERGE with Multiple Key Fields.

//STEP01  EXEC PGM=SORT
//SORTIN01 DD  DSN=MATEPK.INPUT.FILE1,DISP=SHR
//SORTIN02 DD  DSN=MATEPK.INPUT.FILE2,DISP=SHR
//SORTOUT DD  DSN=OUTPUT.DATASET,DISP=(NEW,CATLG,DELETE),
//             SPACE=(CYL,(100,10)),UNIT=SYSDA
//SYSIN   DD  *
  SORT FIELDS=(1,5,CH,A,10,3,ZD,D)
/*

The first key is a 5-character field starting at position 1, sorted in ascending order. The second key is a 3-byte Zoned Decimal (ZD) field starting at position 10, sorted in descending order. The datasets are merged based on these two key fields.