Select language:

Macros 

If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.

The following statement block defines a macro macro:

DEFINE makro.

  statements

END-OF-DEFINITION.

You must specify complete statements between DEFINE and END‑OF‑DEFINITION. These statements can contain up to nine placeholders &1, &2,...., &9). You must define the macro before the point in the program at which you want to use it.

Macros do not belong to the definition part of the program. This means that the DEFINE statement block is not interpreted before the processing blocks in the program. At the same time, however, macros are not operational statements that are executed within a processing block at runtime. When the program is generated, macro definitions are not taken into account at the point at which they are defined. For this reason, they do not appear in the overview of the structure of processing logic.

A macro definition inserts a form of shortcut at any point in a program and can be used at any subsequent point in the program. As the programmer, you must ensure that the macro definition occurs in the program before the macro itself is used. Particular care is required if you use both macros and include programs, since not all include programs are included in the syntax check (exception: TOP include).

To use a macro, use the following form:

makro [p1 p2... p9].

When the program is generated, the system replaces the macro by the appropriate statements and the placeholders &1, &2, …, &9 by the parameter p1, p2, …, p9. You can use macros within macros. However, a macro cannot call itself.

Example

REPORT demo_mod_tech_macros.

DATA: result TYPE i,
 n1  TYPE i VALUE 5,
 n2  TYPE i VALUE 6.

DEFINE operation.
 result = &1 &2 &3.
 output  &1 &2 &3 result.

END-OF-DEFINITION.

DEFINE output.
 write: / 'The result of &1 &2 &3 is', &4.
END-OF-DEFINITION.

operation 4 + 3.
operation 2 ** 7.
operation n2 - n1.

This produces the following output:

The result of 4 + 3 is 7

The result of 2 ** 7 is 128

The result of N2 - N1 is 1

In this example, the two macros operation and output are defined. output is nested in operation. operation is called three times with different parameters. Note how the placeholders &1, &2,... are replaced in the macros.

Example

The following example shows that a macro definition only works in the program lines following its definition. Do not copy it!

Let us have a look at a program with a subroutine test:

PROGRAM macro_test.

...

FORM test.
 WRITE '...'.
ENDFORM.

We can rewrite the program by introducing a macro macro:

PROGRAM macro_test.

...

FORM test.

DEFINE macro.
 WRITE '...'.
 ENDFORM.
END-OF-DEFINITION.

MACRO.

Inserting the macro changes nothing in the generated form of the program. Processing blocks - here a subroutine - are always indivisible. We could also write the program as follows:

PROGRAM macro_test.

...

DEFINE macro.
 WRITE '...'.
 ENDFORM.
END-OF-DEFINITION.

...

FORM test.

MACRO.

The most essential feature of a macro definition is that it should occur before the macro is used.