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

Macro

Hi Friends

can u explain what is the use of macro? also i need a sample program for Macro.

Regards

Anbu

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

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

    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.

    Few Uses and advantages of Macro are

    Instead of writing the same data declaration statement each time a new variable is declared, you can simply reuse the code via macros.

    You can use macros to define variables. A simple example is:

    DEFINE table

    data: &1 like &2 occurs 0 with header line.

    END-OF-DEFINITION.

    table itab1 lfa1.

    table itab2 pernr.

    Instead of writing the same data declaration statement each time a new variable is declared, you can simply reuse the code via macros.

    Complex WRITE statements

    Programs often get unnecessarily verbose and difficult to understand because they contain lengthy and redundant 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:

    DEFINE Write_stat

    Write : &1 no-zero right-justified color &2.

    END-OF-DEFINITION.

    The example declares a macro write_stat. The macro prints—after truncating their zeros—variables in a right-aligned format in the color specified by number &2. Instead of writing similar WRITE statements repeatedly, you can simply call the macro and pass the desired output specifications as parameters, like this:

    Write_stat var1 1.

    Write_stat var2 2.

    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.

    Endif.

    clear: &1,

    &2.

    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:

    DEFINE operation.

    result = ( &1 &2 &3 ) &4 ( &5 &6 &7 ).

    END-OF-DEFINITION.

    Operation 5 – 3 / var2 + 9.

    Operation var1 + 7 * 6 + 5.

    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:

    DEFINE EXPORT_MACRO.

    Export &1 &2 &3 to database indx(DB) id 'DATA'.

    END-OF-DEFINITION.

    EXPORT_MACRO itab1 itab2 itab3.

    In this example, the data of three internal tables that may each consist of a large number of records is exported to the table INDX. Had we used form routines in this case, you can't imagine the resources (CPU time and memory) that would be consumed while passing tables as parameters.

    Hope this Helps

    VB

    Add a comment
    10|10000 characters needed characters exceeded

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

    Hi anbulakshmi arul,

    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 <macro>.

    <statements>

    END-OF-DEFINITION.

    Example:

    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.

    See this link.

    http://help.sap.com/saphelp_nw04/helpdata/en/9f/db972835c111d1829f0000e829fbfe/frameset.htm

    Plzz reward if it is useful,

    Mahi.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 31, 2008 at 03:02 AM

    Hi Lakshmi

    Macro is also like a perform, but macro is local to the specific program..

    check this code:

    *Macro for assigning space rather than 0.00 for numeric fields
    DEFINE concat_nonull.
    
    your code in between "<< what ever u wanted that code to do
    
    END-OF-DEFINITION.
    
    "calling the macro here:
    concat_nonull lf_line lf_buf lf_line c_tab <fs_comp>.
    
    

    concat_nonull is the name of the macro

    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.