2013 May 21 2:59 PM
Hi experts,
At the end of an abap program, let’s say I have the following internal table:
Field1 | Field2 | Field3 | Amount |
TEST1 | 22/01/2013 | A001 | -412.80499 |
TEST2 | 15/03/2013 | P001 |
|
TEST3 | 10/02/2012 | A231 |
|
TEST4 | 15/04/2013 | PXXX |
|
I would like to perform some mathematic calculations on my table:
-Calculate Amount where field3(1) = A and Amount where field3(1) = P and then display some messages regarding the result of calculations by comparing the 2 sums.
So in my case, the calculations will be as following:
Field3 | Amount |
A001 | -412.80499 |
A231 | 238.49557 |
Sum 1 | -174,30942 |
P001 | 2089.27519 |
PXXX | 0.68680 |
Sum 2 | 2089,96199 |
Then I have to calculate Sum1 – Sum2 and display some messages based on the result.
How do you advice me to calculate the sums on my internal table?
Thanks for your support.
Amine
2013 May 21 3:27 PM
Hi Amine,
1. Sort the ITAB on Field3 before loop.
2. I guess, the LOOP -ENDLOOP code u wriiten works well, but not give the desired output u mention above.
Field3 | Amount |
A001 | -412.80499 |
A231 | 238.49557 |
Sum 1 | -174,30942 |
P001 | 2089.27519 |
PXXX | 0.68680 |
Sum 2 | 2089,96199 |
the sum amount will be added and displayed against the end of FIELD3, but another row of sum 1 is not added.
3. please let know whether field3 will hav values only starting with A and P?
Regards,
Azhar
Hi experts,
At the end of an abap program, let’s say I have the following internal table:
Field1 | Field2 | Field3 | Amount |
TEST1 | 22/01/2013 | A001 | -412.80499 |
TEST2 | 15/03/2013 | P001 |
|
TEST3 | 10/02/2012 | A231 |
|
TEST4 | 15/04/2013 | PXXX |
|
I would like to perform some mathematic calculations on my table:
-Calculate Amount where field3(1) = A and Amount where field3(1) = P and then display some messages regarding the result of calculations by comparing the 2 sums.
So in my case, the calculations will be as following:
Field3 | Amount |
A001 | -412.80499 |
A231 | 238.49557 |
Sum 1 | -174,30942 |
P001 | 2089.27519 |
PXXX | 0.68680 |
Sum 2 | 2089,96199 |
Then I have to calculate Sum1 – Sum2 and display some messages based on the result.
How do you advice me to calculate the sums on my internal table?
Thanks for your support.
Amine
2013 May 21 3:00 PM
Below 2 proposals, but I don’t know if they are good practices or not in my case…
Loop at ITAB into structure.
at end of structure-field3.
If structure-field3(1) = ‘A’.
sum.
Endif.
endat.
Endloop.
LOOP at itab into structure where -field3(1) = ‘A’.
COLLECT Amount.
ENDLOOP
Thanks.
Amine
2013 May 21 3:09 PM
One thing you can do is create another column(FIELD4) in your table to store an indicator of the matching sets of rows you want to calculate. Then, it's easy to loop through them
Field4 | Field2 | Field3 | Amount |
A | 22/01/2013 | A001 | -412.80499 |
P | 15/03/2013 | P001 |
|
| A | 10/02/2012 | A231 |
|
| P | 15/04/2013 | PXXX |
|
I wouldn't subtotal inside your internal table. Loop through it and subtotal out into another table or onto whatever your output is going to be.
2013 May 21 3:13 PM
Loop at ITAB into structure.
at end of structure-field3.
If structure-field3(1) = ‘A’.
sum.
Endif.
endat.
Endloop.
first option is better one..Collect statement is absolute don't use it.
2013 May 21 3:27 PM
Hi Amine,
1. Sort the ITAB on Field3 before loop.
2. I guess, the LOOP -ENDLOOP code u wriiten works well, but not give the desired output u mention above.
Field3 | Amount |
A001 | -412.80499 |
A231 | 238.49557 |
Sum 1 | -174,30942 |
P001 | 2089.27519 |
PXXX | 0.68680 |
Sum 2 | 2089,96199 |
the sum amount will be added and displayed against the end of FIELD3, but another row of sum 1 is not added.
3. please let know whether field3 will hav values only starting with A and P?
Regards,
Azhar
2013 May 21 4:21 PM
Hi Azhar,
I confim that field3 will have only values starting with either A or P.
Amine
2013 May 21 5:27 PM
Hi Amine,
the below is the simple logic that i built..
loop at itab into wa_itab.
if wa_itab-field3+0(1) = 'A'.
sum_a = sum_a + wa_itab-field4.
elseif wa_itab-field3+0(1) = 'P'.
sum_p = sum_p + wa_itab+field4
endloop.
sum_diff = sum_a - sum_p.
if sum_diff = 0.
write:/ "difference is zero".
else.
write:/ "difference is", sum_diff.
hope this would do the business that u need.
Regards,
Azhar
endif.
2013 May 21 6:23 PM
2013 May 22 2:34 AM
Hi amine,
I would do just like Azhar proposes, but instead of a new loop, can't you add this to the processing where you build your internal table? Avoiding an unnecessary loop is always good
Cheers,
Custodio
@zcust01
2013 May 21 3:33 PM
Hi,
Below logic may be useful in your case..
types: begin of itype,
field1 type string,
field2 type datum,
field3(5) type C, "make sure field3 is of type c for alphabetic sorting.
field4 type wogxxx,
end of itype.
data : itab type table of itype,
amt_A type wogxxx,
amt_P type wogxxx,
tot type wogxxx.
field-symbol: <fs> type itype.
......fill itab table with all data..
.....then follow below logic for summation..
sort itab BY field3 AS TEXT. " this will group data alphabetically
loop at itab assigning <fs>.
if <fs>-field3(1) = 'A'.
amt_A = amt_A + <fs>-field4.
elseif <fs>-field3(1) = 'P'.
amt_P = amt_P + <fs>-field4.
endif.
endloop.
tot = amt_A - amt_P.
Thanks,
Nandi.
2013 May 21 3:37 PM
2013 May 21 3:35 PM
Ok thanks guys.
And how can i do to use the obtained sums A and P in my program:
Field3 | Amount |
A001 | -412.80499 |
A231 | 238.49557 |
Sum 1 | -174,30942 |
P001 | 2089.27519 |
PXXX | 0.68680 |
Sum 2 | 2089,96199 |
Actually, i want to calculate Sum1 and Sum2 internally in my program and then use them to display some messages as following:
SUM = Sum1+Sum2
If SUM = 0.
write:'XXXX'
elseif SUM <> 0.
write :'YYYY'.
Thanks.
Amine
2013 May 21 4:42 PM
Hi,
If you are trying to find the SUM of A's - SUM of P's, then you can use a single variable SUM to maintain.
the code snippet looks like this..
data: sum type wogxxx.
sum = 0.
loop at ITAB assigning <FS>.
if <fs>-field3(1) = 'A'.
sum = sum + <fs>-field4.
elseif <fs>-field3(1) = 'P'.
sum = sum - <fs>-field4.
endif.
if sum = 0.
write: 'sum is zero'.
else.
write: 'sum is non zero'.
endif.
Regards,
Karthik
2013 May 21 5:01 PM
Hi Karthik,
And what about if i want to show the diffeence between the sum of A and the Sum of P.
Let's say:
Sum A = 100
Sum P = 150
Difference = SUM A - SUM P
Difference = 100 - 150
Difference = (-50)
Thanks.
Amine
2013 May 21 5:17 PM
Hi Amine
First option is better i think
Loop at ITAB into structure.
at end of structure-field3.
If structure-field3(1) = ‘A’.
sum.
Endif.
endat.
Endloop.
Regards,
Bastin.G
2013 May 21 5:23 PM
2013 May 21 5:27 PM
2013 May 21 7:50 PM
hi,
This logic may help you.
sum = 0
loop at itab.
if itab-field3+0(1) eq 'A'.
sum = sum + itab-amount.
elseif itab-field3+0(1) eq 'P'.
sum = sum - itab-amount.
endif.
endloop.
if sum eq 0.
write / 'sum is zero'.
else.
write / 'sum is non zero'.
endif.
* if u want to display [ sum A - sum P]
if sum ne 0
write 😕 'Difference of sumA and sumP is :' ,sum.
endif.
2013 May 22 6:28 AM
Hi Amine,
Try this code
DATA: lv_total_a TYPE i, " Holds sum of field3 starting with 'A'
lv_total_p TYPE i, " Holds sum of field3 starting with 'P'
lv_sum TYPE i. " Holds total sum
LOOP AT itab INTO wa.
IF wa-field3(1) = 'A'.
ADD wa-amount TO lv_total_a.
ELSEIF wa-field3(1) = 'P'.
ADD wa-amount TO lv_total_p.
ENDIF.
CLEAR wa.
ENDLOOP.
lv_sum = lv_total_a - lv_total_p.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |