BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.

View solution in original post

5 REPLIES 5
ballardw
Super User

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.

alepage
Barite | Level 11

I am exploring the various options.  Could you please provide more information on autocall libraries . Please provide an example

ballardw
Super User

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.

 

 

SASKiwi
PROC Star

The INITSTMT system option may be helpful here:

sas -initstmt '%include "test.sas"; %test;'
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1033 views
  • 3 likes
  • 4 in conversation