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: 

macros concept

Former Member
0 Kudos

hi,

can any one help in the macros concept.

how it will be help ful to us

3 REPLIES 3

Former Member
0 Kudos

Hi,

Go through the doc attached.

Syntax Diagram

DEFINE

Syntax

DEFINE macro.

... &1 ... &9 ...

END-OF-DEFINITION.

Effect

The statement DEFINE defines a macro. Naming conventions apply to the name macro and you cannot use ABAP words. Between the statements DEFINE and END-OF-DEFINITION, you can use any number of complete ABAP statements, with the exception of DEFINE, END-OF-DEFINITION, and program-initiating statements. These statements constitute a section of source code that can be inserted into a program under the name macro. The definition of a macro is not restricted to the limits of processing blocks.

The validity of a macro is defined by its position in the source code. It can be inserted at any position after END-OF-DEFINITION. If another macro is defined with the same name, it overwrites the previous macro from its new position.

Within a macro, you can use up to nine placeholders &1 ... &9 instead of ABAP words and operands. These placeholders must be replaced by fixed words when the macro is inserted.

As well as in the source code of a program, you can also store macros in the database table TRMAC, where they can be used by any program. The system first searches in the current program for a macro, and then in the table TRMAC. Do not define your own macros in TRMAC. One example of a macro in TRMAC is break, which sets a breakpoint in the system field sy-uname, depending on the current user name.

In macros, you cannot set any breakpoints. In the ABAP Debugger, the statements of a macro cannot be performed in individual steps.

Macros must not include more than a few lines, and should be used sparingly, since it is very difficult to analyze macro errors. Instead of macros, use internal procedures or include programs.

Inserting Macros

macro [p1 p2 ... ].

If a previously defined macro is listed as the first word in an ABAP statement instead of a valid ABAP keyword, then these statements are inserted in the source code at this position. Appropriate ABAP words or operands p1 p2 ... must be specified for all placeholders of the macro. p1 p2 ... replace the placeholders literally, one after the other.

Note

A macro can insert other macros, but not itself.

Example

In this example, a macro write_frame, which draws a frame around a placeholder &1 in a list, is defined and then implemented.

DATA: x TYPE i, y TYPE i, l TYPE i.

DEFINE write_frame.

x = sy-colno. y = sy-linno.

WRITE: '|' NO-GAP, &1 NO-GAP, '|' NO-GAP.

l = sy-colno - x.

y = y - 1. SKIP TO LINE y. POSITION x.

ULINE AT x(l).

y = y + 2. SKIP TO LINE y. POSITION x.

ULINE AT x(l).

y = y - 1. x = sy-colno. SKIP TO LINE y. POSITION x.

END-OF-DEFINITION.

SKIP.

write_frame 'In a frame!'.

Regards,

Anji

0 Kudos

can u give me another simple ex. of this

Former Member
0 Kudos

hi ramana,

DEFINE macro.

Effect

Defines a program component (macro) under the name macro . It must consist only of ABAP/4 statements and is expanded at compilation time.

A macro should always be concluded with the END-OF-DEFINITION statement.

In the definition, you can use &n to reference positional parameters (n = 0 .. 9). When the macro is called, &n is replaced by the n-th actual parameter.

Example

Define a macro called "++" for use in the program.

DEFINE ++.

ADD 1 TO &1.

END-OF-DEFINITION.

DATA: NUMBER TYPE I VALUE 1.

...

++ NUMBER.

check this blog:

https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/5129 [original link is broken] [original link is broken] [original link is broken] [original link is broken]

regards,

keerthi