SORT Tips.

A Mainframe-Tip a Day keeps the bugs away!
Forum rules
All of these Tips/Tuning-suggestions should be tested your own, at your shop, prior to use in Prod.
Previous topicNext topic
User avatar

Topic Author
Anuj Dhawan
Founder
Posts: 2624
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Zodiac: Sagittarius

SORT Tips.

Post by Anuj Dhawan » Thu Oct 03, 2013 7:04 pm

1. If you run the following job it will list your shop's installation defaults:

Code: Select all

//STEP01    EXEC PGM=ICETOOL 
//TOOLMSG   DD SYSOUT=*      
//DFSMSG    DD SYSOUT=*      
//OUT       DD SYSOUT=*      
//TOOLIN    DD *            
  DEFAULTS LIST(OUT)        
//*
This is valid for both DFSort and SyncSort. I'm not sure about CA-SORT, though.

Once you execute the above Job it'll give a list of parameters; depending on your specific need you might be interested in one or many parameters. For example, some of the fields of interest, usually, are DSA ,DYNALOC, DYNAUTO, DYNSPC, MAXLIM, MINLIM, MOSIZE.


Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.

User avatar

Topic Author
Anuj Dhawan
Founder
Posts: 2624
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Zodiac: Sagittarius

Re: SORT Tips.

Post by Anuj Dhawan » Fri Oct 11, 2013 12:13 pm

2. VB to FB conversion - Variable Block file to Fixed Block Conversion:

The VTOF or CONVERT and OUTREC operands of OUTFIL can be used to change variable-length (VB) input records to fixed-length (FB) output records. VTOF or CONVERT indicates that conversion is to be performed and OUTREC defines the reformatted records. All output data sets for which VTOF or CONVERT is used must have or will be given fixed-length record formats.

For example:

Code: Select all

//STEP001 EXEC PGM=ICEMAN
//SYSOUT DD SYSOUT=
//SORTIN DD 
SORT FIELDS=(7,8,CH,A)
OUTFIL FNAMES=FBFILE,VTOF,OUTREC=(5,84)
It is assumed that "there is no short record" in the VB file. If the records are shorter that is a bit different case and can be discussed later.


Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.


William Collins
Global Moderator
Global Moderator
Posts: 499
Joined: Sun Aug 25, 2013 7:24 pm

Re: SORT Tips.

Post by William Collins » Fri Oct 11, 2013 1:51 pm

OUTFIL FNAMES=FBFILE,VTOF,BUILD=(5,84) would be better. OUTREC on OUTFIL is best left for backwards compatibility only. BUILD is 100% identical and does not cause confusion with OUTREC itself, which it is still correct to use when needed.

Code: Select all

  INREC .... BUILD

  SORT .... FIELDS

  OUTREC .... BUILD

  OUTFIL ... BUILD
is much clearer than

Code: Select all

  INREC .... FIELDS

  SORT .... FIELDS

  OUTREC .... FIELDS

  OUTFIL .... OUTREC/FIELDS 



User avatar

Topic Author
Anuj Dhawan
Founder
Posts: 2624
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Zodiac: Sagittarius

Re: SORT Tips.

Post by Anuj Dhawan » Sun Oct 13, 2013 4:21 pm

That's a nice explanation and makes the concept very clear. Much appreciate your contribution William.

Regards,

PS.: And I observe, that did not work out well. I'll check that.


Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.


William Collins
Global Moderator
Global Moderator
Posts: 499
Joined: Sun Aug 25, 2013 7:24 pm

Re: SORT Tips.

Post by William Collins » Sat Nov 16, 2013 6:33 pm

3. The following step demonstrates the look-up function CHANGE. For the below example the input and output data-sets are assumed to be of FB/80.

