Useful options and tips whiles writing SAS Macros

Cool options and tips recommended to be used with SAS Macros

While writing a SAS macro, it is often not possible to write the code with no bug in one go, unless you are super-duper expert. Using the options elaborated in the articles makes it simpler to debug the code and also the tips would make your code much more efficient.



Do you know why God has given us two eyes ...?
So that we can keep one eye on Editor window and the other on LOG.


Jokes apart, checking LOG is something that often we ignore, but we shouldn't. Even if we are 200% sure about our code, do check LOG.

Below are few options that would make the SAS log much more rich and informative.


Options:

1. MPRINT 

SAS code that is generated by a macro is generally not displayed in the LOG. The MPRINT option enables the displays of SAS statements that are generated by macro execution, with macro variable references resolved. It basically helps us understand how the code is working.

2. MLOGIC

When macros are designed with logical branches based on %IF - %THEN - %ELSE statements, this option traces the macro logic and follows the pattern of execution. It shows in LOG whether a %IF statement has got resolved in TRUE or FALSE.

3. SYMBOLGEN

Using this option, a message is printed in the LOG  "to what value macro variable is resolved". This option is useful for checking if the Macro variable is taking the value as per requirement or not.

Efficiency tips :

Imagine, you are working on a SAS Macro with %Do loop that runs 1000 times. You write code, debug it well, F3 it, and go for a cup of coffee. While relaxing with coffee, you  are sure that you would find the code fully executed once you will be back on your desk.

What the hell is that ... that's your expression once you are back and find that  there is prompt for Log window or Output window full and hence work is not done.


Now, imagine your boss is there to follow up with you ... and you start stammering.

Don't want to be in such a situation ? use following tips.

1. Use Proc printto for saving log in an external text file.

Proc printto  log = "location\log.txt"
     print =  "location\output.txt"; 
Run;

However, try to use noprint options in the procedures, which are bound to give output print in output window such as Proc Reg, proc SQL etc. e.g.

Proc SQL noprint;
select ..................;
Quit;

These tips would not only save you from above mentioned situtaion but also would make the code running faster.

2.  If you don't like to save log and output, but just want to clear the log and output window post every loop execution, use just after defining Do loop:

DM "Log" clear continue;
DM "Output" clear continue;


3.  Disable the HTML output or even ODS listing

It can be done in Tools >> Options >> Preferences window's result tab by un-checking the options of Create Listing and Create HTML. Now output window in not there to bother you, just tackle LOG.

4. With using options OBS = 10; in code and executing the code once, You can check any possible error in your code. Once checked, use options OBS = Max; to bring the settings to default.

Would add to this article further ... so keep checking.

Enjoy reading our other articles and stay tuned with ....

Kindly do provide your feedback in the 'Comments' Section and share as much as possible.

No comments:

Post a Comment

Do provide us your feedback, it would help us serve your better.