SAP MACROS - Guide
Get Example source ABAP code based on a different SAP table
We recommend that procedures (
Macros are often used as callable units, instead of real procedures. This is rarely a good idea however. Macros do not have a real context, and cannot be executed in steps in ABAP Debugger. This makes it practically impossible to look for errors in programs that use large or complex macros. For these reasons, a macro cannot be viewed as a worthy replacement for a genuine procedure.
In addition, in the past macros were not just used to replace procedures, they were also used to perform recurrent declarations of s tructured data. Today, macros are avoided of course and
Nowadays, expressions can be used instead of macros in many cases. One example is using the value operator
In certain cases, however, the use of macros could be justified, as long as the statement patterns are simple and recurring. Here, a macro can be seen as a design-time generation tool. The following (good) example shows how a macro can be used in this way. In a situation like this, a macro may be preferable to a procedure for the following reasons:
This means that, in certain cases, using macros can improve the correctness and maintainability of source code. Macros that contain non-trivial control structures, however, always present a maintenance problem because they cannot run in steps in ABAP Debugger. For this reason, use macros very sparingly and only if they contain no more than a few lines. Errors in macros are almost impossible to analyze.
As well as existing in the source code of a program, macros can also be saved as cross-program macros in type groups. However, no new macros should be defined in type groups.
The following source code is an example where a macro is an unsuitable replacement for a genuine procedure. In this case, the macro could only be used once and in a single context, since the work area
DATA wa TYPE <(> <)>1.
SELECT SINGLE *
FROM <(> <)>1
WHERE <(> <)>2 = <(> <)>3 AND
<(> <)>4 = <(> <)>5.
The following source code shows an example where using a macro could be a good idea. Here, a few simple statement lists (assignments enclosed in
value TYPE string,
flag TYPE c LENGTH 1,
END OF value_and_flag.
component_up TYPE value_and_flag,
component_down TYPE value_and_flag,
component_top TYPE value_and_flag,
END OF structure.
IF struct-component_<(> <)>1-flag = abap_true.
struct-component_<(> <)>1-value = <(> <)>2.
macro_set_value_if_flag_is_set down 'IJK'.
macro_set_value_if_flag_is_set top 'XYZ'.