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 "¯o_folder.\¯o_1.";
%include "¯o_folder.\¯o_2.";
%include "¯o_folder.\¯o_3.";
%include "¯o_folder.\¯o_4.";
%include "¯o_folder.\¯o_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 "¯o_folder.\&¯o_&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
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 "¯o_folder.\%scan(&list, &i).sas" ;
%end;
%mend include_many_files;
%include_many_files;
Hope it helps,
Ahmed
%let macro_folder = C:\SAS_macros_chang;
filename code "¯o_folder";
%include code( write_access NIS_surveymeans NIS_surveyfreq reshape_univar_long2wide duplicate_APPEND) / source2 ;
First, to address your original question ... you need three dots instead of two:
%include "¯o_folder.\&¯o_&i...sas";
The first dot delimits &i, the second delimits ¯o_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="¯o_folder";
Then all the macros in that folder will automatically be available to the program.
Good luck.
Does this work?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.