BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LineMoon
Lapis Lazuli | Level 10

Hello experts,

Please, I want to ask some questions about the performance of sas macro -programs .

 

The first one: increasing the number of sas macro-programs in sas application

 

What's about the performance of the sas application, if we increase the number of sas maco-programs.

For exemple passing from 40 sas macro-programs to 1000 or more ?

 

 

The scond one about the writing of some sas macro -programs on the performance

implrication of sas macro-programs

 

%marco test;

......

.....

%macro test2;

 

 

%mend test2;

 

 

 

%macro testn;

 

%mend testn;

 

.....

...

%Mend test;

 

Sas macro-program without %mend

 

%macro try;

....

%try;

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Performance generally depends on what the macros are doing rather than the number of them.  How you do things also makes a difference (whether using macro code or not).  If you need to calculate sums for 100 groups use BY group processing to do it one step instead of looping 100 times

 

Having hundreds of macros will be difficult to maintain and use.  Users won't know which one to use. I would tend to having fewer macros if you can.  If the macros are being used frequently then it is worth the effort to make them efficient and bug free.

 

Do not nest macro definitions. There is no reason to do that.  Having the macro definition inside of another macro's defintion does NOT imply that the inner macro is some how related to the outer one.  It will NOT "inherit" any macro variables or other things from them. The scoping of macro variables is based on when the macro RUN, not when they are defined.

 

View solution in original post

9 REPLIES 9
Reeza
Super User

Don't nest macros. 

Patrick
Opal | Level 21

Your questions are too generic to give you a real answer. How your macros perform depends very much on how you code them.

 

On what level do you care about performance impact? Miliseconds or minutes?

 

Macro compilation as such doesn't take much time. I've seen some discussions where searching for not yet compiled macros could take up to a second (as it requires creating directory listing of all paths in the SASAUTO's path - you can pre-compile macros though if this is of concern).

 

I believe what @Reeza meant by "don't nest macros" is "don't nest macro definitions" as this will lead to the inner macro being compiled every single time you call the outer macro.

%macro outer;

    %macro inner;

    %mend;

    %inner;

%mend;

 

Instead write things as below as then the macros get compiled only once no matter how often you call them.

%macro inner;

 %mend;

 

%macro outer;

    %inner;

%mend;

 

And then: To keep code maintainable don't nest too many macro calls (=macro calling macro calling macro). It makes debugging really hard in case something goes wrong.

 

LineMoon
Lapis Lazuli | Level 10

@Patrick thank you again for this details.

You have given this exemple

 

%macro outer;

    %macro inner;

    %mend;

    %inner;

  %mend;

 

But, what about this one without calling %inner ?

%macro outer;

    %macro inner;

    %mend;

%mend;

 

Really, I agree with your explanations and Mister @Tom, Is it possible to see some beugs created by the used of "macro nest" ?

 

Thank you again

Patrick
Opal | Level 21

@LineMoon

But, what about this one without calling %inner ?

%macro outer;

    %macro inner;

    %mend;

%mend;

 

It's just something you shouldn't do at all.

LineMoon
Lapis Lazuli | Level 10

@Patrick

With some programmers, we can see some things like that.

So, is it possible to show "the no performance of the two methods " ?

 

Methode 1

%macro outer;

    %macro inner;

    %mend;

    %inner;

%mend;

 

Methode 2

 

%macro outer;

    %macro inner;

    %mend;

%mend;

 

 

Tom
Super User Tom
Super User

Those two programs are not equivalent.  The second one does NOT run the %INNER macro.

There is only a small performance penalty for re-compiling a macro since it does not involve any actual data manipulation.

The bigger penalty will be paid in maintaining the program source file and in confusion that the nested structure will instill into novice users that might assume there was a valid reason for nesting the macro definitions.

 

Method 1 FIXED

%macro inner;
%mend;

%macro outer;
    %inner;
%mend;

 

Method 2 FIXED

%macro inner;
%mend;

%macro outer;
%mend;

 

Tom
Super User Tom
Super User

Performance generally depends on what the macros are doing rather than the number of them.  How you do things also makes a difference (whether using macro code or not).  If you need to calculate sums for 100 groups use BY group processing to do it one step instead of looping 100 times

 

Having hundreds of macros will be difficult to maintain and use.  Users won't know which one to use. I would tend to having fewer macros if you can.  If the macros are being used frequently then it is worth the effort to make them efficient and bug free.

 

Do not nest macro definitions. There is no reason to do that.  Having the macro definition inside of another macro's defintion does NOT imply that the inner macro is some how related to the outer one.  It will NOT "inherit" any macro variables or other things from them. The scoping of macro variables is based on when the macro RUN, not when they are defined.

 

LineMoon
Lapis Lazuli | Level 10

@Tom : Thank you for answer.

               I agree with this explanation, but as you know coding depends on the programmer with best practics or not..So is it possible to show in sas "no perfermance of nest macro "  with some exemple?

 

What's also about  the using of "%mend" without the name of macro ?

%macro test;

%mend;

 

%macro test;

 

%mend test;

 

 

 

 

 

 

 

 

Tom
Super User Tom
Super User

It is good practice to include the macro name in the %MEND statement because it makes the code easier for humans to read and maintain. But it does not change how SAS compiles the macro.  SAS will even happily compile the macro with only a WARNING if the names do not match.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1283 views
  • 3 likes
  • 4 in conversation