Mario,

Max has the right idea but it is a overkill with 2 passes of data.  Here is a 
simpler version with just 1 pass.   Since the data is NOT sorted, I added a 
SORT statement.

I assumed that the product has a length of 5 bytes and starts in position 1. I 
also added lot of comments so that it is easy to understand the job.

//STEP0100 EXEC PGM=SORT
//SYSOUT   DD SYSOUT=*
//SORTIN   DD *
SUGAR,2022/08/01,100
SUGAR,2022/08/02,97
SUGAR,2022/08/03,93
XXXXX,2022/08/03,20
YYYYY,2022/08/01,150
YYYYY,2022/08/01,140
ABCDE,2022/07/02,140
ABCDE,2022/07/03,120
//SORTOUT  DD SYSOUT=*
//SYSIN    DD *
  SORT FIELDS=(01,16,CH,A),EQUALS

** Tag records based on the key with a sequence number
  OUTREC IFOUTLEN=80,
         IFTHEN=(WHEN=GROUP,KEYBEGIN=(01,5),
                 PUSH=(081:SEQ=5)),

** If sequence is even then push the availability to odd number
** record at position 087, so that is easy to calculate delta
         IFTHEN=(WHEN=GROUP,
                BEGIN=(085,1,BI,EQ,B'.......1'),
                 PUSH=(087:018,03)),

** If sequence is even then push the availability to odd number
** record at position 091, so that is easy to calculate delta
         IFTHEN=(WHEN=GROUP,
                BEGIN=(085,1,BI,EQ,B'.......0'),
                 PUSH=(091:018,03)),

** once again check the sequence for odd and calculate the
** delta of availability at position 22
         IFTHEN=(WHEN=(085,1,BI,EQ,B'.......1'),
              OVERLAY=(22:091,03,UFF,SUB,
                          018,03,UFF,M10,LENGTH=3),
                          HIT=NEXT),

** For the first record the delta is not applicable, so hardcode
** the value of N/A as delta for it
         IFTHEN=(WHEN=(081,5,ZD,EQ,1),
              OVERLAY=(22:C'N/A')),

** once again check the sequence for even and calculate the
** delta of availability at position 22
         IFTHEN=(WHEN=(085,1,BI,EQ,B'.......0'),
              OVERLAY=(22:087,03,UFF,SUB,
                          018,03,UFF,M10,LENGTH=3))
/*


The output from this job is

ABCDE,2022/07/02,140 N/A
ABCDE,2022/07/03,120  20
SUGAR,2022/08/01,100 N/A
SUGAR,2022/08/02,97    3
SUGAR,2022/08/03,93    4
XXXXX,2022/08/03,20  N/A
YYYYY,2022/08/01,150 N/A
YYYYY,2022/08/01,140  10

Further if you have any questions, please let me know

Thanks,
Kolusu
DFSORT Development
IBM Corporation


-----Original Message-----
From: IBM Mainframe Discussion List <IBM-MAIN@LISTSERV.UA.EDU> On Behalf Of 
Mario Bezzi
Sent: Tuesday, August 30, 2022 12:32 AM
To: IBM-MAIN@LISTSERV.UA.EDU
Subject: [EXTERNAL] Calculate deltas using DFSORT

Hello list,

say I have a file with goods, dates, and available quantities, not necessarily 
sorted. I want to sort it and calculate daily deltas.

For example using the following input :

Product,Date,Availability
Sugar,2022/08/01,100
Sugar,2022/08/02,97
Sugar,2022/08/03,93

I need to get to this:

Product,Date,Sold
Sugar,2022/08/01,N/A  (No daily delta for the first record)
Sugar,2022/08/02,3    (100 - 97)
Sugar,2022/08/03,4    (97 - 93)

May I use bultin DFSORT functions to calculate deltas from the previous record 
and the current one?

I had a look at the OUTREC statement in the Application Programming Manual, but 
as far as I can see arithmetic expressions can only use input fields from the 
current record or decimal constants.

It believe I can achieve the above result using an E35 exit, just wanted to be 
sure that's the only way.

Thanks!
mario

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions, send email to 
lists...@listserv.ua.edu<mailto:lists...@listserv.ua.edu> with the message: 
INFO IBM-MAIN


----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to