DATA Step, Macro, Functions and more

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

Reply
Frequent Contributor
Posts: 99

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.

PROC Star
Posts: 1,561

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,561

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: 6,500

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: 6,500

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

That syntax would no include files from subdirectories.

Super User
Posts: 5,081

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

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?

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,081

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

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?

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: 198

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

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;

Ask a Question
Discussion stats
  • 17 replies
  • 6733 views
  • 4 likes
  • 11 in conversation