MACRO in ABAP – an under-used Modularization Technique

0
1078

Macros are hardly used by the programers and quite often underused. Lets have a closer look at this modularization technique, to have a better idea of it.

1. Getting a feel of macros

The basic syntax of macros is as follows:
DEFINE  macro_name.                           “Macro Definition

……………. Statements

……………. Statements

…………………

END-OF-DEFINITION.                            “Macro Definition

macro_name  par1 par2 …par9.   “Macro call -param separated by spaces

2. The system simply ignores the presence of the macros during the execution of the program—that is, all statements are executed as a single block of code:

write :  int1.
write :  int2.
write :  int3.

3. Comparing macro and Form routine

DEFINE INCREMENT.

END-OF-DEFINITION.

DO N TIMES.

INCREMENT VAR1.

ENDDO.

The runtime was measured using an SAP tool known as the Runtime Analyzer (SE30) for different values of N. To check performance, I copied the same program but replaced the macro call and definition with those of form routines. Table A shows a comparison of the performance of both macros and form routines(all times in microseconds). Macros, in this case, were nine times faster than forms routines.

N  1000  10000  100000
Form  731  6974  71236
Macros  74  708  7134

4. Applying macros to suit business needs

(a) Data declaration
You can use macros to define variables. A simple example is:

DEFINE table.

data: &1 like &2 occurs 0 with header line.

END-OF-DEFINITION.
table itab1 lfa1.

table itab2 pernr.

(b) Complex WRITE statements
Macros allow you to maintain readability while writing complex WRITE statements. You can write macros to replace all variants supported by the WRITE statement. Here’s a simple example:

DEFINE Write_stat.

Write : &1 no-zero right-justified color &2.

END-OF-DEFINITION.
To use this, call macro like
Write_stat var1 1.

Write_stat var2 2.

You can use macros to replace any set of repetitive statements that check or modify values of variables. Typical examples are simple IF checks or CLEAR statements. For simplicity’s sake, I’m showing only how the body of such macros may look:

If not &1 is initial.
Endif.
clear: &1,&2.

(d) Formulas or calculations

You can use macros to define formulas for complex calculations. In this case, the placeholders may be operands (such as +, -), constant values, or local variables. Here’s an example:

DEFINE operation.

result = ( &1 &2 &3 ) &4 ( &5 &6 &7 ).

END-OF-DEFINITION.
Operation 5 – 3 / var2 + 9.
Operation var1 + 7 * 6 + 5.

(e) Nesting macros
The ability of macros to call other macros makes them even more powerful. Consider the following example:

DEFINE macro_1.

Write : / ‘Macro 1 called’.

Macro_2.

END-OF-DEFINITION.
DEFINE macro_2.

Write : / ‘Macro 2 called’.

END-OF-DEFINITION.
Macro_1.             “Macro Call for Macro 1.

(f) Program flexibility
Macros help you to minimize hard coding to make your applications more flexible. For example, you can call function modules dynamically. Consider the following program segment:

define dynamic_function_call .

CALL FUNCTION   &1
EXPORTING
SECONDS              = ’10’.

end-of-definition.
dynamic_function_call ‘ENQUE_SLEEP’.

dynamic_function_call funcname.

(g) Working with heavy data
Many SAP programs use macros to export and import data to and from the database. Consumption of system resources is optimal if macros are employed, particularly when a large amount of data is involved:

DEFINE EXPORT_MACRO.

Export  &1  &2  &3  to database indx(DB) id ‘DATA’.

END-OF-DEFINITION.
EXPORT_MACRO itab1 itab2 itab3.

Determine the best option
Macros are one of the modularization options provided by the ABAP language, and we’ve focused here on situations where macros are most appropriate. However, macros are not always the best choice. It is up to you to decide which of the modularization techniques suits a particular scenario.

SHARE
Previous articleUseful SAP BASIS T-Codes
Next articleABAP Editor: Create custom Pattern