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);

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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