Skip to Content
Technical Articles

Creating Macros in ABAP programs for re-usability with Exception Handling

Introduction: Macros in ABAP are set of statements that are defined within DEFINE  and END-OF_DEFINITION within the program where we wished to use this macro. We can use the Macros after the Macro definition only in the same program in which it is defined. Macros are used for reusing same calculation in many places of same program instead of writing the same set of statements for same calculation.

useful link:

https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenmacros_guidl.htm

In this article, i want to explain how we can define a Macro for different types of calculations and achieve exception handling within Macro definition.

Lets define a Macro with addition of two variables, subtraction two variables and division of one variable with another one. So that you can use the same macro within the program for different calculations by passing corresponding parameters.

Step 1 : Go to T Code SE38 and create Executable Program with name YTEST.

*&———————————————————————*
*& Report YTEST
*&———————————————————————*
*&
*&———————————————————————*
REPORT ytest.

***data declaration
DATAaddi TYPE i,
subt TYPE i,
divi TYPE f.

***constants declaration
CONSTANTSnum1 TYPE VALUE 100,
num2 TYPE VALUE 200,
num3 type value 0.

***definition of macro
DEFINE tests.
TRY.
&1 &2 + &3 &4 – &5 &6 / &7 ).
CATCH cx_sy_arithmetic_error.
CLEAR &1.
ENDTRY.
END-OF-DEFINITION.

***addition of two values 100 and 200
***      &1      &2      &3     &4  &5  &6  &7
tests addi   num1  num2   0    0    0    .

***subtraction of two values 100 and 200
***     &1     &2    &3   &4    &5    &6   &7
tests subt    0      num1 num2   0    .

***division of two values 100 and 200
***     &1    &2  &3  &4  &5  &6     &7
tests divi    0     0    0    0   num1 num2 .

***division of two values 100 and 0
***     &1    &2  &3  &4  &5   &6    &7
tests divi    0     0    0    0   num1 num3 .

***displaying calcullated values
WRITE‘Addition of’,num1,‘&’,num2,‘:’,addi,
‘Subtraction of’,num1,‘&’ ,num2,‘:’abssubt )“eliminating (-) sign
‘Division of ‘,num1,‘by’,num2,‘:’floordivi ),  “rouding to lower value
‘Division of ‘,num1,‘by’,num3,‘:’floordivi ).  “rouding to lower value

In the above program YTEST, we defined the Macro TESTS with below formula.

&1 &2 + &3 &4 – &5 &6 / &7 ).

In this case when you want o calculate the addition of two parameters in the program, use the Macro by passing required parameters  to &1, &2, &3 and 0’s to other parameters. here &1 refers addi ( result variable ). 

***addition of two values 100 and 200
***      &1      &2      &3     &4  &5  &6  &7
tests addi   num1  num2   0    0    0    .

similarly for other calculations as well.

finally we can handle the exceptions also in the Macro definition by using the try clause .

TRY.
&1 &2 + &3 &4 – &5 &6 / &7 ).
CATCH cx_sy_arithmetic_error.
CLEAR &1.
ENDTRY.

when we pass the Macro parameters with possibility of zero values and there is a chance of raising division by zero error. we can use arithmetic exception class to catch it in try catch block.

***division of two values 100 and 0
***     &1    &2  &3  &4  &5   &6    &7
tests divi    0     0    0    0   num1 num3 .

Above we are using Macro by passing parameters &1 i.e divi (result variable)  , &6 as 100 and &7 as 0 value and finally it will result 0 value to variable divi.

Click on F8 (execute)

Conclusion: From the above program output result we can understand that same macro definition has been used for addition , subtraction and division with exception handling.

9 Comments
You must be Logged on to comment or reply to a post.
  • The documentation you helpfully linked to says near the top:

    Only use macros in exceptional cases.

    We recommend that procedures (methods) or expressions with appropriate operators are used instead of macros.

    …and then goes on to explain all the reasons why their use is usually a bad idea.

    • Hi Mike,

      Thank you for your response, Macros will be preferable only in below few exceptional cases:

      1. when the statements of Macro definition are very clear and no need to debug.
      2. when same set of statements are repeating in program frequently.
      3. when dynamic functionality is required in programming.

       

      Regards,

      Kolatum Suman

       

      • 2. and 3. are good reasons to write methods, which are recommended over macros.

        Also, consider readability:

        tests subt    0      0  num1 num2   0    0 .

        What does this mean? If it is impossible to understand it without comments, then what have we gained by using a macro?

        Another weakness of macros (and FORMs) is positional notation. Consider if someone uses the wrong positions by accident:

        An expected 5 – 3 = 2 calculation suddenly becomes ( 0 + 5 ) + ( 3 – 0 ) = 8, and you can’t debug it.

        A method on the other hand is clear and unambiguous:

        result = calculate( subtract = num1 
                            from     = num2 ).
        • Hi Mike,

          Thank you for your response.

          Using Macros easy to manintain and make sense specially when using recurrent statements in program with clear logic that no need to debug.

          One example is , assignment of complex structure field values assingment.

           

          Thanks,

          Kolatum suman

      • But, as this blog explains, we’ve had it all wrong all these years and the ABAP documentation needs updating. Step outside your comfort zone, macros are the future I tell ya!

        </sarcasm> – just in case anyone gets the wrong idea…

  • Just a quick comment, I copied this code and it would not execute.  It failed with error in arithmetic expression: “)” expected, not “-“.  This was caused because it had trouble &1 &2 + &3 &4 – &5 &6 / &7 ). That I copied directly from this blog.  When I retyped that line everything worked after I fixed the quotes in the output statements.  Not sure how the line was formatted but I could not copy it directly.