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

regarding macros

Former Member
0 Likes
1,320

i just want to know about macros.how it works.specially i

want to know about placeholders.

can anybody please give example of more than 1 placeholders used in macros.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,012

Hi

The following statement block defines a macro macro:

DEFINE makro.

statements

END-OF-DEFINITION.

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

Thanks

Vasudha

8 REPLIES 8
Read only

Former Member
0 Likes
1,013

Hi

The following statement block defines a macro macro:

DEFINE makro.

statements

END-OF-DEFINITION.

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

Thanks

Vasudha

Read only

0 Likes
1,012

hi vasuddha,

goodmorning.

i am not able to understand placeholders used here.

plz tell under define operation u have written

output &1 &2 &3 result.

then in second macro u have defined output as a macro.

what is that.

Read only

0 Likes
1,012

Hi,

they are placeholders.at runtime they are replaced by values pass in the implementation.

output macro is being called from operation macro.

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.

Regards

Read only

Former Member
0 Likes
1,012

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.

Sample program:

*************************************

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.

*******************************

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.

Read only

Former Member
0 Likes
1,012

Hi,

Check this.

[help.sap.com/saphelp_nw70/helpdata/en/9f/db972835c111d1829f0000e829fbfe/content.htm - 22k ]

Regards,

Priya.

Read only

Former Member
0 Likes
1,012

Hi,

ABAP, SAP's programming language, is no exception. It leverages various modularization options, each having its own strengths and weaknesses. These options may have local access from a particular program—such as form routines and macros—or they may have global access, such as function modules or include programs.

Although a comprehensive discussion of all the modularization techniques available in ABAP is beyond the scope of this article, we can look at one option—macros—and consider some situations where they work better than other options.

Getting a feel of macros

The basic syntax of macros is as follows:

DEFINE macro_name. "Macro Definition

……………. Statements

……………. Statements

…………………

END-OF-DEFINITION. "Macro Definition

macro_name par1 par2 …par9. "Macro call -parameters separated by spaces

Within the DEFINE... and END-OF-DEFINITION lies the body of the macro—the statements that you wish to be executed each time the macro is called. These statements may be any valid ABAP statements, such as WRITE, CLEAR, FORM calls, or database statements such as SELECT or UPDATE.

To familiarize yourself with the working of macros, it's necessary to take a close look at exactly what happens when an ABAP program containing a macro call is generated. Consider Listing A.

All ABAP programs must be generated before they can be executed. At the time of program generation, the system supplants each macro call, as shown in Listing A, with the statement(s) placed between the macro definition. Furthermore, the parameters passed at the time of macro calling are copied in place of the any placeholders (numbered &1, &2 …&9) found in the body of the macro definition. The system simply ignores the presence of the macros during the execution of the program—that is, all statements are executed as a single block of code:

write : int1.

write : int2.

write : int3.

Other than readability and meaningfulness, macros also offer performance advantages. For testing purposes, I wrote a macro that incremented the value of a variable by 1 and called the macro N times via a DO loop, as shown here:

DEFINE INCREMENT.

ADD 1 TO &1.

END-OF-DEFINITION.

DO N TIMES.

INCREMENT VAR1.

ENDDO.

The runtime was measured using an SAP tool known as the Runtime Analyzer (SE30) for different values of N. To check performance, I copied the same program but replaced the macro call and definition with those of form routines. Table A shows a comparison of the performance of both macros and form routines(all times in microseconds). Macros, in this case, were nine times faster than forms routines.

N 1000 10000 100000

Form 731 6974 71236

Macros 74 708 7134

Applying macros to suit business needs

Apart from their readability and efficiency, macros can be more effective than other modularization techniques because of their versatility in replacing different ABAP statements in solving business problems.

ABAP provides only the syntax for creating macros. It's up to the programmer to devise ways that best suit the requirements. The power of macro programming lies in mastering the ways macros can be used in ABAP programs.

Macros can be used for different purposes within the SAP terrain. I have tried to compile the variety of ways I've seen macros adding elegance to ABAP applications. Here's a closer look.

Data declaration

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.

Cheers,

vasavi.

Read only

Former Member
0 Likes
1,012

hi,

Macros are statements which are converted into code snippets at the time of Program Execution.

These codes are general functions which are used in many programs in HR ABAP

Examples of Macros are:

RP-PROVIDE-FROM-LAST

This macro is used to retrieve the last data record which is valid in the data selection period.

RP-PROVIDE-FROM-FRST

This macro is used to retrieve the first data record which is valid in the data selection period.

Provide and Endprovide are used as Looping Constructs while using LDB.

PROVIDE / ENDPROVIDE

All the data which satisfies the conditions associated with the Provide and Endprovide statements is picked for each Employee between the given Intervals.

refer this example

tables: pernr.

infotypes: 0000,0001.

get pernr.

rp_provide_from_last p0001 space sy-datum sy-datum.

write: / pernr-pernr , p0001-begda, p0001-endda.

regards,

sreelakshmi

Read only

Former Member
0 Likes
1,012

hi,

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.

i am not able to understand placeholders used here.

plz tell under define operation u have written

output &1 &2 &3 result.

then in second macro u have defined output as a macro.

what is that.