include multiple SAS files with a do loop

Reply
Contributor
Posts: 61

include multiple SAS files with a do loop

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

Regular Contributor
Posts: 211

Re: include multiple SAS files with a do loop

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

Super User
Super User
Posts: 6,365

Re: include multiple SAS files with a do loop

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


Respected Advisor
Posts: 4,995

Re: include multiple SAS files with a do loop

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.

Ask a Question
Discussion stats
  • 3 replies
  • 428 views
  • 1 like
  • 4 in conversation