Page 1 of 1

How to sort groups of records based on the header

PostPosted: Mon Feb 23, 2009 10:13 pm
by Irene Ioujanina
Hi, May I please ask again. If I have groups of records: each group starts with one record of the header followed by one to many records of details. I would like the order of records within the group does NOT change, but all groups will be ordered by the header value, so the groups with the same headers will be together. If the header is the same - it doesn't matter which group comes first, but the details records inside the group of each header will not be changed.

Thanking in advance,
Input:
---------
header A2
data1
data2
data3
header A3
data4
data5
header A1
data6
data7
header A3
data8
header A2
data9

( the headers start with the constant 'header' and it is sorted by the pos 8-9 in the header )

Output:
---------
header A1
data6
data7
Header A2
data1
data2
data3
header A2
data9
header A3
data4
data5
header A3
data8

Irene

Re: How to sort groups of records based on the header

PostPosted: Mon Feb 23, 2009 11:03 pm
by skolusu
Irene Ioujanina ,

You can use the technique discussed in the "Sort groups of records" Smart DFSORT Trick at:

http://www.ibm.com/systems/support/stor ... vs/tricks/

Re: How to sort groups of records based on the header

PostPosted: Mon Feb 23, 2009 11:20 pm
by Irene Ioujanina
Thank you for your responce. It seems it is a diferent situation: they sorted the records within each group,-and I want the records within each group are NOT sorted, but the groups (with the unsorted records) will be sorted based on the header value.
for instance- the input:
header A2
9999999
1111111
22222222
header A1
44444
33333
header A2
555555

will became as output:
header A1
44444
33333
header A2
9999999
1111111
2222222
header A2
555555

Thank you anyway for providing a very useful link to the sort tricks :)

Re: How to sort groups of records based on the header

PostPosted: Tue Feb 24, 2009 12:01 am
by Frank Yaeger
You can use a DFSORT job like the following to do what you asked for. I assumed your input file has RECFM=FB and LRECL=80, but the job can be changed appropriately for other attributes.

//S1    EXEC  PGM=SORT
//SYSOUT    DD  SYSOUT=*
//SORTIN DD *
header A2
9999999
1111111
22222222
header A1
44444
33333
header A2
555555
/*
//SORTOUT DD SYSOUT=*
//SYSIN    DD    *
  OPTION EQUALS
  INREC IFTHEN=(WHEN=GROUP,BEGIN=(1,6,CH,EQ,C'header'),
    PUSH=(81:8,2))
  SORT FIELDS=(81,2,ZD,A)
  OUTFIL BUILD=(1,80)
/*

Re: How to sort groups of records based on the header

PostPosted: Tue Feb 24, 2009 12:28 am
by Irene Ioujanina
Thank you so much. :)