If same statements block is repeated more than once in various places of the program, it causes code redundancy.
One of the repeated statements block can place in the macro and the macro call replaces the statements block where ever it repeatedly existed.
Macro is a statements block created with set of statements that calls multiple times within the same program.
The macro only used in the program where it is defined. The macro should be defined before the macro call coded. All the statements in the macro should code in between DEFINE and END-OF-DEFINITION.
MACROS are not operational statements. MACRO cannot call itself. Macros are nested, another MACRO can be called within a MACRO.
Lets take an example program Y and below diagram explains how the program Y look like before and after macro implementation –
Program Y has the same statements block in three places which is a redundant code. It also increases the complexity of the program.
The below diagram shows the same program after macro implementation –
In the above diagram, macro-1 is the macro definition and the repeated code block is replaced with calling macro-1 in all the places.
Syntax –
Definition –
DEFINE <macro-name>.
.
Statements-block
.
END-OF-DEFINITION.
Calling –
<macro-name> [<param1> <param2>....<param9>].
- Macro-name – Specifies the macro name.
- Param1, param2,… – Specifies the macro parameters. The parameters replace with place holders in the macro definition.
The maximum number of parameters/place holders used in macro are nine. <param1> <param2>….<param9> replaces the comma separated place holders &1, &2, …., &9.
Example –
Below example displays the selected option from two radio buttons without using macros.
Code without Macro –
*&---------------------------------------------------------------------*
*& Report Z_MACRO
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_MACRO.
PARAMETERS: OPTION1 type C RADIOBUTTON group RG,
OPTION2 type C RADIOBUTTON group RG.
START-OF-SELECTION.
IF OPTION1 = 'X'.
WRITE: 'Macro from option: 1'.
SKIP 1.
WRITE: 'Radio button selected: 1'.
ENDIF.
IF OPTION2 = 'X'.
WRITE: 'Macro from option: 2'.
SKIP 1.
WRITE: 'Radio button selected: 2'.
ENDIF.
Code with Macro –
*&---------------------------------------------------------------------*
*& Report Z_MACRO
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_MACRO.
DEFINE disp_macro_info.
WRITE: 'Macro call : &1'.
SKIP 1.
WRITE: 'Radio button selected: &1'.
END-OF-DEFINITION.
PARAMETERS: OPTION1 type C RADIOBUTTON group RG,
OPTION2 type C RADIOBUTTON group RG.
START-OF-SELECTION.
IF OPTION1 = 'X'.
disp_macro_info 1.
ENDIF.
IF OPTION2 = 'X'.
disp_macro_info 2.
ENDIF.
Output –
Select OPTION2 radio button and the output is –
Explaining Example –
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
In code without macro, the below peice of code repeated 2 times with very minimal changes for every radio button selected.
WRITE: 'Macro from option: 1'.
SKIP 1.
WRITE: 'Radio button selected: 1'.
The peice of code can added to macro and macro can be called two times to reduce redundancy. So the macro can be coded like below.
DEFINE disp_macro_info.
WRITE: 'Macro call : &1'.
SKIP 1.
WRITE: 'Radio button selected: &1'.
END-OF-DEFINITION.
The call for the macro with the option number is –
disp_macro_info