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

ABAP 7.4 syntax for If condition within loop.

0 Likes
11,354

Hello Experts,

Please help me how i can write below type of Loop AT statement as per 7.4 syntax.

Loop at lt_items into data(ls_items) where matnr = hzd456 and erdat le sy-datum .

if ls_items-sequence = 1.

lv_found = 'X'.

exit.

else.

clear : ls_items.

Endif.

endloop.

lv_value = ls_items-value.

1 ACCEPTED SOLUTION
Read only

pfefferf
Active Contributor
8,809

As you have a LE condition no usage of a table expression is possible. So an adapted loop is a good option from my point of view (consider to use e.g. a sorted table when you have a lot of data in the internal table):

LOOP AT lt_items REFERENCE INTO DATA(lr_item)
WHERE matnr = 'hzd456'
  AND erdat le sy-datum
  AND sequence = 1.
  
  lv_value = lr_item->value.
  EXIT.

ENDLOOP.

Of course you could write something like following, but I see not real advantage in that:

DATA(lt_filtered_items) = FILTER #( lt_items 
  WHERE matnr = 'hzd456' AND erdat le sy-datum AND sequence = 1 ).

TRY.
  lv_value = lt_filtered_items[ 1 ]-value.
CATCH cx_sy_itab_line_not_found.
  " exception handling
ENDTRY.
3 REPLIES 3
Read only

pfefferf
Active Contributor
8,810

As you have a LE condition no usage of a table expression is possible. So an adapted loop is a good option from my point of view (consider to use e.g. a sorted table when you have a lot of data in the internal table):

LOOP AT lt_items REFERENCE INTO DATA(lr_item)
WHERE matnr = 'hzd456'
  AND erdat le sy-datum
  AND sequence = 1.
  
  lv_value = lr_item->value.
  EXIT.

ENDLOOP.

Of course you could write something like following, but I see not real advantage in that:

DATA(lt_filtered_items) = FILTER #( lt_items 
  WHERE matnr = 'hzd456' AND erdat le sy-datum AND sequence = 1 ).

TRY.
  lv_value = lt_filtered_items[ 1 ]-value.
CATCH cx_sy_itab_line_not_found.
  " exception handling
ENDTRY.
Read only

8,809

Why isn't it possible with LE / <= ? (there's only an issue with EXIT for which the only workaround I know is to use a THROW exception):

I didn't test it, something like that:

TRY.
  DATA(lv_value) = REDUCE #( 
      INIT v TYPE ... 
           exit TYPE i
      FOR ... WHERE ( ... erdat <= sy-datum ... )
      NEXT v    = ...
           exit = COND #( WHEN v IS NOT INITIAL THEN THROW lx_stop ) )
CATCH lx_stop.
  " Stop
ENDTRY.

of course that's a stupid code 😛

Read only

pfefferf
Active Contributor
0 Likes
8,809

Sure, with a REDUCE expression it is possible - not really readable on a first glimpse, but it works.

I talked about table expressions.