Application Development and Automation 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: 
Read only

macro.

Former Member
0 Likes
583

Hi,

Can any one tell me what is the utilities of macro? can we reduce the time of fetching data from rt by using macro??

4 REPLIES 4
Read only

Former Member
0 Likes
530

HI,

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.

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.

Reward If Helpful

Read only

former_member156446
Active Contributor
0 Likes
530

A series of commands and procedures that are carried out in response to a single command or keystroke, or identified by a single name.

there is nothing to do with data fetching with macros... macros is some thing like a subroutine or a FM

http://help.sap.com/saphelp_nw04s/helpdata/en/64/fe4efc8d9c4b1a83eec2bbb14ea213/content.htm

Read only

Former Member
0 Likes
530

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.

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.

Regards,

Raj.

Read only

Former Member
0 Likes
530

hi arnab ,

If you want to reuse the same set of statements more than once in a program, you can include them in a macro.

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.

  • debugging inside macro not possible .

  • it only replace code at the point of call.

* IT doesn't reduce the time fetching .

regard ,

sandeep