Delete Duplicates in ABAP

To delete duplicates in ABAP either from a string or an internal table, we need to use ABAP statement “DELETE ADJACENT DUPLICATES FROM”. In this article, we have taken an example, which implements deletion of duplicate data when two strings are merged. This process also involves internal table.

Introduction

DELETE ADJACENT DUPLICATES FROM <Internal Table> COMPARING <field name>.

Using the ABAP statement mentioned above, we can compare values of a column of any table/ internal table and delete the duplicates. Once the duplicates are deleted, we get unique values from a string/ internal table.

SPLIT <variable> AT ‘,’ INTO TABLE <internal table>.

Using the ABAP statement mentioned above, we can divide a string in to a form of internal table where each data is separated by comma ‘,’.

 

Delete Duplicates in ABAP
Delete Duplicates in ABAP – Image Illustration

ABAP Program to Delete duplicates

DATA : lv_string       TYPE string,
       lv_string2      TYPE string,
       lv_string_final TYPE string.
TYPES: BEGIN OF ty_data,
         auth TYPE c,
       END OF ty_data.
DATA: ls_user  TYPE ty_data,
      ls_user2 TYPE ty_data,
      lt_user2 TYPE TABLE OF ty_data,
      lt_user  TYPE TABLE OF ty_data.
lv_string = 'a,b,c,a'.
lv_string2 = 'd,b,c,a'.
SPLIT lv_string AT ',' INTO TABLE lt_user.
SPLIT lv_string2 AT ',' INTO TABLE lt_user2.
LOOP AT lt_user2 ASSIGNING FIELD-SYMBOL(<fs_field>).
  INSERT  <fs_field> INTO TABLE lt_user.
ENDLOOP.
  SORT lt_user BY auth.
  DELETE ADJACENT DUPLICATES FROM lt_user COMPARING auth.
  IF sy-subrc = 0.
    LOOP AT lt_user ASSIGNING FIELD-SYMBOL(<fs_user_table>).
        IF lv_string_final IS INITIAL.
          lv_string_final = <fs_user_table>-auth.
        ELSE.
          CONCATENATE  lv_string_final <fs_user_table>-auth INTO lv_string_final SEPARATED BY ','.
        ENDIF.
    ENDLOOP.
  ENDIF.
WRITE lv_string_final.​

Explanation

Here, we have written program which tIn the above program, following steps have been implemented:

  1. Initially, we have defined variables: lv_string, lv_string2 and lv_string_final of type string. These variables will be used to feed two input strings and save the merged the string.
  2. Now, we have defined a structure with one field. We have created this so that we can use this field to perform duplicate deletion.
  3. Again, we have defined variables: ls_user, ls_user2, lt_user2 and lt_user. These are the work areas (ls) and internal tables (lt). These will be used to store data line by line from the strings defined in step 01 above.
  4. Now, we will feed some default data in form of strings i.e. lv_string = ‘a,b,c,a’ and lv_string2 = ‘d,b,c,a’. These variables do contain duplicate data.
  5. Here, we converted the strings into internal table.
  6. Then, we deleted duplicates from the internal table created above.
  7. Then, we have again concatenated the new internal table data i.e. unique data into a string divided by comma ‘,’.
  8. In the end, we have printed the string as output.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.