BookmarkSubscribeRSS Feed
Chang
Quartz | Level 8

hi,

You wouldn't bother to write a loop for including SAS files when there are just few. However, I have >50 to do. For each file, I have used a %let to name the file in a macro variable, and a %include to bring it to the current SAS session as the following:

%let macro_folder    =    C:\SAS_macros_chang;

%let macro_1        =    write_access.sas;

%let macro_2        =    NIS_surveymeans.sas;

%let macro_3        =     NIS_surveyfreq.sas;

%let macro_4        =    reshape_univar_long2wide.sas;

%let macro_5        =    duplicate_APPEND.sas;

%include "&macro_folder.\&macro_1.";

%include "&macro_folder.\&macro_2.";

%include "&macro_folder.\&macro_3.";

%include "&macro_folder.\&macro_4.";

%include "&macro_folder.\&macro_5.";

I have attempted to replace the five pairs of %let and %include with the following code but just can't get SAS include the files correctly.

%sysmacdelete include_many_files;

%macro include_many_files;

    %let list= write_access NIS_surveymeans NIS_surveyfreq reshape_univar_long2wide duplicate_APPEND ;

    %let n = %sysfunc(countw(&list.)) ;

        %do i= 1 %to &n. ;   

            %let macro_&i. = %scan(&list., &i.) ;

            %include "&macro_folder.\&&macro_&i..sas" ;

        %end;

%mend include_many_files;

%include_many_files;

This is the log. Apparently the file extension .sas is not there. I am not sure why my two periods can't get it right.

WARNING: Physical file does not exist, C:\SAS_macros_chang\write_accesssas.

ERROR: Cannot open %INCLUDE file C:\SAS_macros_chang\write_accesssas.

WARNING: Physical file does not exist, C:\SAS_macros_chang\NIS_surveymeanssas.

ERROR: Cannot open %INCLUDE file C:\SAS_macros_chang\NIS_surveymeanssas.

WARNING: Physical file does not exist, C:\SAS_macros_chang\NIS_surveyfreqsas.

ERROR: Cannot open %INCLUDE file C:\SAS_macros_chang\NIS_surveyfreqsas.

WARNING: Physical file does not exist, C:\SAS_macros_chang\reshape_univar_long2widesas.

ERROR: Cannot open %INCLUDE file C:\SAS_macros_chang\reshape_univar_long2widesas.

WARNING: Physical file does not exist, C:\SAS_macros_chang\duplicate_APPENDsas.

ERROR: Cannot open %INCLUDE file C:\SAS_macros_chang\duplicate_APPENDsas.

Any advice would be highly appreciated.

Chang

4 REPLIES 4
AhmedAl_Attar
Ammonite | Level 13

Try this

%macro include_many_files; 

    %let list= write_access NIS_surveymeans NIS_surveyfreq reshape_univar_long2wide duplicate_APPEND ;  

    %let n = %sysfunc(countw(&list.)) ;  

        %do i= 1 %to &n ;     

            %include "&macro_folder.\%scan(&list, &i).sas" ; 

        %end; 

%mend include_many_files; 

%include_many_files;

Hope it helps,

Ahmed

Tom
Super User Tom
Super User

%let macro_folder    =    C:\SAS_macros_chang; 

filename code "&macro_folder";

%include code( write_access NIS_surveymeans NIS_surveyfreq reshape_univar_long2wide duplicate_APPEND) / source2 ;


Astounding
PROC Star

First, to address your original question ... you need three dots instead of two:

%include "&macro_folder.\&&macro_&i...sas";

The first dot delimits &i, the second delimits &macro_1, and the third becomes text.

More important, you need to look at the subject known as the AUTOCALL facility.  If your macros are "properly" defined, you don't need to %include any of them.  "Properly" defined means that write_access.sas defines %write_access, NIS_surveymeans.sas defines %NIS_surveymeans, etc.  In that case, just add one statement to your program:

options sasautos="&macro_folder";

Then all the macros in that folder will automatically be available to the program. 

Good luck.

petlove
Obsidian | Level 7

Does this work?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2457 views
  • 1 like
  • 5 in conversation