BookmarkSubscribeRSS Feed
Rakesh93
Calcite | Level 5

How to store SAS Macros in a permanent Library?

 

eg:

%macro import(set,sh,op);
proc import datafile="D:\SAS Docs\CDM Docs\Protocol.xlsx" dbms=xlsx out=&set replace;
sheet=&sh;
getnames=&op;
run;
proc print data=&set;run;
%mend;

This is a macro but it is getting stored in Work Library. I want to store it in a permanent Library. How?

9 REPLIES 9
Rakesh93
Calcite | Level 5
Can you explain with an example?
Tom
Super User Tom
Super User

It is possible to store COMPILED macros into catalogs and tell SAS to look for them there.

But it is much easier to create and manage if you use the SASAUTOS facility instead.  Use the SASAUTOS option to point to a folder (or series of folders) where SAS can look for an auto-compile the macro from the source.  Place each macro in its own individual file. Name the file with the name of the macro with .sas extension. Make sure to use lowercase letters for the filename.  So your example should be stored in a file named import.sas.

Rakesh93
Calcite | Level 5

Please explain the Syntax of Sasautos in SAS environment.

Tom
Super User Tom
Super User

Make a directory on you filesystem.  For example

/home/me/sas_macros/

Make a macro and store it in a file in that directory.  For example a macro name %MyMac() might be in a file named.

/home/me/sas_macros/mymac.sas

And have content like:

%macro MyMac(dsname);
proc contents data=&dsname;
run;
%mend MyMac;

Then in your main program add this line to tell SAS where to find the macros:

options insert=(sasautos=('/home/me/sas_macro'));

And then after than in your main program you just call the macro.

%mymac(sashelp.class)

If you have called it before it just runs, but if not then SAS will search the folders in the SASAUTOS option tor the file named 'mymac.sas' and include it.   Then, now that the macro has been compiled, it calls it.

 

Rakesh93
Calcite | Level 5
While running this file:

%MyMac(dsname);
proc contents data=&dsname;
run;
%mend MyMac;

It shows no matching macro with mend.

Tom
Super User Tom
Super User
You are missing the keyword MACRO in the first line.
%macro MyMac(dsname);

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 2506 views
  • 2 likes
  • 3 in conversation