Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Layout →  Modularization Statements →  Source Code Modules →  Macros 

Inserting Macros

Other versions: 7.31 | 7.40 | 7.54

Syntax


macro [p1 p2 ... ].

Effect

If a macro is executed as the first word in an ABAP statement instead of a valid ABAP keyword, its content is included at this position in the source code.

To ensure that the program has correct syntax, the included macro must only contain full ABAP statements (except DEFINE and END-OF-DEFINITION) and introductory program statements. These statements create a source code section that is included in the current source code. The statements of a macro are not bound to the limits of processing blocks here.

Suitable ABAP words or operands p1, p2, ... must be passed to all of the placeholders of the macro. The specified operands p1, p2, replace the placeholders sequentially. The characters are converted to uppercase (except for the content of character literals).

ABAP Compiler searches for a macro specified in an ABAP program as follows:

  • In the preceding source code of the same compilation unit.
  • In the type groups that can be used for the program. Local macros of the program obscure macros of the same name in type groups.
  • In the table TRMAC. Macros in the table TRMAC usually follow different name conventions to those in type groups and therefore nothing should be obscured.
  • A macro can insert other macros but not itself.


    Note

    In global classes, macros are defined in a dedicated include program and can be found in this program.


    Example

    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.

    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.
    

    Executable Example

    Macros