Summation in SAP Script

To sum values in sap script, I did the following:

1-      Go to se38

2-      Create a program with type subroutine pool

3-      Add the following function to your program :

form calc_sum TABLES in_tab  STRUCTURE itcsy

out_tab STRUCTURE itcsy.

data: Sum_kwert LIKE komvd-kwert.

Read Data into internal table (in_tab)


CHECK sy-subrc = 0.

(your variable name) = in_tab-value. “Take the value from the internal table into a variable

 “Do your Calculation here , for example summation .

 “Pass the calculated value to the script


CHECK sy-subrc = 0.

out_tab-value = (Your Calculated Value).

MODIFY out_tab INDEX sy-tabix .

endform.                    “calc_sum

Then open your SAP Script , right click on the main window then choose edit text
Then open your SAP Script, right click on the main window then choose edit text and add the following code :

/E                         Sum_Line

/:                            DEFINE &KOMVD_KWERT& = &KOMVD-KWERT&

/:                            DEFINE &SUM_KWERT&

/:                            PERFORM CALC_SUM IN PROGRAM Z_LVSKOMMIL1_PROG

/:                            USING &KOMVD_KWERT&

/:                            CHANGING &SUM_KWERT&

/:                            ENDPERFORM