Oh_canada
Oh_canada

Reputation: 95

How to make macros callable without definition

Currently I have a few macros which are very useful but I need to define in each program and then call them. Is there a way I can make a library of macros for myself that I can reference with no need to define them in the same program, maybe needing to be able to call them from a specific library.

Currently:

%macro GenericMacro1(&File);
....
%mend;

%GenericMacro1(File1);
%GenericMacro1(File2);

Want:

libname MyMacros "C://.....";

%MyMacros.GenericMacro1(File1);
%MyMacros.GenericMacro2(File2);

Upvotes: 1

Views: 145

Answers (2)

DomPazz
DomPazz

Reputation: 12465

The closest thing to what you are asking for would be to include all the macros at the start of the program. Save the macros into a folder with no other sas programs. Then

%include "C:\path\to\folder\*.sas";

That will include all SAS files in that folder and will compile the macros at the start.

Richard mentions the AUTOCALL feature. Same idea, create a folder with all your macros. Here you need to name the file and the macro the same name. 1 macro per file. Best practice is to use lowercase only.

In your config file (C:\Program Files\SASHome\SASFoundation\9.4\nls\en\sasv9.cfg on my system), you will see a line that starts -SASAUTOS (line 60 in mine). Append your folder location to that list. Restart SAS and you should be able to call the macros in your session.

Alternatively, you can alter the SASAUTOS values during your autoexec.sas.

OPTIONS SASAUTOS=("C:\path\to\folder",
                  SASAUTOS);

Upvotes: 2

Richard
Richard

Reputation: 27508

Refer to the SASAUTOS documentation

identifies a location that contains library members that contain a SAS macro definition. A location can be a SAS fileref or a host-specific location name enclosed in quotation marks. Each member contains a SAS macro definition.

Related options are MAUTOSOURCE to enable the autocall facility.

You can also use stored precompiled macros in your SAS session use.

options mstored sasmstore=mylib;

The original source code of a compiled macro is not always extractable during a running SAS session, the documentation warns

CAUTION: Save your macro source code.

Upvotes: 4

Related Questions