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

wat is macro?

Former Member
0 Likes
1,044

hi friends

what is the definitions of Macro, Events, Sub routines in abap and can you give some examples for the above mentioned

5 REPLIES 5
Read only

Former Member
0 Likes
868

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:

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.

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.

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

REGARDS,

KAVITHA

Edited by: kavitha koppada on Mar 5, 2008 8:26 AM

Read only

Former Member
0 Likes
868

What is Macro?:

Macro is a set of executable statement/s which can be called in a program.

Whenever it is called in the program its replaces the statement which is defined inside the macro.

Syntax :

DEFINE macro_name.

"Set of statements to be executed.

END-OF-DEFINITION.Note:

It is also possible to pass parameters to the MACRO.

Steps Involved

Here am declaring a macro which can accept 4 Parameter during runtime.

Declaring necessary data types.

Calling Macro in the Program.

Code

DEFINE s.

select &1

from &2

into table &3

where &4.

END-OF-DEFINITION.

DATA: t_mara TYPE mara OCCURS 0.

DATA: l_mara(4) VALUE 'MARA',

l_s(6) VALUE 'SELECT',

l_field(5) VALUE '*',

l_table(6) VALUE 'T_MARA',

l_query TYPE string VALUE 'MATNR = ''000000000000000001'''.

FIELD-SYMBOLS: <fs> TYPE table.

ASSIGN t_mara TO <fs>.

s (l_field) (l_mara) <fs> (l_query).

Read only

Former Member
0 Likes
868

CLASSICAL EVENTS

Initalization

At selection-screen output

At seelction-screen on field

At selection-screen on block

At selection-screen

Start-of-selection

End-of-selection

INTERACTIVE EVENTS

At line-selection

At user-command

At pfnn

Top-of-page during line selection

LIST EVENTS

Top-of-page

End-of-page

CONTROL BREAK EVENTS

At first

At last

At new

At end of

On change of

INITIALIZATION

Before the standard selection screen is displayed

AT SELECTION-SCREEN

After user input on a selection screen has been processed, but while the selection screen is still active

START-OF-SELECTION

After the standard selection screen has been processed, before data is read from the logical database

GET node

After the logical database has read a data record from the node node

GET node LATE

After all of the nodes of the logical database have been processed that are hierarchically subordinate to the node node in the structure of the logical database

END-OF-SELECTION

After all data has been read by the logical database

List processor events:

TOP-OF-PAGE

In list processing when a new page starts

END-OF-PAGE

In list processing when a page ends

AT LINE-SELECTION

When the user triggers the predefined function code PICK

AT PFnn

When the user triggers the predefined function code PFnn

AT USER-COMMAND

When the user triggers a function code defined in the program

regards

Ramya

Read only

Former Member
0 Likes
868

Hai mahesh kumar ,

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.

Subroutines

Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module.

FORM <subr> [USING ... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ]

[CHANGING... [VALUE(]<pi>[)] [TYPE <t>|LIKE <f>]... ].

...

ENDFORM.

You call subroutines using the statement

PERFORM... [USING ... <pi>... ]

[CHANGING... <pi>... ].

Subroutines can call other subroutines (nested calls) and may also call themselves (recursive calls). Once a subroutine has finished running, the calling program carries on processing after the PERFORM statement. You can use the USING and CHANGING additions to supply values to the parameter interface of the subroutine.

<subr> is the name of the subroutine. The optional additions USING and CHANGING define the parameter interface. Like any other processing block, subroutines cannot be nested. You should therefore place your subroutine definitions at the end of the program, especially for executable programs (type 1). In this way, you eliminate the risk of accidentally ending an event block in the wrong place by inserting a FORM...ENDFORM block.

Read only

Former Member