Hello,
I would like to know, from a Unix terminal window, which command can I use to execute the macro function test?
%macro test;
libname dest1 "/.../dataretention/reports";
data dest1.class;
set sashelp.class;
run;
%mend test;
%macro test1;
libname dest1 "/.../dataretention/reports";
data dest1.class;
set sashelp.class;
run;
%mend test1;
%test1;
the call for test1 from a Unix terminal will be: sas test1.sas
But it there a way to execute / call test.sas
I don't understand the question. If you have a program test.sas with contents:
%macro test;
libname dest1 "/.../dataretention/reports";
data dest1.class;
set sashelp.class;
run;
%mend test;
You can execute that program with: sas test.sas. And that will execute the program, which will compile the macro named TEST and then the SAS session will exit successfully. But it won't execute the macro, because you never called the macro.
If you have a program test2.sas with contents:
%test()
Then you could run that program with: sas test2.sas. And that will execute the macro TEST. If it can find a macro named TEST in an autocall library it will execute. If it can't find macro named TEST it will generate an error.
You would use UNIX commands to start SAS and provide the file with the macro code and other statements as the program script to run.
If the program only has the code for macro test and no call to the macro then you are wasting computer clock cycles as all that wold happen is the compilation of the macro. The macro define this was does not persist or exist outside of that specific SAS session.
What is the real objective you are attempting to do. There are ways to make macro code available such as AUTOCALL libraries such as SAS uses to store the SAS supplied macros that you can use.
I am exploring the various options. Could you please provide more information on autocall libraries . Please provide an example
An example of an autocall macro:
Run this from your editor:
%colormac;
The log will likely show something like
NOTE: The macro COLORMAC completed compilation without errors. 59 instructions 1648 bytes. NOTE: The macro RGB completed compilation without errors. 19 instructions 440 bytes. NOTE: The macro HLS completed compilation without errors. 59 instructions 1180 bytes.
Then run this code:
proc options option=set; run;
You will likely see something like this in the log:
SET=[FT15F001 = 'FT15F001.DAT'] [SASROOT = "C:\Program Files\SASHome\SASFoundation\9.4"] [SASHOME = "C:\Program Files\SASHome"] [SASAUTOS = ( "!SASROOT\core\sasmacro" "!SASROOT\aacomp\sasmacro" "!SASROOT\accelmva\sasmacro" "!SASROOT\dmscore\sasmacro" "!SASROOT\graph\sasmacro" "!SASROOT\hps\sasmacro" "!SASROOT\mlearning\sasmacro" "!SASROOT\stat\sasmacro" )] [SAMPSIO = ( "!SASROOT\core\sample" "!SASROOT\access\sample" "!SASROOT\accesssample\sample" "!SASROOT\graph\sample" "!SASROOT\hps\sample" "!SASROOT\hpstat\sample" "!SASROOT\stat\sample" )] [SAMPsrc=( "!SASROOT\core\sample" "!SASROOT\access\sample" "!SASROOT\accesssample\sample" "!SASROOT\graph\sample" "!SASROOT\hps\sample" "!SASROOT\hpstat\sample" "!SASROOT\stat\sample" )] [INSTALL = ( )] [MYSASFILES = "?FOLDERID_Documents\My SAS Files\9.4"] [SASCFG = "C:\Program Files\SASHome\SASFoundation\9.4\nls\en"] [SAS_NO_RANDOM_ACCESS = "1"] [SAS_ODSG_CRENDER_PATH = "C:\Program Files\SASHome\SASODSGraphicsCRenderer\9.46"] Defines a SAS environment variable.
The part underlined and highlighted in blue above relates to the SAS supplied macro definitions. !SASROOT is an environment variable where SAS is installed and the rest is the path from there to the folders containing macros.
If you run some code like this with a valid drive/folder replacing my C:\somefolder\subfolder
options insert=(SASAUTOS=("c:\somefolder\subfolder")) ;
and rerun the Proc options above you will see your folder in the list of SASAUTOS.
Any macro placed in that folder where the name of the file and the macro have the same name: i.e. the file named
Mymacro.sas and the first program lines other than comments are "%macro mymacro ("
Whenever you us %mymacro in your code if you have not compiled the macro in the current session then SAS will look in the folders listed in the SASAUTOS for a file named Mymacro.sas and compile it when needed.
I have an options statement in my AUTOEXEC.SAS file that executes whenever SAS starts to set this option (among several other options, library definitions and %include a couple of other program files I want to execute each time SAS starts). There are other ways.
The INITSTMT system option may be helpful here:
sas -initstmt '%include "test.sas"; %test;'
I don't understand the question. If you have a program test.sas with contents:
%macro test;
libname dest1 "/.../dataretention/reports";
data dest1.class;
set sashelp.class;
run;
%mend test;
You can execute that program with: sas test.sas. And that will execute the program, which will compile the macro named TEST and then the SAS session will exit successfully. But it won't execute the macro, because you never called the macro.
If you have a program test2.sas with contents:
%test()
Then you could run that program with: sas test2.sas. And that will execute the macro TEST. If it can find a macro named TEST in an autocall library it will execute. If it can't find macro named TEST it will generate an error.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.