MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sum positive and negative amounts separately

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Utilities
View previous topic :: View next topic  
Author Message
Telbont
Beginner


Joined: 28 Apr 2008
Posts: 4
Topics: 1

PostPosted: Wed Sep 24, 2008 3:52 pm    Post subject: Sum positive and negative amounts separately Reply with quote

Is it possible using SORT utility (have SYNCSORT) to sum same key field records based on sign of the sum field i.e. to sum positive and negative records separately for a given key? The sum field is a S9(11)V99 zoned decimal field.
Back to top
View user's profile Send private message
Sqlcode
Intermediate


Joined: 15 Dec 2006
Posts: 157
Topics: 38

PostPosted: Thu Sep 25, 2008 8:54 am    Post subject: Reply with quote

Telbont,

Could you please post some test records and expected output?

Thanks,
Back to top
View user's profile Send private message
edkir98
Beginner


Joined: 27 Aug 2007
Posts: 102
Topics: 42
Location: Chennai

PostPosted: Wed Oct 01, 2008 8:18 am    Post subject: Reply with quote

Try this
Code:
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
                           
//R010    EXEC PGM=SORT                                                 
//SORTIN    DD *                                                       
11111   +10                                                             
11111   -05                                                             
11111   -02                                                             
22222   +20                                                             
33333   +30                                                             
44444   +40                                                             
55555   +50                                                             
//SORTOUT   DD SYSOUT=*                                                 
//SYSIN     DD *                                                       
  SORT FIELDS=(1,5,CH,A,9,1,CH,A)                                       
   SUM FIELDS=(10,2,ZD)                                                 
//SYSOUT    DD SYSOUT=*                                                 

this will give the following output

Code:
************
11111   +10
11111   -07
22222   +20
33333   +30
44444   +40
55555   +50
************

_________________
Thanks
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Wed Oct 01, 2008 11:13 am    Post subject: Reply with quote

edkir98,

A field defined as S9(11)V99 zoned decimal field will have the sign overpunched on the last byte.
Code:

{ABCDEFGHI}JKLMNOPQR
CCCCCCCCCCDDDDDDDDDD
01234567890123456789


Hex C0 thru C9 are positive numbers and D0 thru D9 are negative numbers. So -5 would look 000000000000N

Hope this helps...

Cheers
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Telbont
Beginner


Joined: 28 Apr 2008
Posts: 4
Topics: 1

PostPosted: Tue Oct 07, 2008 1:08 pm    Post subject: Reply with quote

The output shown by edkir98 is exactly what I want except that the numeric field is defined as shown by kolusu so the sign is embedded on the last byte. Maybe I should try to include a bit field in the sort key such that hex C and hex D would separate out the positive and negative records.....
Back to top
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Thu Oct 09, 2008 7:37 pm    Post subject: Reply with quote

Assuming key is from columns 1 to 5 and data from 6 to 18 you can modify the sort in http://www.mvsforums.com/helpboards/viewtopic.php?t=2593&highlight=sort+cond as -

Code:

    SORT FIELDS=(1,5,CH,A)
    SUM FIELDS=(6,13,ZD)
    OUTFIL FNAMES=SOUT0,INCLUDE=(18,1,ZD,GE,0)
    OUTFIL FNAMES=SOUT1,INCLUDE=(18,1,ZD,LT,0)


This will produce two ouputs, one for positive and one for negative values. You can merge them again if required.
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Oct 09, 2008 8:02 pm    Post subject: Reply with quote

Dibakar,

Did you really test the solution you posted? OUTFIL is processed after your sort and sum operations. When the outfil is processed the postive and negative values are already summed as one single record for each key.

for ex: if you have this input
Code:
                                                       
11111   +10
11111   -05
11111   -02
22222   +20
33333   +30
44444   +40
55555   +50


By the time outfil is processed you have these records

Code:
                                                       
11111   +03 (+10-05-02= +03)     
22222   +20
33333   +30
44444   +40
55555   +50


But Op wants the key 1111 as 2 separate records

Code:
                                                       
11111   +10
11111   -07 (-05-02=  -07)
22222   +20
33333   +30
44444   +40
55555   +50

_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Sat Oct 11, 2008 9:39 am    Post subject: Reply with quote

kolusu,

I realised the mistake but before I could correct you had pointed out the mistake. No I didn't test the code, don't have access to mainframe anymore. Should have given a disclaimer. I will try to correct it.

Dibakar.
Back to top
View user's profile Send private message Send e-mail
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Sat Oct 11, 2008 10:54 am    Post subject: Reply with quote

Telbont,

You can add two sort steps as follows -

Code:

//SORTPOS ...
...
//SYSIN DD *
 SORT FIELDS=(1,5,CH,A)
     SUM FIELDS=(6,13,ZD)
     INCLUDE COND=(18,1,ZD,GE,0)
//*
//SORTNEG ...
...
//SYSIN DD *
 SORT FIELDS=(1,5,CH,A)
     SUM FIELDS=(6,13,ZD)
     INCLUDE COND=(18,1,ZD,LT,0)

Note:
1 This code is not tested
2 You can add another step if you want to merge and sort these outputs

Regards,
Diba.
Back to top
View user's profile Send private message Send e-mail
Telbont
Beginner


Joined: 28 Apr 2008
Posts: 4
Topics: 1

PostPosted: Thu Oct 16, 2008 9:15 am    Post subject: Reply with quote

Thanks all for the responses and time spent. I used 3 steps as suggested by Dibakar and achieved the end result - 2 SORT INCLUDES and 1 MERGE step.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Oct 16, 2008 10:25 am    Post subject: Reply with quote

Telbont,

Since you asked specifically for syncsort solution I haven't posted it, however I just feel it is an overkill as to what you are doing. Here is a 1 step solution which would give you the desired results

Assuming that your input is FB 80 bytes lrecl , just add an indicator at the end if the value is less than zero. Now sort on the key and the indicator at the end and sum the values. After summation just remove the additional indicator using OUTREC

Code:

//SYSIN    DD *                                       
  INREC IFTHEN=(WHEN=(10,13,ZD,LT,0),OVERLAY=(81:C'N'))
  SORT FIELDS=(1,5,CH,A,81,1,CH,A)                     
  SUM FIELDS=(10,13,ZD)                               
  OUTREC BUILD=(1,80)                                 
/*

_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Telbont
Beginner


Joined: 28 Apr 2008
Posts: 4
Topics: 1

PostPosted: Wed Feb 25, 2009 1:42 pm    Post subject: Reply with quote

Thanks for the one step approach which works fine and is certainly more efficient than a 3 steps sort merge. Never knew much about the IFTHEN conditional keyword so a lot learnt on this topic.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Utilities All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group