Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

 Performing arithmetic on input field Goto page Previous  1, 2
Author Message

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

Posted: Thu Dec 08, 2016 8:58 pm    Post subject:

My input:
 Code: 518791041070069

 Code: OPTION COPY                                        INREC IFTHEN=(WHEN=INIT,                                  BUILD=(1,1,UFF,MUL,+2,EDIT(IT),                           2,1,UFF,MUL,+1,EDIT(T),                             3,1,UFF,MUL,+2,EDIT(IT),                           4,1,UFF,MUL,+1,EDIT(T),                             5,1,UFF,MUL,+2,EDIT(IT),                           6,1,UFF,MUL,+1,EDIT(T),                             7,1,UFF,MUL,+2,EDIT(IT),                           8,1,UFF,MUL,+1,EDIT(T),                             9,1,UFF,MUL,+2,EDIT(IT),                          10,1,UFF,MUL,+1,EDIT(T),                            11,1,UFF,MUL,+2,EDIT(IT),                          12,1,UFF,MUL,+1,EDIT(T),                            13,1,UFF,MUL,+2,EDIT(IT),                          14,1,UFF,MUL,+1,EDIT(T),                            15,1,UFF,MUL,+2,EDIT(IT))),                  IFTHEN=(WHEN=INIT,FINDREP=(INOUT=(C' ',C'')))

Result is:
 Code: 10116718104201400618

number of digits in Result will vary based on input. How do I tell SORT to pick all the digits when adding them?

P.S. - I was amazed to know every IFTHEN picks up amendments done by previous IFTHEN. I didn't know that.

Thanks.

Arun Raj

Moderator

Joined: 17 Oct 2006
Posts: 2361
Location: @my desk

 Posted: Thu Dec 08, 2016 10:07 pm    Post subject: I think you got confused. Forget the FINDREP suggestion. You don't need FINDREP. I will repeat what was suggested already if that makes any clearer. 1 - In the first IFTHEN , multiply only the digits at odd positions(pos-1,3,5,..etc), which in the input are in ZD format (you don't need to multiply others by 1) and OVERLAY at the end of the record (say pos-81,83,85..etc), here either keep it edited as 'TT', or 'IT' so you can read them as ZD or UFF in the second IFTHEN. To avoid confusion I'd recommend using TT, so you see ZEROes instead of blanks. 2 - In the second IFTHEN, ADD all the individual digits from the results above(pos-81,82,83,...etc) and the digits at even positions from your original input which is still available at pos-2,4,6,..etc. And include the MUL by 9 in the same calculation, edit the final result to format it as per your requirement.
Bill Woodger

DFSORT Moderator

Joined: 09 Mar 2011
Posts: 7314

Posted: Thu Dec 08, 2016 10:40 pm    Post subject: Reply to: Performing arithmetic on input field

 zh_lad wrote: I was amazed to know every IFTHEN picks up amendments done by previous IFTHEN

It is not the IFTHEN, it is the BUILD. Each BUILD that is executed creates a "new version" of the current record, from the previous version.

OVERLAY does not create a new version of the current records, but operates on the existing version.

 Code: BUILD=(21,20,1,20) OVERLAY=(21,20,1,20)

Try those, the output will be different, because of the way BUILD and OVERLAY operate.

If you need to "rearrange" data, BUILD is more convenient. If you need to update data where it is, OVERLAY is 1) clearer and 2) performs better.

Other differences are that although you can specify columns in BUILD, you can only define fields in "ascending" order (the columns will just get space-padded if needed), whereas with OVERLAY you can specify the columns in any order, and even do multiple transformations to the same field in one OVERLAY, by specifying the same column multiple times.

Anyway, I'm assuming that was the confusion (how BUILD works), because it is entirely normal to expect something that has been changed to be available to the next line of code.

