BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
avz
Obsidian | Level 7 avz
Obsidian | Level 7

I found a helpful macro function to import multiple Excel files and combine them into a single SAS table from https://www.listendata.com/2013/10/sas-importing-multiple-excel-files-in.html

 

%macro MultImp(dir=,out=);

%let rc=%str(%'dir %")&dir.%str(\%" /A-D/B/ON%');
filename myfiles pipe %unquote(&rc);

data list;
length fname $256.;
infile myfiles truncover;
input myfiles $100.;

fname=quote(upcase(cats("&dir",'\',myfiles)));
out="&out";
drop myfiles;
call execute('
  proc import dbms=xlsx out= _test
            datafile= '||fname||' replace ;
  run;
  proc append data=_test base='||out||' force; run;
  proc delete data=_test; run;
');
run;
filename myfiles clear;

%mend;

%MultImp(dir=G:\MY DRIVE\Folder\test,out=merged);

 However, if the folder contains something other than an Excel file, the import fails:

 

ERROR: Error opening XLSX file -> G:\MY DRIVE\Folder\desktop.ini.xlsx .  It is either not an 
Excel spreadsheet or it is damaged.   Error code=8014900A
Requested Input File Is Invalid
ERROR: Import unsuccessful.  See SAS Log for details.

How can I ensure that the function only attempts to import .xlsx files containing a specific text string in the filename? 

 

Thanks for the help. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
%macro MultImp(dir=,out=);

%let rc=%str(%'dir %")&dir.%str(\%" /A-D/B/ON%');
filename myfiles pipe %unquote(&rc);

data list;
length fname $256.;
infile myfiles truncover;
input myfiles $100.;

fname=quote(upcase(cats("&dir",'\',myfiles)));
out="&out";
drop myfiles;

if find(fname, "xlsx", 'it') then 
call execute('
  proc import dbms=xlsx out= _test
            datafile= '||fname||' replace ;
  run;
  proc append data=_test base='||out||' force; run;
  proc delete data=_test; run;
');
run;
filename myfiles clear;

%mend;

%MultImp(dir=G:\MY DRIVE\Folder\test,out=merged);

https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p00ab6ey29t2i8n1ihel88tqtga...

 

Only import if the file is XLSX.

View solution in original post

4 REPLIES 4
PhilC
Rhodochrosite | Level 12

This is technically a DOS question.  Run these lines so you can see the RC macro variable.

%let dir=MYfiles;
%let rc=%str(%'dir %")&dir.%str(\%" /A-D/B/ON%');

%put NOTE: &=rc;

and run this line in a DOS shell to see how to use the DOS command that you are using.

dir /?
Reeza
Super User
%macro MultImp(dir=,out=);

%let rc=%str(%'dir %")&dir.%str(\%" /A-D/B/ON%');
filename myfiles pipe %unquote(&rc);

data list;
length fname $256.;
infile myfiles truncover;
input myfiles $100.;

fname=quote(upcase(cats("&dir",'\',myfiles)));
out="&out";
drop myfiles;

if find(fname, "xlsx", 'it') then 
call execute('
  proc import dbms=xlsx out= _test
            datafile= '||fname||' replace ;
  run;
  proc append data=_test base='||out||' force; run;
  proc delete data=_test; run;
');
run;
filename myfiles clear;

%mend;

%MultImp(dir=G:\MY DRIVE\Folder\test,out=merged);

https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p00ab6ey29t2i8n1ihel88tqtga...

 

Only import if the file is XLSX.

Tom
Super User Tom
Super User

Only keep the XLSX files in your list.  For example by adding a subsetting IF statement.

  if lowcase(scan(myfiles,-1,'.')='xlsx';

Updated macro.

%macro MultImp(dir=,out=);

filename myfiles pipe %bquote('dir "&dir\" /A-D/B/ON');

data list;
  length fname $256 ;
  infile myfiles truncover;
  input myfiles $100.;
  if lowcase(scan(myfiles,-1,'.')='xlsx';
  fname=quote(cats("&dir",'\',myfiles));
  out="&out";
  drop myfiles;
  call execute(catx(' '
    ,'proc import dbms=xlsx out= _test datafile=',fname,'replace;run;'
    ,'proc append data=_test base=',out,'force;run;'
    ,'proc delete data=_test; run;'
  ));
run;

filename myfiles clear;

%mend;

%MultImp(dir=G:\MY DRIVE\Folder\test,out=merged);

Note make sure you have complete control over how the data is entered into those XLSX sheets.  Otherwise your PROC APPEND step will fail when variables had the wrong type on some of the XLSX files.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1846 views
  • 3 likes
  • 4 in conversation