Collect Data in an Internal Table

In ABAP, the statement ‘COLLECT’ is used to add all numeric values of a work area into an internal table.

For example: In the example below, we will explain and demonstrate how to we will collect the data in an internal table.

 

Step 1: Internal table which will be collected:

MATNR WERKS OMENG
MAT1 AB 10
MAT1 AB 5
MAT1 AB 10
MAT2 AB 30
MAT2 CD 20
MAT3 DE 40

 

Step 2: When the above internal table is collected, all numerical field of similar non-numerical field will be added together.

MATNR     WERKS      OMENG
MAT1  AB 10 + 5 + 10
MAT1 AB 5
MAT1 AB 10
MAT2 AB 30 + 20
MAT2 AB 20
MAT3 DE 40

 

Results: The results will be as follows:

MATNR WERKS  OMENG
MAT1 AB 25
MAT2 AB 50
MAT3 DE 40

 

We can use the following code to achieve the above results:

*Types:
TYPES:
BEGIN OF gxs_vbbe ,
matnr TYPE vbbe-matnr,
werks TYPE vbbe-werks,
omeng TYPE vbbe-omeng,
END OF gxs_vbbe.

*Internal tables and structures:
DATA:
lt_vbbe TYPE STANDARD TABLE OF gxs_vbbe,
lt_vbbe_collected TYPE STANDARD TABLE OF gxs_vbbe,
lst_vbbe TYPE gxs_vbbe.

* Dummy retrieval for demo purposes.
SELECT matnr \" Material Number
werks \" Plant
omeng \" Open Qty
FROM vbbe
INTO TABLE lt_vbbe
UP TO 10 ROWS.

* Looping on the internal table to be collected.
LOOP AT lt_vbbe INTO lst_vbbe.

* Collecting all numeric values into the
* internal table \'LT_VBBE_COLLECTED\'.
COLLECT lst_vbbe INTO lt_vbbe_collected.

ENDLOOP.

 

Please note: In the above example, the program will check if MATNR and WERKS are similar, if they are similar, then we will add/collect the numerical fields together.

In case you want to collect only when the MATNR is similar, then clear the value of the field LST_VBBE-WERKS before the collect statement. The loop will look as follows:

* Looping on the internal table to be collected.
LOOP AT lt_vbbe INTO lst_vbbe.

* Clearing the plant(WERKS) as we want only
* to collect for similar material number (MATNR).
CLEAR lst_vbbe-werks.

* Collecting all numeric values into the
* internal table \'LT_VBBE_COLLECTED\'.
COLLECT lst_vbbe INTO lt_vbbe_collected.

ENDLOOP.

 

Another way to collect without clearing the variable is to move the fields we want to collect into a different structure. We will then use the different structure to do the collect. This can be useful in case we want to collect different values in the same loop. The code will look as follows:

*Types:
TYPES:
  BEGIN OF  gxs_vbbe,
    matnr TYPE vbbe-matnr,
    werks TYPE vbbe-werks,
    omeng TYPE vbbe-omeng,
  END OF gxs_vbbe,

  BEGIN OF  gxs_vbbe_col_mat,
    matnr TYPE vbbe-matnr,
    omeng TYPE vbbe-omeng,
  END OF gxs_vbbe_col_mat.

*Internal tables and structures:
DATA:
  lt_vbbe           TYPE STANDARD TABLE OF gxs_vbbe,
  lt_vbbe_collected TYPE STANDARD TABLE OF gxs_vbbe,
  lst_vbbe          TYPE gxs_vbbe,
  lt_vbbe_col_mat   TYPE STANDARD TABLE OF gxs_vbbe_col_mat,
  lst_vbbe_col_mat  TYPE gxs_vbbe_col_mat.


* Dummy retrieval for demo purposes.
  SELECT matnr  \" Material Number
         werks  \" Plant
         omeng  \" Open Qty
    FROM vbbe
    INTO TABLE lt_vbbe
   UP TO 10 ROWS.

* Looping on the internal table to be collected.
  LOOP AT lt_vbbe INTO lst_vbbe.

*   Move MATNR and OMENG to a different structure,
*   we will use the different structure to do the
*   collect.
    MOVE lst_vbbe-matnr TO lst_vbbe_col_mat-matnr.
    MOVE lst_vbbe-omeng TO lst_vbbe_col_mat-omeng.

*   Collecting all numeric values into the
*   internal table \'LT_VBBE_COLLECTED\'.
    COLLECT lst_vbbe_col_mat INTO lt_vbbe_col_mat.

  ENDLOOP.