DFSORT – Merge two files using SORT

Advertisement

This tutorial will show you how to merge two files OR two sets of records using IBM DFSORT.

If you want to learn the basic workings of DFSORT before going through the MERGE process, go through below post:
DFSORT – A simple example

For merging, we need two sets of data. 1st set of data needs to be sorted in order to merge successfully. 2nd set of data need not to be sorted.

If the first set of data is not sorted, your JOB shall abend with S000 U0016. To resolve this abend – make sure your 1st set of data is sorted.

Lets say, we have below records for 1st set. They are employee name, employee salary and employee department.

1ST INPUT DATA SET: SORTIN01

----+----1----+----2----+----3----+----4----+----5
SAM   11234 ACCOUNTS
JOHN  78916 IT
STEVE 62541 ACCOUNTS
TIM   90013 HR

You can see, they are sorted, because, SORT needs 1st set of records to be sorted to perform MERGE function.

And the 2nd set of data is –
2ND INPUT DATA SET: SORTIN02

----+----1----+----2----+----3----+----4----+----5
ANDY  56734 IT

Now, we have both the sets of data ready to be merged. To merge the both the datasets, the statement should be same as the SORT, only difference is the keyword ‘MERGE’ to be used instead of SORT.

MERGE FIELDS=(13,8,CH,A,1,5,CH,A,7,5,CH,A)

COMPLETE JCL:

//YOUR.JOB.CARD.HERE
//STEP01 EXEC PGM=SORT
//SORTIN01 DD *
SAM   11234 ACCOUNTS
JOHN  78916 IT
STEVE 62541 ACCOUNTS
TIM   90013 HR
/*
//SORTIN02 DD *
ANDY  56734 IT
/*
//SORTOUT DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
//SYSIN DD *
MERGE FIELDS=(13,8,CH,A,1,5,CH,A,7,5,CH,A)
/*

OUTPUT:

SAM   11234 ACCOUNTS
STEVE 62541 ACCOUNTS
TIM   90013 HR
ANDY  56734 IT
JOHN  78916 IT

Drop a comment if you like this article or face any problem using the above code.

You may also like...

Translate »