DFSORT Mainframe Code Examples

   

  Asked by Anonymous - DFSORT Examples:
Below are some mainframe cheat sheet code examples for using IBM sort utility DFSORT on mainframe.

This example will sum a packed decimal field which is 10 bytes stored on the input file and sum based on the 3 sort fields identified.  It also shows dynamic allocation and comment field (no sort work files should be specified with dynamic allocation otherwise they will take priority over this).
//SYSIN DD *
    SORT FIELDS=(35,4,CH,A,
                                42,8,CH,A,
                                92,1,CH,A)
    SUM FIELDS=(72,10,PD)         *dollar amount - always comment fields like this!
    OPTION DYNALLOC=(SYSDA,6)

Anonymous Says:
This example will copy the first 155 rows only from the input file to output using STOPAFT

//SYSIN DD *
    SORT FIELDS=COPY
    OPTION STOPAFT=155

This DFSORT example will copy only specific rows from input and write only specfic columns to output file. It will also pad 50 spaces on end of row.


//SYSIN DD *
    SORT FIELDS=COPY
    INCLUDE COND=(110,4,CH,EQ,'TEST')
    OUTREC FIELDS=(1:32,5,         * This means in pos 1 of output, copy from input byte pos 32 for 5 bytes
                                      6:142,10,
                                    17:50X)

Anonymous Says:
Example using AND condition along with showing equal example along with not equal sort example.

//SYSIN DD *
    SORT FIELDS=COPY
    INCLUDE COND=((110,4,CH,EQ,C'TEST'),AND,
                                     (120,1,CH,NE,C'5'))


Sort example using OR condition.  Output result will not contain any other rows other then what the include condition specifies.

//SYSIN DD *
    SORT FIELDS=COPY
    INCLUDE COND=(110,4,CH,EQ,C'TEST',OR,
                                     120,1,CH,NE,C'5')

Anonymous Says:
Sort file in order specified but also get rid of any duplicate rows.  Duplicates are based on sort fields specified.  Note that when duplicates are removed based of "SUM FIELDS=NONE", the first row is the one kept on output result.

//SYSIN DD *
    SORT FIELDS=(110,20,CH,A,175,2,CH,A)   *Sort ascending pos 110 for 20 and 175 for 2
    SUM FIELDS=NONE                                     *This gets rid of duplicates
    OPTION DYNALLOC=(SYSDA,6)


Anonymous Says:
You can save the eliminated duplicate rows from a sort when using SUM FIELDS=NONE.  Just create a DD file in your JCL as shown below and identify it on the sort line as shown below.

//SORTXSUM DD DSN=.............

//SYSIN DD *
    SORT FIELDS=(110,20,CH,A,175,2,CH,A)   *Sort ascending pos 110 for 20 and 175 for 2
    SUM FIELDS=NONE,XSUM                         *This will then save all eliminated duplicates to the file specified.
    OPTION DYNALLOC=(SYSDA,6)

Anonymous Says:
When removing duplicates in the sort using SUM FIELDS=NONE, which keeps the first row only as duplicates get discarded, you can also use:

OPTION EQUALS - Keeps the same input sequence of duplicate rows when sorting, first row is the one kept.
OPTION NOEQUALS - Specifies that the original sequence does not need to be kept and can be unpredicatable.

Anonymous Says:
How to round with DFSORT - Sort always truncates the final result.  For example if you want rounding with sort you can just add .5 to the result, which then gives you the same effect since its truncated.

Anonymous Says:
The same way you INCLUDE rows in the examples above, so the output file only has the rows specified, you can also OMIT rows the same way.  The output file will have all rows except for those you omitted.

//SYSIN DD *
    SORT FIELDS=COPY
    OMIT COND=(110,4,CH,EQ,C'TEST')

Anonymous Says:
Here is a mainframe code example of DFSORT doing many things.  It shows a combination of AND clause and OR clause on the include condition statement with parens used.  It also shows output writing different bytes from the input file, and it multiplies a packed decimal field on the input and writes a packed decimal field to the output. It also writes a hardcoded value TEST in position 21 of the output file, and also writes a 3 byte signed HEX zero value at the end of every line.


//SYSIN DD *
    SORT FIELDS=COPY
    INCLUDE COND=((110,4,CH,EQ,C'TEST',OR,
                                       110,2,CH,EQ,C'SA'),AND,
                                       225,2,CH,EQ,C'X4')
    OUTREC FIELDS=(1:240,10,
                                     11:170,10,PD,MUL,-1,TO=PD,LENGTH=10,
                                     21:C'TEST',
                                     25:X'00000C')

Anonymous Says:
Here is an example of dfsort sorting in ascending order a 5 byte character field starting in position 10 of the input, and 4 byte binary field in position 29.  Along with that if position 105 is space or low values in that one byte, it will overlay it with B

SORT FIELDS=(10,5,CH,A,29,4,BI,A)
INREC IFTHEN=(WHEN=(105,1,CH,LE,C' ')
            ,OVERLAY=(105:C'B'))

Anonymous Says:
Mainframe example sorting different field types. This is sorting a character field, sorting binary field, sorting zoned decimal field descending, and sorting packed decimal field.  All are ascending order except the ZD field.

SORT FIELDS=(34,3,CH,A
                           ,40,4,BI,A
                           ,51,10,ZD,D
                           ,88,10,PD,A)

Anonymous Says:
Comparison operators sort example.

Comparing 2 binary fields in different file positions:

Greater than:
INCLUDE COND=(10,4,BI,GT,180,4,BI)

Equal condition:
INCLUDE COND=(10,4,BI,EQ,180,4,BI)

Not equal condition:
INCLUDE COND=(10,4,BI,NE,180,4,BI)

Greater than or equal to:
INCLUDE COND=(10,4,BI,GE,180,4,BI)

Less than:
INCLUDE COND=(10,4,BI,LT,180,4,BI)

Less than or equal to:
INCLUDE COND=(10,4,BI,LE,180,4,BI)

Anonymous Says:
Include condition examples shows char, packed decimal, zoned decimal, and binary.

Character equal value
INCLUDE COND=(11,5,CH,EQ,C'523BC')

Packed decimal value (10 bytes total packed) equal zero
INCLUDE COND=(322,10,PD,EQ,0)

Zoned decimal value equal zero
INCLUDE COND=(322,10,ZD,EQ,0)

Only include binary values on file input in position 322 that are 10 bytes binary and greater than zero
INCLUDE COND=(322,10,BI,GT,0)


Add your reply below ...


Your Nickname:

Your Reply:

Type the code: