Skip to Content
Technical Articles
Author's profile photo Ronak Jain

SAP ABAP 7.4: For Loop with Multiple table – Append all the data to final Internal table

Hello,

Many of you already know to work with For loop in SAP ABAP. However, those who are new to the ABAP 7.4 syntax often struggle at some places to achieve the expected result using new syntax. One of the scenario, I am covering below so that new learner can find help with this post.

Scenario:

When we process multiple internal table inside the for loop(in which the where condition of the second internal table will be from the first internal table inside the For Loop). In this case, the result in the final internal table will be only the common data in which they find in both the internal tables.

As you can see below, GT_N has two rows. While GT_C has only one row. When we are processing these two internal tables, only the common data between these two tables is processed completely. If the data with the similar key is not present in both the internal tables then it does not process and skips the complete execution for that particular row and moves to the next row in the first table. Finally the result will be only the common data between the two tables.

Multiple%20For%20Loop%20with%20Multiple%20Tables

Multiple For Loop with Multiple Tables

 

REPORT zrj_test.


TYPES: BEGIN OF ty_n,
         numbers TYPE i,
       END OF ty_n.

TYPES: BEGIN OF ty_c,
         numbers TYPE i,
         text    TYPE char20,
       END OF ty_c.



TYPES: ty_t_n TYPE STANDARD TABLE OF ty_n WITH EMPTY KEY,
       ty_t_c TYPE STANDARD TABLE OF ty_c WITH DEFAULT KEY.

DATA(gt_n) = VALUE ty_t_n( ( numbers = 1 ) ( numbers = 2 ) ).
DATA(gt_c) = VALUE ty_t_c( ( numbers = 2 text = 'Two' ) ).
DATA(gt_o) = VALUE ty_t_c( FOR gs_n IN gt_n
                           FOR gs_c IN gt_c WHERE ( numbers = gs_n-numbers )
                           ( numbers = gs_n-numbers
                             text = gs_c-text ) ).

cl_demo_output=>display( gt_o ).

Result:

Result%20for%20Multiple%20For%20Loop%20with%20Multiple%20Tables

Result for Multiple For Loop with Multiple Tables

Sometimes, we have a case, even if the record is not found in the second table then too we need to append the data of first table to the final internal table with the blank values for which the record is not found in the second tables. To achieve this, we can use LET keyword inside the for loop.

For%20loop%20with%20Let%20Statement%20for%20Multiple%20Tables

For loop with Let Statement for Multiple Tables

REPORT zrj_test.


TYPES: BEGIN OF ty_n,
         numbers TYPE i,
       END OF ty_n.

TYPES: BEGIN OF ty_c,
         numbers TYPE i,
         text    TYPE char20,
       END OF ty_c.



TYPES: ty_t_n TYPE STANDARD TABLE OF ty_n WITH EMPTY KEY,
       ty_t_c TYPE STANDARD TABLE OF ty_c WITH DEFAULT KEY.

DATA(gt_n) = VALUE ty_t_n( ( numbers = 1 ) ( numbers = 2 ) ).
DATA(gt_c) = VALUE ty_t_c( ( numbers = 2 text = 'Two' ) ).
DATA(gt_o) = VALUE ty_t_c( FOR gs_n IN gt_n
                           LET lv_text = VALUE char20( gt_c[ numbers = gs_n-numbers ]-text OPTIONAL )
                           IN text =  lv_text
                           ( numbers = gs_n-numbers ) ).

cl_demo_output=>display( gt_o ).

Result

Result%20-%20For%20loop%20with%20Let%20Statement%20for%20Multiple%20tables

Result – For loop with Let Statement for Multiple tables

 

Using the LET keyword, For loop does not skip the record and process every record completely. Also, every record is appended in the final internal table. For the one which does not have the records in the second internal table is appended in the final table with the blank value for that particular column.

Thanks for taking your time to read this blog. I hope this will help you to achieve these kind of scenario easily. Also, you can check SAP Help site to see more examples with For Loop with different expressions.

Kindly share your comments, if you find this useful. Also, share your ideas in comments if you have used different expression with new ABAP 7.4 syntax to meet your scenario.

Assigned Tags

      9 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Karthikeyan Ravikumar
      Karthikeyan Ravikumar

      Could you explain what does the second "IN" keyword after the LET statement mean?

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Wherever you use LET in a constructor expression, it must be followed by the initialization of auxiliary variables and always end with IN. It's just required by ABAP syntax and has no other meaning than ending the LET block.

      ...
      LET var1 = value1
          var2 = value2
          ...
      IN
      ...

      Note that the example given by Ronak Jain is especially useful to understand why IN is required:

      Without IN (invalid ABAP syntax), it's impossible for the ABAP compiler to know if TEXT is an auxiliary variable which is part of the LET block, or if TEXT is part of TY_T_C line:

      DATA(gt_o) = VALUE ty_t_c( FOR gs_n IN gt_n
                                 LET lv_text = VALUE char20( gt_c[ numbers = gs_n-numbers ]-text OPTIONAL )
                                     text    =  lv_text
                                 ( numbers = gs_n-numbers ) ).

      With IN, you clearly understand that TEXT is not part of the LET block, it's part of the line of TY_T_C:

      DATA(gt_o) = VALUE ty_t_c( FOR gs_n IN gt_n
                                 LET lv_text = VALUE char20( gt_c[ numbers = gs_n-numbers ]-text OPTIONAL )
                                 IN  text    =  lv_text
                                 ( numbers = gs_n-numbers ) ).

      More information in ABAP documentation: let_exp, LET ... IN

      Author's profile photo Karthikeyan Ravikumar
      Karthikeyan Ravikumar

      Thank you so much for this explanation. I could've never wrapped my head around this one myself. The meaning of "IN" keyword in this position is not intuitive.

      However, after reading your explanation, I am able to  think of it as we "letting" the value of the auxiliary variables be "in" the following actual field(s). (like we're some dictator who lets things be what they ought to be 🙂)

      In this case, we're letting the value of lv_text be <result of the expression> IN the actual field "text".

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Saying

      we're letting the value of lv_text be <result of the expression> IN the actual field "text"

      is not how I would describe "IN".

      The Microsoft "M" language which is used in Power Query (one editor in Excel) has the same kind of  LET ... IN ... expression. The reserved word "IN" indicates the data to be returned. What is in the LET block are the auxiliary variables. Example:

      let
          Source = Excel.CurrentWorkbook(){[Name="Tableau1"]}[Content],
          Source2 = Table.TransformColumnTypes(Source,{{"COL1", type text}}),
          Source3 = Table.AddColumn(Source2, "COL2", each Text.BetweenDelimiters([A], "b", "b"), type text),
          Source4 = Table.AddColumn(Source3, "COL3", each Text.BeforeDelimiter([A], "b"), type text),
          Result = Table.AddColumn(Source4, "COL4", each Text.AfterDelimiter([A], "b", 1), type text)
      in
          Result
      Author's profile photo Karthikeyan Ravikumar
      Karthikeyan Ravikumar

      Aah! Got it. So, the result is returned "IN" the result expression that follows the keyword.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Could you please paste the code as text instead of image so that we can try your code? Thank you.

      It's really confusing that you use the "factorization" for just one line (you use factorization when there are several lines):

                        IN text = lv_text
                        ( numbers = gs_n-numbers ) ).

      instead of

                        IN 
                        ( text    = lv_text
                          numbers = gs_n-numbers ) ).

      Also there's absolutely no reason to use LET ... IN in your case, it obfuscates the code, you'd better use directly:

                        ( text    = VALUE #( ... OPTIONAL )
                          numbers = gs_n-numbers ) ).

      An example where factorization is often used is the initialization of Ranges Tables:

      range = VALUE #( sign   = 'I'
                       option = 'EQ'
                       ( low = 'VALUE 1' )
                       ( low = 'VALUE 2' )
                       ( low = 'VALUE 3' ) ).
      Author's profile photo Matthew Billingham
      Matthew Billingham

      Seconded. The button you need in the editor is {;}

      Author's profile photo Ronak Jain
      Ronak Jain
      Blog Post Author

      As requested, I have pasted the code in the blog.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Thanks a lot!