- SAP ABAP - Web Dynpro
- SAP ABAP - Business Add-Ins
- SAP ABAP - User Exits
- SAP ABAP - Customer Exits
- SAP ABAP - SAPscripts
- SAP ABAP - Smart Forms
- SAP ABAP - Dialog Programming
- SAP ABAP - Report Programming
- SAP ABAP - Object Events
- SAP ABAP - Interfaces
- SAP ABAP - Encapsulation
- SAP ABAP - Polymorphism
- SAP ABAP - Inheritance
- SAP ABAP - Classes
- SAP ABAP - Objects
- SAP ABAP - Object Orientation
- SAP ABAP - Deleting Internal Tables
- SAP ABAP - Reading Internal Tables
- SAP ABAP - Copying Internal Tables
- ABAP - Populating Internal Tables
- SAP ABAP - Creating Internal Tables
- SAP ABAP - Internal Tables
- SAP ABAP - Native SQL Overview
- SAP ABAP - Open SQL Overview
- SAP ABAP - Include Programs
- SAP ABAP - Function Modules
- SAP ABAP - Macros
- SAP ABAP - Subroutines
- SAP ABAP - Modularization
- SAP ABAP - Lock Objects
- SAP ABAP - Search Help
- SAP ABAP - Views
- SAP ABAP - Structures
- SAP ABAP - Tables
- SAP ABAP - Data Elements
- SAP ABAP - Domains
- SAP ABAP - Dictionary
- SAP ABAP - Exception Handling
- SAP ABAP - Formatting Data
- SAP ABAP - Date & Time
- SAP ABAP - Strings
- SAP ABAP - Decisions
- SAP ABAP - Loop Control
- SAP ABAP - Operators
- SAP ABAP - Constants & Literals
- SAP ABAP - Variables
- SAP ABAP - Data Types
- SAP ABAP - Basic Syntax
- SAP ABAP - Screen Navigation
- SAP ABAP - Environment
- SAP ABAP - Overview
- SAP ABAP - Home
SAP ABAP Useful Resources
- SAP ABAP - Discussion
- SAP ABAP - Useful Resources
- SAP ABAP - Quick Guide
- SAP ABAP - Questions Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SAP ABAP - Macros
If we want to reuse the same set of statements more than once in a program, we need to include them in a macro. For example, a macro can be useful for long calculations or for writing complex WRITE statements. We can only use a macro within a program in which it is defined. Macro definition should occur before the macro is used in the program.
Macros are designed based on placeholders. Placeholder works pke pointers in C language. You can define a macro within the DEFINE...END-OF-DEFINITION statement.
Following is the basic syntax of a macro definition −
DEFINE <macro_name>. <statements> END-OF-DEFINITION. ...... <macro_name> [<param1> <param2>....].
It is necessary to define a macro first before invoking it. The <param1>…. replaces the placeholders &1...in the ABAP statements contained in the macro definition.
The maximum number of placeholders in a macro definition is nine. That is, when a program is executed, the SAP system replaces the macro by appropriate statements and the placeholders &1, &2,….&9 are replaced by the parameters param1, param2,....param9. We may invoke a macro within another macro, but not the same macro.
Example
Go to transaction SE38. Create a new program ZMACRO_TEST along with the description in the short text field, and also with appropriate attributes such as Type and Status as shown in the following screenshot −
Following is the code −
REPORT ZMACRO_TEST. DEFINE mac_test. WRITE: This is Macro &1 . END-OF-DEFINITION. PARAMETERS: s1 type C as checkbox. PARAMETERS: s2 type C as checkbox. PARAMETERS: s3 type C as checkbox default X . START-OF-SELECTION. IF s1 = X . mac_test 1. ENDIF. IF s2 = X . mac_test 2. ENDIF. IF s3 = X . mac_test 3. ENDIF.
We have 3 checkboxes. While executing the program, let’s select the S2 checkbox.
The above code produces the following output −
A Macro Program This is Macro 2
If all checkboxes are selected, the code produces the following output −
A Macro Program This is Macro 1 This is Macro 2 This is Macro 3Advertisements