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.

Code:
      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';
run;

proc print;
run;
      %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);

%test(var=age,dataset=sashelp.class,outdset=new);

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
(er.chauhansaurabh@gmail.com)

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.

5 comments:

  1. Doug Dame - (Doug_Dame@yahoo.com)February 20, 2012 at 7:52 AM

    Autocalled macros are a great, and under-used, utility.

    But I wouldn't call the example "pre-compiled" ... it's still being saved as regular SAS code in the sasautos library, and is compiled when initially invoked by any given calling program. There is a way to store actual pre-compiled macros, but I've never personally found a compelling reason to do so.

    The one little nuance in using autocalled macros is that, because they are compiled and saved in memory ONCE and then re-used as needed, if you find you need to make changes in the macro code, the changes will not be "active" in your current session. (Unless you do a temp work-around like %include the now-edited source.) So you want to do all your R&D work with the macro as part of your in-stream working program, and only migrate the utility macro to the sasautos library when you're pretty sure it's well tested and stable.

    Another cute trick I use regularly for budget work or other things that have an annual cycle is to stack multiple libraries in the sasautos statement. For example (mainframe code):

    options sasautos
    = (
    "DSS.PGMR.BUD2013.MACLIB",
    "DSS.PGMR.BUD2012.MACLIB",
    "DSS.PGMR.BUD2011.MACLIB",
    "DSS.PGMR.BUD2010.MACLIB",
    "DSS.PGMR.BUD2009.MACLIB",
    "DSS.PGMR.BUD2008.MACLIB",
    "DSS.PGMR.BUD2007.MACLIB",
    "DSS.PGMR.BUDGET.FY2006.MACLIB",
    "DSS.PGMR.BUDGET.FY2005.MACLIB",
    sasautos)
    ;

    Because of the search order, this will always use "the newest version" of any called macro. But I don't have to replicate the entire collection of project macros every year. And all the prior year versions are available for inspection, as needed.

    ReplyDelete
  2. Hey Doug,
    Yes you are right, they really aren't pre-compiled but just a way to call macros from a library. Pre compilation can be done using MSTORED and SASMSTORE options...maybe thats a good idea for a new post as well.. :-) and your cute trick was really good ...maybe i'll also start storing my macros this way. Thanks for your comment...please keep reading and providing your valuable feedback....Thanks

    ReplyDelete
  3. thanx master still am learning from u :)

    ReplyDelete