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
111

Hi,

what is macro in abap? how does it work?

Regards,

Joanna

1 ACCEPTED SOLUTION

Former Member
0 Kudos
75

Hi!

DEFINE macro.

Effect

Defines a section of source code (macro) that you can address using the name macro. Source code saved as a DEFINE macro may only consist of complete ABAP statements

All macro use is expanded fully in translation. Macros are a text substitute for the translation phase - not a modularization technique for runtime use.

You conclude a macro with the END-OF-DEFINITION statement.

When you define a macro, you can use placeholders (&n, where n = 1, 2, ..., 9). When the macro is expanded, &n is replaced with the n-th current parameter.

Example

Suppose you define a macro "INCREMENT", which you then use in your program.

DEFINE INCREMENT.

ADD 1 TO &1.

END-OF-DEFINITION.

DATA: NUMBER TYPE I VALUE 1.

...

INCREMENT NUMBER.

Notes

As a rule, you should use subroutines (FORM, FUNCTION) instead of macros. This is because subroutines - unlike macros - are supported by all of the ABAP Workbench tools (debugging, runtime analysis, runtime error handling, ...).

You cannot define a macro within a macro using the DEFINE statement.

You cannot use an ABAP keyword as a macro name.

The validity of a macro definition is determined by its position in the source code. You can use a given macro in any line of code following its definition. There is no distinction between global and local macros. For example, the fact that a macro is defined within a subroutine has no effect on its validity.

If you redefine a macro, that is, assign a new meaning to an existing name, the new meaning takes effect from the position in the program where the macro was redefined.

Regards

Tamá

4 REPLIES 4

Former Member
0 Kudos
76

Hi!

DEFINE macro.

Effect

Defines a section of source code (macro) that you can address using the name macro. Source code saved as a DEFINE macro may only consist of complete ABAP statements

All macro use is expanded fully in translation. Macros are a text substitute for the translation phase - not a modularization technique for runtime use.

You conclude a macro with the END-OF-DEFINITION statement.

When you define a macro, you can use placeholders (&n, where n = 1, 2, ..., 9). When the macro is expanded, &n is replaced with the n-th current parameter.

Example

Suppose you define a macro "INCREMENT", which you then use in your program.

DEFINE INCREMENT.

ADD 1 TO &1.

END-OF-DEFINITION.

DATA: NUMBER TYPE I VALUE 1.

...

INCREMENT NUMBER.

Notes

As a rule, you should use subroutines (FORM, FUNCTION) instead of macros. This is because subroutines - unlike macros - are supported by all of the ABAP Workbench tools (debugging, runtime analysis, runtime error handling, ...).

You cannot define a macro within a macro using the DEFINE statement.

You cannot use an ABAP keyword as a macro name.

The validity of a macro definition is determined by its position in the source code. You can use a given macro in any line of code following its definition. There is no distinction between global and local macros. For example, the fact that a macro is defined within a subroutine has no effect on its validity.

If you redefine a macro, that is, assign a new meaning to an existing name, the new meaning takes effect from the position in the program where the macro was redefined.

Regards

Tamá

0 Kudos
75

Hi,

MACRO is to group some peice of code, then you can put the name of the macro any where in the program to replace that with the code that you put in in the macro.

But this concept is now replaced by INCLUDE programs which also serve the same purpose and which can be used acrosss programs and packages. MACRO's cannot be used across programs.

Regards,

Sesh

0 Kudos
75

Thaks

0 Kudos
75

Oh dear -- I don't think you've understood the concept.

Here''s a simple piece of code.

DEFINE col_name.

read table it_fldcat into wa_it_fldcat index &1.

wa_it_fldcat-coltext = &2.

modify it_fldcat from wa_it_fldcat index &1.

end-of-definition.

Now in my main program

            • code

ASSIGN wa_elements TO <fs1>.

CREATE OBJECT z_object EXPORTING z_object = z_object.

  • i_parent = grid_container1.

CALL METHOD z_object->build_dynamic_structures

CHANGING it_fldcat = it_fldcat.

  • Dynamic field cat is returned with dynamic table

  • Here before displaying table you can change the field * catalog to adjust your own names

  • The above Method call just uses the

  • names in the table structure.

col_name 1 'Customer'.

col_name 2 'Name'.

col_name 3 'Street'.

col_name 4 'Phone'.

col_name 5 'City'.

col_name 6 'Post Code'.

Without using the macro I'd have to write loads more code such as

read table it_fldcat into wa_it_fldcat index 1.

wa_it_fldcat-coltext = 'Customer

modify it_fldcat from wa_it_fldcat index 1.

read table it_fldcat into wa_it_fldcat index 2.

wa_it_fldcat-coltext = 'Name'

modify it_fldcat from wa_it_fldcat index2 .

and so on.

You can see here that a function call or a subroutine call is OF NO USE IN THIS INSTANCE, nor is this code any use as an INCLUDE since it's only applicable to the case used in this program.

The macro is being used simply as a shorthand way of generating a lot of repetitive code quickly and simply.

With more variables it becomes even more useful -- but the downside is that if you have an error in the macro then it's sometimes very hard to find as you don't step through each line like in normall debugging.

Cheers

jimbo