DATA Step, Macro, Functions and more

How to use %include to include all macros under a folder?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 99
Accepted Solution

How to use %include to include all macros under a folder?

%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.


Accepted Solutions
Solution
‎08-30-2017 01:30 PM
Super User
Posts: 5,516

Re: How to use %include to include all macros under a folder?

[ Edited ]

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


All Replies
PROC Star
Posts: 1,760

Re: How to use %include to include all macros under a folder?

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.


Frequent Contributor
Posts: 99

Re: How to use %include to include all macros under a folder?

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)

Super Contributor
Posts: 349

Re: How to use %include to include all macros under a folder?

Hi,

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

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

%include "&macrodir\Import.sas";

Thanks,

Shiva

PROC Star
Posts: 1,760

Re: How to use %include to include all macros under a folder?

this is a command, not a statement.

run from the command box.

Super User
Super User
Posts: 7,074

Re: How to use %include to include all macros under a folder?

filename mymacs "&macrodir";

%include mymacs('*.sas');

N/A
Posts: 1

Re: How to use %include to include all macros under a folder?

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?

Super User
Super User
Posts: 7,074

Re: How to use %include to include all macros under a folder?

Posted in reply to psherman42wallabyway

That syntax would no include files from subdirectories.

Solution
‎08-30-2017 01:30 PM
Super User
Posts: 5,516

Re: How to use %include to include all macros under a folder?

[ Edited ]

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.

Super Contributor
Posts: 644

Re: How to use %include to include all macros under a folder?

Posted in reply to Astounding

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

Super User
Posts: 5,516

Re: How to use %include to include all macros under a folder?

Posted in reply to RichardinOz

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.

Super Contributor
Posts: 644

Re: How to use %include to include all macros under a folder?

Posted in reply to Astounding

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

Regular Contributor
Posts: 227

Re: How to use %include to include all macros under a folder?

Posted in reply to RichardinOz

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

Occasional Contributor
Posts: 14

Re: How to use %include to include all macros under a folder?

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)

Occasional Contributor
Posts: 14

Re: How to use %include to include all macros under a folder?

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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 17 replies
  • 8199 views
  • 5 likes
  • 11 in conversation