Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SUM Function with a LOOP

Former Member
0 Likes
3,911

hi,

Let's say I have a table (i_tab) consisting of...

A 01 $10

A 01 $15

A 02 $20

B 01 $25

B 02 $30

B 02 $35

And I want to get an internal table (i_tab_sum) consisting of the sums...

A 01 $25

A 02 $20

B 01 $25

B 02 $65

My question is regarding the use of the SUM command in a LOOP AT. I figure the code should look something like this...

LOOP AT i_tab

AT NEW {column B}

SUM

i_tab_sum-col1 = i_tab-col1.

i_tab_sum-col2 = i_tab-col2.

i_tab_sum-col3 = i_tab-col3.

APPEND i_tab_sum.

CLEAR i_tab_sum.

ENDAT.

ENDLOOP.

What I'm missing is what the SUM command is doing with the data. How do I populate my i_tab_sum table with the data I need?

Of course I also needed to switch Col1 and Col2 since Col1 is my primary column to be sum'd.

Thanks in advance.

2 REPLIES 2
Read only

Former Member
0 Likes
1,540

hi,

Try this ......

DATA : BEGIN OF int OCCURS 0,

val TYPE char1,

cnt TYPE char2,

dol TYPE i,

END OF int.

DATA : int_final LIKE int OCCURS 0 WITH HEADER LINE.

DATA : temp_dol TYPE i.

int-val = 'A'.

int-cnt = '01'.

int-dol = 10.

APPEND int.

int-val = 'A'.

int-cnt = '01'.

int-dol = 15.

APPEND int.

int-val = 'A'.

int-cnt = '02'.

int-dol = 20.

APPEND int.

int-val = 'B'.

int-cnt = '01'.

int-dol = 25.

APPEND int.

int-val = 'B'.

int-cnt = '02'.

int-dol = 30.

APPEND int.

int-val = 'B'.

int-cnt = '02'.

int-dol = 35.

APPEND int.

SORT int BY val cnt.

LOOP AT int.

AT NEW val.

int_final-val = int-val.

ENDAT.

temp_dol = temp_dol + int-dol.

At end OF cnt.

int_final-cnt = int-cnt.

int_final-dol = temp_dol.

APPEND int_final.

CLEAR temp_dol.

ENDAT.

ENDLOOP.

.reward points if useful and mark it answered

Read only

Former Member
0 Likes
1,540

Hi,

look at the below code.....your doubt should be cleared...

****************

DATA: BEGIN OF LINE,

COL1 TYPE C,

COL2 TYPE I,

COL3 TYPE I,

END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE

WITH UNIQUE KEY COL1 COL2.

LINE-COL1 = 'A'.

DO 3 TIMES.

LINE-COL2 = SY-INDEX.

LINE-COL3 = SY-INDEX ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

LINE-COL1 = 'B'.

DO 3 TIMES.

LINE-COL2 = 2 * SY-INDEX.

LINE-COL3 = ( 2 * SY-INDEX ) ** 2.

INSERT LINE INTO TABLE ITAB.

ENDDO.

SORT ITAB.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

AT END OF COL1.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

SKIP.

ENDAT.

AT LAST.

SUM.

ULINE.

WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.

ENDAT.

ENDLOOP.

****************

Please rememeber

You can only use SUM statement within a LOOP. If you use SUM in an AT - ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area (see example in ). If you use the SUM statement outside an AT - ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in AT...ENDAT blocks.

If the table contains a nested table, you cannot use the SUM statement.

Cheers,

Chandra Sekhar.