DATA Step, Macro, Functions and more

the performance of some sas macro programs

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

the performance of some sas macro programs

[ Edited ]

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;

 

 

 

 

 

 

 


Accepted Solutions
Solution
‎11-27-2016 06:07 AM
Super User
Super User
Posts: 7,042

Re: the performance of some sas macro programs

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


All Replies
Super User
Posts: 19,789

Re: the performance of some sas macro programs

Don't nest macros. 

Respected Advisor
Posts: 4,173

Re: the performance of some sas macro programs

[ Edited ]

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.

 

Super Contributor
Posts: 371

Re: the performance of some sas macro programs

@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

Respected Advisor
Posts: 4,173

Re: the performance of some sas macro programs

@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.

Super Contributor
Posts: 371

Re: the performance of some sas macro programs

[ Edited ]

@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;

 

 

Super User
Super User
Posts: 7,042

Re: the performance of some sas macro programs

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;

 

Solution
‎11-27-2016 06:07 AM
Super User
Super User
Posts: 7,042

Re: the performance of some sas macro programs

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.

 

Super Contributor
Posts: 371

Re: the performance of some sas macro programs

@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;

 

 

 

 

 

 

 

 

Super User
Super User
Posts: 7,042

Re: the performance of some sas macro programs

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.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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