In my organization we have many macros and we store them by category in scripts. So we have let's say 15 scripts each containing about 15 macros.
When running other scripts we include the categorys we need by an %include statement. So one script containing an analysis could contain 3 %include statements since we need macros from 3 different categories.
Now, as far as I have understood this is not the "way to do it" and one should (Best practice?) use Autocall libraries.
So, I would like to do it and if anyone could give the steps one normally undertakes that would help alot (when reading about it I find it a bit confusing).
- For instance, the first step I have to do, I believe is to split the scripts containing the macros into seperate files and put the macros for each category in a seperate folder?
If I for instance would like to include macros from a folder called category 1 in a script, what code should I write at the top of the script to include those macros?
The simplest would be:
options
MRECALL
MAUTOSOURCE
SASAUTOS = (sasautos '/your/location/of/macro/files');
Links to the doc:
Saving Macros in an Autocall Library:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n1o5fkxq0gqdpcn1xs3ksdks69tf.htm
MAUTOSOURCE Macro System Option:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n1fbcsjdhakzywn1qg28id3gl0ph.htm
SASAUTOS= Macro System Option:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p12b2qq72dkxpsn1e19y57emerr6.htm
MRECALL Macro System Option:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0cfvf12looza7n10yaa4foizemd.htm
I would also checked out the APPEND= system option:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lesysoptsref/p1q9ay0ai0h2ein1n68qxomnm3q7.htm
As an alternative approach I would also recommend you to check out the idea of SAS Packages. The details are here:
https://github.com/yabwon/SAS_PACKAGES (links also in my signature's footer)
All the best
Bart
I think you can use SASAUTOS to make automatic calls.
One more thing. I would also suggest using stored macros.
You will have to rewrite the macro code a bit, though.
You can consolidate your macros into one catalog for each category, or you can combine all of them into one macro catalog if they don't have the same macro name.
LIBNAME mylib 'C:\temp';
OPTIONS MSTORED SASMSTORE=mylib;
%MACRO msample /STORE;/* Macro stored in mylib.SASMACR */
/* Macro code */
%MEND msample;
when next SAS session.
LIBNAME mylib 'C:\temp';
OPTIONS MSTORED SASMSTORE=mylib;
%msample;/* not %include need! */
I would definitely regard using AUTOCALL macros as best practice. One big benefit is sharing their use across all of your SAS users by including the setup in your SAS server AUTOEXEC.SAS program.
Just be careful if you use compiled SAS macros though. If you lose the macro source code you can't restore it from the compiled macros. For that reason alone I tend to steer clear of using them.
In our organisation, we keep all our macro definitions in a GIT repository, and compile them into each Job or Service or Test before deploying to the target SAS server.
That way every SAS program is fully self contained, with all dependencies, and there is no need to fetch code from the server filesystem. The workflow also plays nicely with GIT for operations such as branching and merging, without impacting other developers on the SAS box.
The framework is open source (we built it) and there are multiple guides available here: https://sasjs.io/resources/
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.