Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Macro

Former Member
0 Kudos

Hi Friends

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

Regards

Anbu

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

3 REPLIES 3

Former Member
0 Kudos

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.

former_member156446
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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