BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bbb_NG
Fluorite | Level 6

%let macrodir=\\133.3.3.3\a\a\DataPre\Macro;

%include "&macrodir\allmacros.sas";

I wrote the above ,trying to include all macros under the directory, but failed.

How to modifiy those code?

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Editor's note:  SASAUTOS is probably the best solution here since the macro will only be compiled if invoked.  SASAUTOS does the %INCLUDE for you behind the scene when called.  To add to the soution below I would make one modification to the option, for example:

 

  options sasautos=(sasautos,"&macrodir");

 

By adding SASAUTOS in the search path we include the SAS supplied autocall macros.  Without adding this you will be overwriting what SASAUTOS is pointing to and you will not have access to the SAS supplied autocall macros.

 

It's possible you found a way by now, but consider that it might not be the correct thing to do.  If you want all the macros in the folder available for use, you don't have to %include them.  Another approach:

 

options sasautos=("&macrodir.");

 

You can also specify more than one folder using this method.

 

Good luck.

View solution in original post

17 REPLIES 17
ChrisNZ
Tourmaline | Level 20

Nothing wrong with your code.

Run the command 

filepad "\\133.3.3.3\a\a\DataPre\Macro\allmacros.sas" to check the file is there.


bbb_NG
Fluorite | Level 6

filepad "\\133.3.3.3\a\a\DataPre\Macro\allmacros.sas";

filepad was red

log is somewhat like invalid statement or not using in correct sequence.

(SAS version not an english version)

shivas
Pyrite | Level 9

Hi,

Try this...F$ is the drive...

%let macrodir=\\133.3.3.3\f$\SASCodes;

%include "&macrodir\Import.sas";

Thanks,

Shiva

ChrisNZ
Tourmaline | Level 20

this is a command, not a statement.

run from the command box.

Tom
Super User Tom
Super User

filename mymacs "&macrodir";

%include mymacs('*.sas');

psherman42wallabyway
Calcite | Level 5

Elegant and to the point.  Will make it easy for my purposes.  With larger projects, I like to have several .sas files with specific purposes.  Any idea on whether this would cause additional .sas files in subfolders to be included?

Tom
Super User Tom
Super User

That syntax would no include files from subdirectories.

Astounding
PROC Star

Editor's note:  SASAUTOS is probably the best solution here since the macro will only be compiled if invoked.  SASAUTOS does the %INCLUDE for you behind the scene when called.  To add to the soution below I would make one modification to the option, for example:

 

  options sasautos=(sasautos,"&macrodir");

 

By adding SASAUTOS in the search path we include the SAS supplied autocall macros.  Without adding this you will be overwriting what SASAUTOS is pointing to and you will not have access to the SAS supplied autocall macros.

 

It's possible you found a way by now, but consider that it might not be the correct thing to do.  If you want all the macros in the folder available for use, you don't have to %include them.  Another approach:

 

options sasautos=("&macrodir.");

 

You can also specify more than one folder using this method.

 

Good luck.

RichardinOz
Quartz | Level 8

The problem with the sasautos option is that it must be invoked when SAS is started.  This means that a user who does not include the option when they start SAS must find some other way to compile the macros.  It can be very confusing if you are trying to debug someone else's program that works perfectly for them because their code may not include any reference to where the macros are located. 

So I would only advocate use of sasautos under very tight controls where all users have it activated and the directories are read only (because one user's mod of a macro would require integration testing of all other SAS code which might make use of the macro).

Richard

Astounding
PROC Star

RichardinOz,

I'm not sure which method would be simpler.  I could imagine a user of either method being dumbfounded by being asked where his macro is stored.  In fact, I've even seen one experienced programmer who stored 40 macros in a single file so he could %include just that one file.  If you're going to %include all of your macro definitions, that makes as much sense as any.

Note that sasautos doesn't have to be specified at SAS invocation.  The options statement can appear within a SAS program.

RichardinOz
Quartz | Level 8

Astounding

Aha, that change to sasautos system option snuck up on me.  Makes it more usable.

I still prefer individual %include statements for each macro file, because that states explicitly at th head of a script, where each macro is located.

Richard

Ron_MacroMaven
Lapis Lazuli | Level 10

I have had many discussions with other SAS-L peers over the years about this idea of

%including user-written macros before they are called.

summary:

keep the log short: do not %include

SAS does the %include for you via the option sasautos.

commentary: isn't mprint enough for you to see the code generated?

Ron Fehd  SASautos maven

http://www.sascommunity.org/wiki/SASautos_Companion_Reusing_Macros

Pallav
Calcite | Level 5

May be this can be useful for your task... Its a bit complicated but can be used as a macro application.

Assuming your single folder have all program that you want to call using %include....

Here is the program

Note: This macro take the full name of all file in a folder ending with .sas .

        Create a series of macro variable with file name .

        Resolve the value of each SAS file in %include statement using %do loop in macro.

%macro test(dir=);

filename filelist pipe "dir /b /s  &dir\*.sas";

data _null_;

   infile filelist truncover end=last;

   input filename $100.;

   call symputx('mac1'||left(_n_) , strip(filename));

     if last then call symputx('nobs' , _n_);

run;

%do i =1 %to &nobs;

%include "&&mac&i";

%end;

%mend test;

Hope it will help.....

%test(dir=\\133.3.3.3\a\a\DataPre\Macro)

Pallav
Calcite | Level 5

More simple program may be...

%macro findsas(path);

   filename filelist pipe "dir /b /s &path\*.sas";

   data _null_;

      infile filelist truncover;

      input filename $100.;

      call execute(cats('%include ',quote(trim(filename)), ';'));

   run;

%mend findsas;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 17 replies
  • 31593 views
  • 9 likes
  • 11 in conversation