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
Rhodochrosite | Level 12

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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