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

Macros?

Former Member
0 Likes
1,132

Hello there,

When exactly to use macros and when not to use??

Thanks in advance,

Shaily

1 ACCEPTED SOLUTION
Read only

Former Member
7 REPLIES 7
Read only

Former Member
0 Likes
1,000

whenever we wanna use same block of code several times we can use macros. suppose if we want to do some function rather than just replacing a particular block then we can use subroutines or function modules.

Read only

Former Member
0 Likes
1,000

Hi ,

See this Example .

<a href="http://www.sap-img.com/ab022.htm"></a>

Regards ,

Senthil

Read only

Former Member
Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,000

Hi,

Apart from their readability and efficiency, macros can be more effective than other modularisation 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.

Check this link for more information.It will help you.

http://www.builderau.com.au/news/soa/Unleash_the_power_of_macros_in_ABAP/0,339028227,320276178,00.ht...

Read only

Former Member
0 Likes
1,000

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.

Check this link for more help <a href="http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db972835c111d1829f0000e829fbfe/frameset.htm">http://help.sap.com/saphelp_erp2005vp/helpdata/en/9f/db972835c111d1829f0000e829fbfe/frameset.htm</a>

Regards,

Raghav

Read only

Former Member
0 Likes
1,000

Here is an example of MAcros, Abap macros conce[t is same as tht of C programming language.

e.g -

Basic form

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.

*****reward if helpful

Read only

Former Member
0 Likes
1,000

Hi Shaily,

Macros enable you to modularize a source text within an ABAP program.

Each ABAP program is divided into processing blocks.

Processing Blocks are modularization units of an ABAP program that cannot be split or nested. Processing blocks are procedures, dialog modules, and event blocks.

Macros can be used to modularize source-text when you cannot use the above three processing blocks. You can also use include programs for the same purpose.

An example could be a case wherein you can have multiple date formats for the date coming from a file in your program. The date could be in 'MMDDYYYY','YYYYMMDD' or any other format.

So for checking the date you can implement a macro in your program where in you can pass the date & date format to the placeholders provided by a macro viz. &1 &2. & check the format.

Refer the coding below:

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

  • Passing Parameters: &1 - Date

  • &2 - Date Format

*

  • Date Format:

  • DDMMYYYY MMDDYYYY YYYYMMDD YYYYDDMM

*

  • SY-SUBRC Return Value:

  • 1 invalid date

  • 0 Valid Date

  • 2 Invalid Format

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

DEFINE valdate.

case &2.

when 'DDMMYYYY'.

val_date &14(4) &12(2) &1+0(2).

when 'MMDDYYYY'.

val_date &14(4) &10(2) &1+2(2).

when 'YYYYMMDD'.

val_date &10(4) &14(2) &1+6(2).

when 'MMYYYYDD'.

val_date &12(4) &10(2) &1+6(2).

when others.

sy-subrc = 2.

endcase.

END-OF-DEFINITION.

You can call the macro as under in your code:

valdate '09229999' 'MMDDYYYY'.

valdate '90212222' 'MMDDYYYY'.

Regards,

Chetan.

PS:Reward points if this helps.