Page 1 of 2

sort accending depends on a field value

PostPosted: Thu Aug 16, 2012 2:41 am
by helen2000
hello, I have a file with 3 fields (AA, SIN, BN), I sort the records as the following rules:
if AA=60 , sort on field SIN; if AA=61, sort on field BN, for example:
the source file is:
AA SIN BN
61 555 777
60 222 111
61 333 555
60 444 666
after sort should be :
AA SIN BN
61 333 555
60 222 111
61 555 777
60 444 666
thanks in advance,

Helen

Re: sort accending depends on a field value

PostPosted: Thu Aug 16, 2012 4:26 am
by BillyBoyo
LRECL and RECFM? Positions of fields?

It is quite simple. Add a new field, within sort, to your record. Put a value in this field depending on the AA value. Sort the file on the new field. Ignore the new field for output as no longer required.

Re: sort accending depends on a field value

PostPosted: Thu Aug 16, 2012 6:53 pm
by helen2000
thanks for your useful information,

sorry, I make a mistake, should be:
If the before sort file is like this:
AA SIN BN
61 555 555
60 222 111
61 333 777
60 444 666


The result I want is like this:
AA SIN BN
60 222 111
60 444 666
61 555 555
61 333 777

Sort by: 1. AA
2. If AA=60 then sort by SIN
If AA=61 then sort by BN

Re: sort accending depends on a field value

PostPosted: Thu Aug 16, 2012 6:55 pm
by BillyBoyo
You missed the lrecl/recfm information.

Re: sort accending depends on a field value

PostPosted: Thu Aug 16, 2012 8:01 pm
by helen2000
sorry, Mr. BIllyboyo, LRECL=80, RECFM=FB, I have a solution for the sort, but it's not a better one, as I have to have 2 times sort,


1: first sort by AA, SIN
2: condition sort by BN, ex, include cond=(1,2,ch,eq,cā€™61ā€™) sort fields=(25,10,ch,A)
anybody can sort just one time for the issue, thanks,

Re: sort accending depends on a field value

PostPosted: Thu Aug 16, 2012 8:49 pm
by dick scherrer
Hello,

It is quite simple. Add a new field, within sort, to your record. Put a value in this field depending on the AA value. Sort the file on the new field. Ignore the new field for output as no longer required.
Please re-read this from BB's first reply. This explains how to get what you want in one pass, i believe . . .

Re: sort accending depends on a field value

PostPosted: Thu Aug 16, 2012 9:25 pm
by skolusu
helen,

Assuming that SIN starts in position 15 for 10 bytes and BIN starts in position 25 for 10 bytes, the following DFSORT JCL will give you the desired results. By default we pick the sort field as SIN and only for records with 61 in pos 1 we pick BIN as sort field.

//STEP0100 EXEC PGM=SORT                             
//SYSOUT   DD SYSOUT=*                               
//SORTIN   DD *                                     
----+----1----+----2----+----3----+----4----+----5---
61            555       555                         
60            222       111                         
61            333       777                         
60            444       666                         
//SORTOUT  DD SYSOUT=*                               
//SYSIN    DD *                                     
  INREC IFTHEN=(WHEN=INIT,OVERLAY=(81:15,10)),       
  IFTHEN=(WHEN=(1,2,CH,EQ,C'61'),OVERLAY=(81:25,10))
  SORT FIELDS=(81,10,CH,A),EQUALS
  OUTREC BUILD=(1,80)                   
//*

Re: sort accending depends on a field value

PostPosted: Fri Aug 17, 2012 4:43 am
by helen2000
thank you for your help, but still problem,
jcl is as following:
000019 //SORTIN  DD DSN=MP.TESTIN,DISP=SHR
000020 //SORTOUT DD DSN=MP.TESTOUT,
000021 //           DISP=(NEW,CATLG,DELETE),
000022 //           UNIT=SYSDA,SPACE=(CYL,(20,50),RLSE)
000023 //SYSIN  DD *
000024   INREC IFTHEN=(WHEN=INIT,OVERLAY=(81:15,10)),
000025   IFTHEN=(WHEN=(1,2,CH,EQ,C'61'),OVERLAY=(81:25,10))
000026   SORT FIELDS=(81,10,CH,A),EQUALS
000027   OUTREC BUILD=(1,80)
000028 //*


my input file is:
000001 61            555       555
000002 60            222       111
000003 61            333       777
000004 60            444       666
000005 60            555       222
000006 60            111       123
000007 61            546       211

output is:
000001 60            111       123
000002 60            222       111
000003 61            333       777
000004 60            444       666
000005 61            546       211
000006 61            555       555
000007 60            555       222


but my expected output is:

000001 60            111       123
000002 60            222       111
000004 60            444       666
000007 60            555       222
000005 61            546       211
000006 61            555       555
000003 61            333       777


thanks again,

helen

Re: sort accending depends on a field value

PostPosted: Fri Aug 17, 2012 6:20 am
by BillyBoyo
Is it so much trouble to get to

  SORT FIELDS=(1,2,CH,A,81,10,CH,A),EQUALS


?

Re: sort accending depends on a field value

PostPosted: Fri Aug 17, 2012 7:16 am
by dick scherrer
Hi Helen,

Do notice that your control statements do not sort on the 60/61 field. . .

Run what BB has provided and let us know. . .