Code: Select all

        //STEP001 EXEC PGM=SORT
        //SORTIN   DD DISP=SHR,DSN=HLQ.ORIGINAL.INPUT
        //SORTOUT  DD DISP=SHR,DSN=HLQ.CHANGED.OUTPUT
        //SYSOUT   DD SYSOUT=*
        //SYSIN    DD *
          SORT FIELDS=COPY
          INREC BUILD=(1,10,
                       56,4,CHANGE=(4,C'0100',C'0200'),
                                    NOMATCH=(C'0999'),
                       15,4,
                       55,1,CHANGE=(8,C'A',C'ANUJ',
                                      C'R',C'ROBERT',
                                      C'W',C'WILLIAM',
                                    NOMATCH=(19,1),
                       27,54,
                       80:X)
        /*

The first CHANGE= changes the text 0100 to 0200 for the data sourced from column 56 for a length of four. The length of the changed field is four bytes (this CHANGE will not increase or decrease the length of the original data). If the existing value is other than 0100 the following NOMATCH option changes all other values to 0999. If NOMATCH is not coded SORT will terminate if a value other than 0100 turns up.

The second CHANGE= shows how to change multiple values and also those of different lengths. One byte of the source record is given a length of 8 in the new BUILD record. Values which are changed but which are shorter than eight in length will be padded with space. The NOMATCH in this case leaves the value from the source unchanged, but pads it with space to a length of eight bytes.

As well as with BUILD, CHANGE can also be used with OVERLAY, but bear in mind that if the length of a field is changed, any data following the source field will be overlayed, so be certain that that is what you want.

From an idea by Anuj.



User avatar

Topic Author
Anuj Dhawan
Founder
Posts: 2624
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Zodiac: Sagittarius

Re: SORT Tips.

Post by Anuj Dhawan » Sat Nov 16, 2013 7:34 pm

Thanks William, this looks neat now. Much appreciate your time and efforts! :)

Regards,


Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.

User avatar

Topic Author
Anuj Dhawan
Founder
Posts: 2624
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Zodiac: Sagittarius

Re: SORT Tips.

Post by Anuj Dhawan » Wed Dec 04, 2013 2:24 pm

4. Replacing low values to spaces in Sort:

ALTSEQ CODE can be used to change the low-values or high-values to spaces in a file using Sort. Here’s an example of how you could change all low values (X’00′) to spaces (X’40′). For illustration, dataset is assumed as FB with an LRECL of 80:

Code: Select all

//STEP001 EXEC PGM=SORT
//SORTIN   DD DISP=SHR,DSN=HLQ.ORIGINAL.INPUT
//SORTOUT  DD DISP=SHR,DSN=HLQ.CHANGED.OUTPUT
//SYSOUT   DD SYSOUT=*
//SYSIN    DD *
    ALTSEQ CODE=(0040)
    OUTREC FIELDS=(1,80,TRAN=ALTSEQ)
/*


Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:03 pm

5. Grouping records in SORT

Code: Select all

//S1      EXEC PGM=SORT
//SYSPRINT  DD SYSOUT=*
//SYSOUT    DD SYSOUT=*
//SORTIN    DD *
1      AAAAA
2
3      BBBBB
4
5      CCCCC
6
7      CCCCC
8
9      ZZZZZ
10
//SORTOUT   DD SYSOUT=*,
//             DCB=(RECFM=FB,LRECL=80)
//SYSIN     DD *
  OPTION COPY
  INREC  IFTHEN=(WHEN=GROUP,BEGIN=(8,5,CH,NE,C'     '),PUSH=(30:8,5))
output

Code: Select all

1      AAAAA                 AAAAA
2                            AAAAA
3      BBBBB                 BBBBB
4                            BBBBB
5      CCCCC                 CCCCC
6                            CCCCC
7      CCCCC                 CCCCC
8                            CCCCC
9      ZZZZZ                 ZZZZZ
10                           ZZZZZ


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:08 pm

6.Find Match between two files using JOINKEYS

Code: Select all

//STEP0100 EXEC PGM=SORT
//SYSOUT   DD SYSOUT=*
//INA      DD *
AAA  123
BBB  345
CBB  345
DBB  345
EBB  345
FBB  345
//INB      DD *
AAA
BBB
CBB
DBB
EBB
FBB
//SORTOUT  DD SYSOUT=*
//SYSIN    DD *
  OPTION COPY
  JOINKEYS F1=INA,FIELDS=(1,3,A)
  JOINKEYS F2=INB,FIELDS=(1,3,A)
  REFORMAT FIELDS=(F1:1,10,F2:1,3)
//*
Output

Code: Select all

AAA  123  AAA
BBB  345  BBB
CBB  345  CBB
DBB  345  DBB
EBB  345  EBB
FBB  345  FBB


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:17 pm

7.Joinkeys to find the Matched,unmatched from both the files

Code: Select all

//STEP0100 EXEC PGM=SORT
//SYSOUT   DD SYSOUT=*
//INA      DD *
AAA  123
ABB  123
BBB  345
CBB  345
DBB  345
CCB  345
EBB  345
FBB  345
//INB      DD *
AAA
AAB
BBB
CBB
CDB
DBB
EBB
FBB
//SORTOUT  DD SYSOUT=*
//SYSIN    DD *
  OPTION COPY
  JOINKEYS F1=INA,FIELDS=(1,3,A)
  JOINKEYS F2=INB,FIELDS=(1,3,A)
  JOIN UNPAIRED F1,F2
  REFORMAT FIELDS=(F1:1,10,F2:1,10,?)
//*
Output

Code: Select all

----+----1----+----2--
AAA  123  AAA       B
          AAB       2
ABB  123            1
BBB  345  BBB       B
CBB  345  CBB       B
CCB  345            1
          CDB       2
DBB  345  DBB       B
EBB  345  EBB       B
FBB  345  FBB       B
Here B indicates key matched in both files,
1 indicates key present only in F1
2 indicates key present only in F2


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:25 pm

8.Join keys to find the matching data present in file F1 and also the data which is present in F1 and not in F2

For the above input

Code: Select all

//SYSIN    DD *
  OPTION COPY
  JOINKEYS F1=INA,FIELDS=(1,3,A)
  JOINKEYS F2=INB,FIELDS=(1,3,A)
  JOIN UNPAIRED F1
  REFORMAT FIELDS=(F1:1,10,?)
//*
Then All you need to do is write only those records that has B or 1


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:32 pm

9.Replace data between certain columns

Code: Select all

//STEP001 EXEC PGM=SORT
//SYSOUT  DD SYSOUT=*
//SORTIN  DD *
    1111567890
    2222678901
    3333789012
    4444890123
/*
//SORTOUT DD SYSOUT=*
//SYSIN   DD *
  SORT FIELDS=COPY
  OUTFIL FINDREP=(INOUT=(C'1',C'A',C'2',C'B',C'3',C'C',
                         C'4',C'D',C'5',C'E',C'6',C'F',
                         C'7',C'G',C'8',C'H',C'9',C'I',
                         C'0',C'J'),STARTPOS=9,ENDPOS=13)
Output

Code: Select all

    1111EFGHI0
    2222FGHIJ1
    3333GHIJA2
    4444HIJAB3


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:36 pm

10.Sort between header and trailer using Datasort

Code: Select all

//STEP0100  EXEC  PGM=ICETOOL
//TOOLMSG   DD SYSOUT=*
//DFSMSG    DD SYSOUT=*
//IN        DD *
HEADER
ACD;UV;
ADD;UV;
ACC;UP;
ACC;UV;
ABJ;UV;
ACC;UZ;
TRAILER
//OUT      DD  SYSOUT=*
//TOOLIN    DD *
  DATASORT FROM(IN) TO(OUT) HEADER TRAILER USING(CTL1)
//CTL1CNTL  DD *
  SORT FIELDS=(1,3,CH,A,5,2,CH,A)
output

Code: Select all

HEADER
ABJ;UV;
ACC;UP;
ACC;UV;
ACC;UZ;
ACD;UV;
ADD;UV;
TRAILER


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:40 pm

11.Find number of records and total for the given key using sort's COUNT and TOTAL

Code: Select all

//S1 EXEC PGM=SORT
//SORTIN DD *
AAA 1
BBB 2
AAA 1
AAA 3
AAA 2
//SORTOUT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSIN DD *
  SORT FIELDS=COPY
  OUTFIL FNAMES=SORTOUT,
  INCLUDE=(1,3,CH,EQ,C'AAA'),
  TRAILER1=(3/,'NUMBER OF RECORDS IS = ',
  COUNT=(TO=ZD,LENGTH=6),' TOTAL AAA FIELDS = ',
  TOTAL=(5,1,ZD,TO=ZD,LENGTH=6)),REMOVECC
/*
Output

Code: Select all

AAA 1
AAA 1
AAA 3
AAA 2

NUMBER OF RECORDS IS = 000004 TOTAL AAA FIELDS = 000007


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Thu Dec 12, 2013 8:44 pm

11.Date processing sample using SYMNAMES

Code: Select all

//STEP0100 EXEC PGM=SORT
//SYSOUT   DD SYSOUT=*
//SYMNAMES DD *
TODAY,S'&WDAY'
//SORTIN   DD *

//SORTOUT  DD SYSOUT=*
//SYSIN    DD *
  SORT FIELDS=COPY
  INREC IFTHEN=(WHEN=INIT,OVERLAY=(1:TODAY,DATE1(-)-1)),
  IFTHEN=(WHEN=(1,3,CH,EQ,C'THU'),OVERLAY=(4:DATE1(-)-3))
Output

Code: Select all

THU2013-12-09


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Mon Feb 17, 2014 9:04 pm

12.ICETOOL to select Unique records in one file and duplicate records in other file

Code: Select all

//STEP0100 EXEC PGM=ICETOOL 
//SYSOUT   DD SYSOUT=* 
//INPUT    DD * 
01 INDIA 
02 HOLLAND 
03 MEXICO 
04 NORWAY 
03 MEXICO 
05 BRAZIL 
//UNIQ     DD SYSOUT=* 
//DUPS     DD SYSOUT=* 
//TOOLMSG  DD SYSOUT=* 
//DFSMSG   DD SYSOUT=* 
//TOOLIN   DD * 
  SELECT FROM(INPUT) TO(UNIQ) ON(1,2,CH) NODUPS DISCARD(DUPS)


zprogrammer


zprogrammer
Global Moderator
Global Moderator
Posts: 605
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars
Zodiac: Libra

Re: SORT Tips.

Post by zprogrammer » Mon Feb 17, 2014 9:08 pm

13.A similar requirement like No.12 but to compare and write Matched records in one file(one after another) and Unmatched in other file one after the other

Code: Select all

//STEP0100 EXEC PGM=SORT 
//SYSOUT   DD SYSOUT=* 
//INA      DD * 
01 INDIA 
02 HOLLAND 
03 MEXICO 
04 NORWAY 
//INB      DD * 
03 MEXICO 
05 BRAZIL 
//OUT1     DD SYSOUT=* 
//OUT2     DD SYSOUT=* 
//SYSIN    DD * 
  OPTION COPY 
  JOINKEYS F1=INA,FIELDS=(1,2,A) 
  JOINKEYS F2=INB,FIELDS=(1,2,A) 
  JOIN UNPAIRED F1,F2,ONLY 
  REFORMAT FIELDS=(F1:1,10,F2:1,10) 
  OUTREC IFTHEN=(WHEN=INIT,BUILD=(1:1,21,JFY=(SHIFT=LEFT))) 
  OUTFIL FNAMES=OUT1,INCLUDE=(11,10,CH,NE,C' '),BUILD=(1,10,/11,10) 
  OUTFIL FNAMES=OUT2,SAVE 
//*


zprogrammer

Previous topicNext topic

Return to “Tip Of the Day.”