SAP ABAP leverages various modularization techniques, each having its own pros and cons. These techniques can provide us with local access from a particular program such as form routines and macros or may also have global access, such as function modules or include programs.There are various modularization techniques available in ABAP and one of them is using MACROS. So broadly speaking macros are small piece of code which can be used several times in the program.
The macros is defined between DEFINE and END-OF-DEFINITION.
For eg:
DEFINE macro_name. “Macro Definition
……………. Statements
……………. Statements
…………………
END-OF-DEFINITION. “Macro Definition
The body of the macro i.e. the statements between the DEFINE and END-OF-DEFINITION gets executed each time the macro is called.
Example1:
*–Macros for Message Logging
DEFINE log_message.
perform f_log_message using &1 &2 &3 &4.
END-OF-DEFINITION.Here the name of the Macro is log_message. Inside this Macro a subroutine f_log_message is called with 4 parameters.
*&———————————————————————*
*& Form F_LOG_MESSAGE
*&———————————————————————*
* Update Log message into the Log table
*———————————————————————-*
* –>P_&1 text
* –>P_&2 text
* –>P_&3 text
* –>P_&4 text
*———————————————————————-*
FORM f_log_message USING p_1 TYPE any ” Record Number
p_2 TYPE c ” Message type
p_3 TYPE t_data ” Employee Data
p_4 TYPE c ” Message Textls_log-recno = p_1. “Record Number
ls_log-msgty = p_2. “Message Type
ls_log-emp_data = p_3. ” Employee Data.
ls_log-msg_txt = p_3-ssn. ” Message Text
*–Inserting record in log table
Append ls_log to gt_log.
ENDFORM. ” F_LOG_MESSAGE
*&———————————————————————*Calling Macros in the program:
Here the macro log_message is called with 4 parameters ls_count, ‘E’, lw_employee-name and Lv_error_txt.
IF sy-subrc <> 0.
Lv_error_txt = ‘Record skipped due to errors’(001).
log_message ls_count ‘E’ lw_employee-name Lv_error_txt.
ENDIF.
Note: Macros are almost ten times faster than forms routines.
