Tuesday, January 17, 2012

SAS Autocall macro facility for Windows environment

Autocall macro facility for Windows environment
My first autocall macro

Objective : This article aims at a simple and easy to use step by step approach to building a precompiled macro library and use it within your program.

Theory : Macros are always compiled before they are used and by default they are stored in SASMACR catalog in work library , SAS provides a powerful feature to pre-compile and store macros permanently. The name looks mind-boggling but it very simple to do it and this can reduce your code writing time as you can store any macro you create and use it in any of your programs without compiling them again.
Before starting a quick over view of the keywords used:

      1)      MAUTOSOURCE option: This option turns of the ability to use sored autocall macros in SAS. (By  
            default it is turned on, but just to be sure use this option).
      2)      SASAUTOS option : This option given the library path where autocall macros are stored. In short this    
             will specify the location where your compiled macros are stored.

      1)      As a first step you need to create a macro which you need to store in permanent library and want to  
           reuse. For e.g :
%Macro test(var=,dataset=,outdset=);
Data &outdset;
Set &dataset;
If &var le 10 then Name = 'Kid';
Else if 11 le &var le 20 then Name = 'Tennager';
Else if 21 le &var le 40 then Name = 'Adolescent';
Else if &var gt 40 then Name = 'Oldie';

proc print;
      %Mend test;

2)      Store the above macro in a file with exactly same name in your macro library (you can use  any library for e.g c:\myfiles\mymacs).
   A point to note here is you should not use any open code outside you macro in that file,   
             since the macro is compiled only once that code will be executed only the first time.
   3)      Now to use your stored macro when you are writing a program just enable option MAUTOSOURCE and in SASAUTOS option give your library location and your macro can be used directly :

option sasautos = ('C:\myfiles\mymacs' sasautos);


Conclusion : So in three easy steps you mastered an arduous task of creating an autocall macro. This facility is very powerful and rest is left to your imagination how to make use of it.

Will be back with some SAS Magic again. Till then Goodbye..!!

Saurabh Singh Chauhan

Disclaimer :
SAS® and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc.in the USA and other countries. ® Indicates USA registration.
Other brand and product names are registered trademarks or trademarks of their respective companies. 
The contents of this post are the works of the author(s)and do not necessarily represent the opinions,recommendations, or practices of any organization whatsoever.