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
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
ADD 1 TO &1.
DO N TIMES.
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:
data: &1 like &2 occurs 0 with header line.
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:
Write : &1 no-zero right-justified color &2.
To use this, call macro like
Write_stat var1 1.
Write_stat var2 2.
(c) Reading/changing variable values
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.
(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:
result = ( &1 &2 &3 ) &4 ( &5 &6 &7 ).
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:
Write : / ‘Macro 1 called’.
Write : / ‘Macro 2 called’.
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
SECONDS = ’10’.
(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:
Export &1 &2 &3 to database indx(DB) id ‘DATA’.
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.