A more efficient method would be to evaluate the length of THETEXT once and assign
that value to another macro variable. Then, use that variable in the %DO %WHILE
statement, as in the following program:
/* MORE EFFICIENT MACRO */
%macro test2(thetext);
%let x=1;
%let length=%length(&thetext);
%do %while (&x > &length);
.
.
.
%end;
%mend test2;
%test(Four Score and Seven Years Ago)
As another example, suppose you want to use the %SUBSTR function to pull the year
out of the value of SYSDATE. Instead of using %SUBSTR repeatedly in your code,
assign the value of the %SUBSTR(&SYSDATE, 6) to a macro variable, and use that
variable whenever you need the year.
Turn Off System Options When Appropriate
While the debugging system options, such as MPRINT and MLOGIC, are very helpful
at times, it is inefficient to run production (debugged) macros with this type of system
option set to on. For production macros, run your job with the following settings:
NOMLOGIC, NOMPRINT, NOMRECALL, and NOSYMBOLGEN.
Even if your job has no errors, if you run it with these options turned on you incur the
overhead that the options require. By turning them off, your program runs more
efficiently.
Note: Another approach to deciding when to use MPRINT versus NOMPRINT is to
match this option's setting with the setting of the SOURCE option. That is, if your
program uses the SOURCE option, it should also use MPRINT. If your program uses
NOSOURCE, then run it with NOMPRINT as well.
Note: If you do not use autocall macros, use the NOMAUTOSOURCE system option. If
you do not use stored compiled macros, use the NOMSTORED system option.
Use the Stored Compiled Macro Facility
The stored compiled macro facility reduces execution time by enabling macros compiled
in a previous SAS job or session to be accessed during subsequent SAS jobs and
sessions. Therefore, these macros do not need to be recompiled. Use the stored compiled
macro facility only for production (debugged) macros. It is not efficient to use this
facility when developing a macro application.
CAUTION:
Save the source code. You cannot re-create the source code for a macro from the
compiled code. You should keep a copy of the source code in a safe place, in case the
compiled code becomes corrupted for some reason. Having a copy of the source is
also necessary if you intend to modify the macro at a later time.
For more information about the stored compiled macro facility, see Chapter 9, “Storing
and Reusing Macros,” on page 115.
Writing Efficient Macros 147