Include macros in SAS

This macro will include the external macros in a library on the disk. This will be done, so you are sure that SAS will choose the external macros not the same macros in libraries included in SASAUTOS. The macros first choosen by SAS will be the macros found in Work.Sasmacr library. Including the external macros will put these macros in this library. Example : %IncludeExternalMacros(dir=&RootPath/Macros/External/);

%macro IncludeExternalMacros(dir=);
%local fileref rc did dnum dmem memname filename didc;                                                                                
%let rc=%sysfunc(filename(fileref,&dir));                                                                                             
%let did=%sysfunc(dopen(&fileref));                                                                                                   
%if &did=0 %then %do;                                                                                                                 
%put ERROR: Directory does not exist;                                                                                               
%put ERROR: The macro will terminate;                                                                                             
%return;                                                                                                                            
%end;                                                                                                                                 
%let dnum=%sysfunc(dnum(&did));                                                                                                       
%do dmem=1 %to &dnum;                                                                                                                 
%let memname=%qsysfunc(dread(&did,&dmem));                                                                                          
%if %qupcase(%qscan(&memname,-1,.)) = SAS %then %do;                                                                              
%let filename=%scan(&memname,1,.);                                                                                              
%inc "&dir.&filename..sas";                                                                                                     
%end;                                                                                                                             
%end;                                                                                                                                 
%let didc=%sysfunc(dclose(&did));                                                                                                     
%let rc=%sysfunc(filename(fileref));                                                                                                  
%mend;

It’s also possible to use this simple piece of SAS-code found below.

filename Macros "<Path>";

%inc Macros("*.sas");