Note also that with IFTHEN=(WHEN=(logicalexpression), when one is true, no further IFTHEN=(WHEN=(logicalexpression) are processed for that "record", unless you ask for it, by using HIT=NEXT, and that includes if you want to use WHEN=ANY.

So one or more IFTHEN=(WHEN=(logicalexpression) are like a "case/select" statement (EVALUATE in COBOL).

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

 Posted: Fri Dec 09, 2016 8:34 pm    Post subject: Reply to: Performing arithmetic on input field Sorry I couldn't pursue this as I was busy in work. I am on holiday for next week. I will resume when I am back from holiday. Wish you all Merry Xmas and a Happy New Year.
Arun Raj

Moderator

Joined: 17 Oct 2006
Posts: 2361
Location: @my desk

 Posted: Fri Dec 09, 2016 8:36 pm    Post subject: Thanks and Wish you the same!

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

Posted: Thu Jan 05, 2017 8:31 pm    Post subject:

Hi, Happy New Year to you all. I am back to the task.

output is 116, it should have been 468 (52 x 9). I have checked, calculation is right up to 52. Something has gone wrong when MULT with 9. Is the length of 52 causing MULT with work differently?

Thanks.
Arun Raj

Moderator

Joined: 17 Oct 2006
Posts: 2361
Location: @my desk

 Posted: Fri Jan 06, 2017 2:25 am    Post subject: Try enclosing all your ADDs in a separate () parenthesis
sergeyken

Active User

Joined: 29 Apr 2008
Posts: 309
Location: Maryland

 Posted: Fri Jan 06, 2017 3:57 am    Post subject: Definitely: MUL has priority over ADD. With all ADDs enclosed in () the result is correct. Edited to remove spurious content

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

 Posted: Fri Jan 06, 2017 2:13 pm    Post subject: Reply to: Performing arithmetic on input field Thanks Arun.

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

 Posted: Fri Jan 06, 2017 3:29 pm    Post subject: Hi Arun, Thanks for your help so far. One last query. Is there a way I can do this calculation in INREC using multiple BUILD as I want to use the OUTREC to present data differently. Currennt output is 468. I want to stick right most digit (8 in this case) to original input value. In this case, my final output should be: 5187910410700698 This algorithm will be used on an input file to calculate check digit (Luhn algorithm). Thanks.
Bill Woodger

DFSORT Moderator

Joined: 09 Mar 2011
Posts: 7314

 Posted: Fri Jan 06, 2017 4:55 pm    Post subject: Reply to: Performing arithmetic on input field Yes, there is no need for the OUTREC, you could put the second BUILD on another WHEN=INIT on INREC.

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

 Posted: Fri Jan 06, 2017 7:50 pm    Post subject: Thanks Bill. It worked with two BUILDs with WHEN=INIT. Now I am working on OUTREC to stick right most digit to original input. Thanks for your help. Cheers!

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

Posted: Wed Feb 08, 2017 7:31 pm    Post subject: Reply to: Performing arithmetic on input field

I have managed to calculate check digit using:

 Code: Output: 5187910410700698 5186210311030217 5186210311030225 5186210311030233 4940001111111111 4562151111111129 4203761111111138 4571501111111140 4462721111111153 4319401111111165

Is there any better way to write above code? Thanks.

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

 Posted: Wed Feb 08, 2017 8:35 pm    Post subject: I am doing this when I am free. If I wanted to do then I too could have written a cobol program in an hour. SORT gives me flexibility to re use code for different files and layout.
Arun Raj

Moderator

Joined: 17 Oct 2006
Posts: 2361
Location: @my desk

Posted: Wed Feb 08, 2017 10:55 pm    Post subject:

Maybe you can remove the calculations that are not required and also directly overlay the final 'result digit', like this:

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

Posted: Thu Feb 09, 2017 10:49 pm    Post subject:

Thanks Arun. Looks brilliant.

I am getting slightly incorrect result with this code.

output is:
 Code: 5187910410700699

Check digit should be '8'. Addition is resulting 51, should have been 52.
51 x 9 = 459 ==> resulting CH as '9'.
52 x 9 = 468 ==> results CH as '8'.

Thanks.
Arun Raj

Moderator

Joined: 17 Oct 2006
Posts: 2361
Location: @my desk

 Posted: Fri Feb 10, 2017 3:26 am    Post subject: You missed the '26,1,ZD,ADD' and you have an extra line instead - '32,1,ZD,ADD'

Active User

Joined: 06 Jun 2009
Posts: 101
Location: UK

 Posted: Fri Feb 10, 2017 3:28 pm    Post subject: Sorted. Thanks.
 All times are GMT + 6 HoursGoto page Previous  1, 2
 Page 2 of 2

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics OUTREC OVERLAY different to INCREC ge... Joerg.Findeisen SYNCSORT 8 Fri Apr 27, 2018 2:11 am How to include a particular field val... sakrat DFSORT/ICETOOL 8 Fri Apr 06, 2018 8:27 pm Extract the records with a PD field's... sudhakar84 DFSORT/ICETOOL 11 Mon Apr 02, 2018 7:26 pm Searching a table for the field name? socker_dad COBOL Programming 8 Sat Mar 31, 2018 2:57 am How to build one file from two input ... daniele DFSORT/ICETOOL 5 Tue Feb 27, 2018 6:45 pm

 © 2003-2017 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us