BookmarkSubscribeRSS Feed
perk
Calcite | Level 5

hi, I have 10 macros which I created and all the 10 macros are saved and has a path.

example:

%include "S:path\macro1.sas";
%include "S:path\macro2.sas";
%include "S:\path\macro\code\macro3.sas";
%include "S:\path\macro4.sas";
%include "S:\Path\macro5.sas";
%include "S:\pathmacro6.sas";
%include "S:path\macro7.sas";  I want all these to be in one line code or library so that I can run that one line and all these macros get invoked. Can I make a code for this?

4 REPLIES 4
Reeza
Super User

Have you tried a wildcard? Are you missing a \ in some of your paths for real?

 

%include 'S:\path\*.sas';

@perk wrote:

hi, I have 10 macros which I created and all the 10 macros are saved and has a path.

example:

%include "S:path\macro1.sas";
%include "S:path\macro2.sas";
%include "S:\path\macro\code\macro3.sas";
%include "S:\path\macro4.sas";
%include "S:\Path\macro5.sas";
%include "S:\pathmacro6.sas";
%include "S:path\macro7.sas";  I want all these to be in one line code or library so that I can run that one line and all these macros get invoked. Can I make a code for this?


 

Patrick
Opal | Level 21

Here how to add a path to the SAS Autocall facility. It's in a macro so that the new path gets only added once even if you call the macro multiple times.

The command itself - which you also could add to the autoexec or .cfg is: insert= sasautos(<'new path'>);

%macro SASAutos_addPath(path);
  %let path=%nrbquote(&path);
  %local curpath;
  %let curpath=%nrbquote(%sysfunc(getoption(sasautos)));
  %if %sysfunc(find(&curpath, &path)) <=0 %then
    %do;
      options insert=sasautos=(%unquote(%nrbquote(')&path.%nrbquote(')));
    %end;
%mend;
%SASAutos_addPath(~/test);

%helloworld();

%put %nrbquote(%sysfunc(getoption(sasautos)));

 You then can save your .sas files with the macro definitions in this folder. Important is that the .sas file has the same name than the macro definition in it (and under Unix/Linux the file name must all be lowercase).

 

In the example above I've created a file helloworld.sas under path ~/test with below code in it.

%macro HelloWorld();
  %put Hello World;
%mend;

Once you've done that you just can call the macro. SAS will search through the paths defined in SASAUTOS and pick the first matching member. So here running the code returns the following:

Patrick_0-1592012333709.png

 

Please note: The first time you call the macro it will get picked from your disk location (here ~/test), the code gets executed which compiles the macro into work. For any subsequent calls of the macro in the same session SAS will pick the already compiled version in the macro catalogue under WORK.

What this means: If you change the macro code in the .sas file then you need to either create a new SAS session or issue an %include (as in your initial code) so that the macro gets recompiled again for testing of the changes.

 

 

 

ballardw
Super User

You can make another INCLUDE file that has those 10 lines of code and call that.

 

If these macro files contain exactly one macro definition, i.e. %macro somename ();

   to the %mend;

You could make sure that the file name is the same as the macro name, i.e. somename.sas and place those in a location referenced as an AUTOCALL library. Then any session with the correct AUTOCALL session will look there for the macros.

SASKiwi
PROC Star

The best solution is to use an AUTOCALL macro library

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1348 views
  • 0 likes
  • 5 in conversation