Skip to Content
author's profile photo Former Member
Former Member

Macros

What are macros why they are used and how to declare them and how to use them?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 31, 2008 at 07:43 AM

    HI,

    If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.

    The following statement block defines a macro <macro>:

    DEFINE <macro>.

    <statements>

    END-OF-DEFINITION.

    You must specify complete statements between DEFINE and END-OF-DEFINITION. These statements can contain up to nine placeholders (&1, &2, ..., &9). You must define the macro before the point in the program at which you want to use it.

    Macros do not belong to the definition part of the program. This means that the DEFINE...END-OF-DEFINITION block is not interpreted before the processing blocks in the program. At the same time, however, macros are not operational statements that are executed within a processing block at runtime. When the program is generated, macro definitions are not taken into account at the point at which they are defined. For this reason, they do not appear in the overview of the structure of ABAP programs.

    A macro definition inserts a form of shortcut at any point in a program and can be used at any subsequent point in the program. As the programmer, you must ensure that the macro definition occurs in the program before the macro itself is used. Particular care is required if you use both macros and include programs, since not all include programs are included in the syntax check (exception: TOP include).

    To use a macro, use the following form:

    <macro> [<p1> <p2> ... <p9>].

    When the program is generated, the system replaces <macro> by the defined statements and each placeholder &i by the parameter <p i >. You can use macros within macros. However, a macro cannot call itself.

    DATA: RESULT TYPE I,

    N1 TYPE I VALUE 5,

    N2 TYPE I VALUE 6.

    DEFINE OPERATION.

    RESULT = &1 &2 &3.

    OUTPUT &1 &2 &3 RESULT.

    END-OF-DEFINITION.

    DEFINE OUTPUT.

    WRITE: / 'The result of &1 &2 &3 is', &4.

    END-OF-DEFINITION.

    OPERATION 4 + 3.

    OPERATION 2 ** 7.

    OPERATION N2 - N1.

    The produces the following output:

    The result of 4 + 3 is 7

    The result of 2 ** 7 is 128

    The result of N2 - N1 is 1

    Here, two macros, OPERATION and OUTPUT, are defined. OUTPUT is nested in OPERATION. OPERATION is called three times with different parameters. Note how the placeholders &1, &2, ... are replaced in the macros.

    The following example shows that a macro definition only works in the program lines following its definition. Do not copy it!

    Suppose we have a program with a subroutine TEST:

    PROGRAM MACRO_TEST.

    ...

    FORM TEST.

    WRITE '...'.

    ENDFORM.

    Suppose we then changed the program to the following:

    PROGRAM MACRO_TEST.

    ...

    FORM TEST.

    DEFINE MACRO.

    WRITE '...'.

    ENDFORM.

    END-OF-DEFINITION.

    MACRO.

    Inserting the macro changes nothing in the generated form of the program. Processing blocks - here a subroutine - are always indivisible. We could also write the program as follows:

    PROGRAM MACRO_TEST.

    ...

    DEFINE MACRO.

    WRITE '...'.

    ENDFORM.

    END-OF-DEFINITION.

    ...

    FORM TEST.

    MACRO.

    The most essential feature of a macro definition is that it should occur before the macro is used.

    plzz reward if it is useful...

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 31, 2008 at 07:39 AM

    Hi Amren

    hope it will help you.

    Pls reward if help.

    Macros

    If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.

    The following statement block defines a macro macro:

    DEFINE makro.

    statements

    END-OF-DEFINITION.

    You must specify complete statements between DEFINE and END-OF-DEFINITION. These statements can contain up to nine placeholders &1, &2,...., &9). You must define the macro before the point in the program at which you want to use it.

    Macros do not belong to the definition part of the program. This means that the DEFINE statement block is not interpreted before the processing blocks in the program. At the same time, however, macros are not operational statements that are executed within a processing block at runtime. When the program is generated, macro definitions are not taken into account at the point at which they are defined. For this reason, they do not appear in the overview of the structure of processing logic.

    A macro definition inserts a form of shortcut at any point in a program and can be used at any subsequent point in the program. As the programmer, you must ensure that the macro definition occurs in the program before the macro itself is used. Particular care is required if you use both macros and include programs, since not all include programs are included in the syntax check (exception: TOP include).

    To use a macro, use the following form:

    makro [p1 p2... p9].

    When the program is generated, the system replaces the macro by the appropriate statements and the placeholders &1, &2, …, &9 by the parameter p1, p2, …, p9. You can use macros within macros. However, a macro cannot call itself.

    REPORT demo_mod_tech_macros.

    DATA: result TYPE i,

    n1 TYPE i VALUE 5,

    n2 TYPE i VALUE 6.

    DEFINE operation.

    result = &1 &2 &3.

    output &1 &2 &3 result.

    END-OF-DEFINITION.

    DEFINE output.

    write: / 'The result of &1 &2 &3 is', &4.

    END-OF-DEFINITION.

    operation 4 + 3.

    operation 2 ** 7.

    operation n2 - n1.

    This produces the following output:

    The result of 4 + 3 is 7

    The result of 2 ** 7 is 128

    The result of N2 - N1 is 1

    In this example, the two macros operation and output are defined. output is nested in operation. operation is called three times with different parameters. Note how the placeholders &1, &2,... are replaced in the macros.

    The following example shows that a macro definition only works in the program lines following its definition. Do not copy it!

    Let us have a look at a program with a subroutine test:

    PROGRAM macro_test.

    ...

    FORM test.

    WRITE '...'.

    ENDFORM.

    We can rewrite the program by introducing a macro macro:

    PROGRAM macro_test.

    ...

    FORM test.

    DEFINE macro.

    WRITE '...'.

    ENDFORM.

    END-OF-DEFINITION.

    MACRO.

    Inserting the macro changes nothing in the generated form of the program. Processing blocks - here a subroutine - are always indivisible. We could also write the program as follows:

    PROGRAM macro_test.

    ...

    DEFINE macro.

    WRITE '...'.

    ENDFORM.

    END-OF-DEFINITION.

    ...

    FORM test.

    MACRO.

    The most essential feature of a macro definition is that it should occur before the macro is used.

    Macro in ABAP

    *

    • Macro in ABAP

    *

    *

    REPORT ZMACRO.

    DATA: RESULT TYPE I,

    N1 TYPE I VALUE 5,

    N2 TYPE I VALUE 6.

    DEFINE OPERATION.

    RESULT = &1 &2 &3.

    OUTPUT &1 &2 &3 RESULT.

    END-OF-DEFINITION.

    DEFINE OUTPUT.

    WRITE: / 'The result of &1 &2 &3 is', &4.

    END-OF-DEFINITION.

    OPERATION 4 + 3.

    OPERATION 2 ** 7.

    OPERATION N2 - N1.

    *-- End of Program

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 31, 2008 at 07:40 AM

    HI,

    macros are just like templates.it is a modularization technique

    dis adv:debugging a macro0 is not possible

    see this example.

    REPORT ZMACRO.

    DATA: RESULT TYPE I,

    N1 TYPE I VALUE 5,

    N2 TYPE I VALUE 6.

    DEFINE OPERATION.

    RESULT = &1 &2 &3.

    OUTPUT &1 &2 &3 RESULT.

    END-OF-DEFINITION.

    DEFINE OUTPUT.

    WRITE: / 'The result of &1 &2 &3 is', &4.

    END-OF-DEFINITION.

    OPERATION 4 + 3.

    OPERATION 2 ** 7.

    OPERATION N2 - N1.

    rgds,

    bharat.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 31, 2008 at 07:45 AM

    This message was moderated.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 31, 2008 at 07:57 AM

    Hi,

    If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.

    Yes, you can get the same results without using them, but will have to explicitly put the code each time you want to perform the operation it does.

    Using Macros is a Programming Utility to avoid repetitive coding in a Program.

    The Macros used in HR reports tied to PNP are stored in the table TRMAC. Go into SE16 and display the Table contents.

    Just refer this link for more details :

    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/4140b590-0201-0010-4b8a-f9367cd9036a

    just avoid to use this Macros utmost.

    If you want to reuse the same set of statements more than once in a program, you can include them in a macro. For example, this can be useful for long calculations or complex WRITE statements. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition.

    The following statement block defines a macro :

    DEFINE .

    END-OF-DEFINITION.

    Macros do not belong to the definition part of the program. This means that the DEFINE...END-OF-DEFINITION block is not interpreted before the processing blocks in the program. At the same time, however, macros are not operational statements that are executed within a processing block at runtime. When the program is generated, macro definitions are not taken into account at the point at which they are defined

    A macro definition inserts a form of shortcut at any point in a program and can be used at any subsequent point in the program. As the programmer, you must ensure that the macro definition occurs in the program before the macro itself is used. Particular care is required if you use both macros and include programs, since not all include programs are included in the syntax check (exception: TOP include).

    To use a macro, use the following form:

    http:// ... .

    When the program is generated, the system replaces by the defined statements and each placeholder &i by the parameter

    . You can use macros within macros. However, a macro cannot call itself.

    DATA: RESULT TYPE I,N1 TYPE I VALUE 5,N2 TYPE I VALUE 6.

    DEFINE OPERATION. RESULT = &1 &2 &3.OUTPUT &1 &2 &3 RESULT.END-OF-DEFINITION.

    DEFINE OUTPUT. WRITE: / 'The result of &1 &2 &3 is', &4.END-OF-DEFINITION.

    OPERATION 4 + 3.OPERATION 2 ** 7.OPERATION N2 - N1.

    The produces the following output:

    The result of 4 + 3 is 7

    The result of 2 ** 7 is 128

    The result of N2 - N1 is 1

    Reward Points if found helpfull..

    Cheers,

    Chandra Sekhar.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.