ABAP - Keyword Documentation →  ABAP - Reference →  Declarations →  Local Declarations in Expressions → 

let_exp - LET ... IN

Syntax

... LET {var1 = rhs1}|{<fs1> = wrexpr1}
        {var2 = rhs2}|{<fs2> = wrexpr2} ... IN ...


Alternatives:

1. ... var = rhs ...

2. ... <fs> = wrexp ...

Effect

A LET expression defines variables var1, var2, ... or field symbols <fs1>, <fs2>, ... as local helper fields in an expression and assigns values to them. When declared, the helper fields can be used in the operand positions of the expression. There is no way of accessing a helper field statically outside its expression. The documentation of an expression specifies whether it contains a LET expression and in which positions. Any LET expressions in an expression or subexpression are evaluated first.

A helper field specified in a LET expression is valid in the context in which the LET expression is specified. This can be a full expression or just part of an expression. All helper fields of a full expression are in the same namespace. A previously specified helper field can be specified in a further LET expression of the same expression. Furthermore, the helper fields are in the same namespace as the data objects or field symbols of the current procedure or program. Helper fields cannot be defined in a LET expression if a data object or field symbol with the same name already exists in the procedure or program of the expression. Conversely, no data objects or field symbols with names given to helper fields can be declared after an expression with a LET expression.

When reusing helper fields in different expressions, the following applies:

Notes

Executable Example

LET expression .

Alternative 1

... var = rhs ...


Effect

Defines a local helper variable var as a helper field in a LET expression. The value of the right side, rhs, is assigned to the helper variable as an initial value.

For the right side, rhs, the same can be specified as in a regular assignment using the assignment operator =. The data type of the helper variable is determined from the right side, rhs, in exactly the same way as when an inline declaration DATA(var) is specified on the left side of an assignment operator using the assignment operator =. It must be possible to determine the data type in full from the right side to avoid syntax errors.

Notes

Example

Defines three local helper variables, x, y, and z, in a constructor expression to construct the values of a structure. The values of the helper variables are used for the structure components.

TYPES:
   BEGIN OF struc,
     col1 TYPE i,
     col2 TYPE i,
   END OF struc.

DATA(rnd) = cl_abap_random_int=>create(
  seed = CONV i( sy-uzeit ) min = 1 max = 10 ).

DO 5 TIMES.
  DATA(struc) = VALUE struc(
    LET x = rnd->get_next( )
        y = x * x
        z = sy-index * 1000 IN col1 = x + z
                               col2 = y + z ).
  cl_demo_output=>write( struc ).
ENDDO.
cl_demo_output=>display( ).

Alternative 2

... <fs> = wrexp ...


Effect

Defines a local field symbol <fs> as a helper field in a LET expression. Here, the result of the writable expression wrexp is assigned to the field symbol. The same applies here as in assignments of writable expressions using ASSIGN, which means that only the expressions specified there can be used. The typing of the field symbol is determined by the type of wrexp and is performed in the same way as in inline declarations of a field symbol using the statement FIELD-SYMBOL.

Example

Defines a field symbol and a variable as helper fields in a LET expression in a conversion expression. The rows of an internal table are assigned to the field symbol.

TYPES:
   BEGIN OF date,
     year  TYPE c LENGTH 4,
     month TYPE c LENGTH 2,
     day   TYPE c LENGTH 2,
   END OF date,
   dates TYPE TABLE OF date WITH EMPTY KEY.

DATA(dates) = VALUE dates(
  ( year = '2013' month = '07' day = '16' )
  ( year = '2014' month = '08' day = '31' )
  ( year = '2015' month = '09' day = '07' ) ).

DO lines( dates ) TIMES.
  DATA(isodate) = CONV string(
    LET <date>  = dates[ sy-index ]
        sep   =   '-'
     IN  <date>-year && sep && <date>-month && sep && <date>-day  ).
  cl_demo_output=>write( isodate ).
ENDDO.
cl_demo_output=>display( ).



Continue
Example LET Expression