SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Mxmaverick
Calcite | Level 5

Hi everyone, I just wanted to know if anyone knows how to import many excel files and at the same time, import specific sheets from this files. I already have a macro that can do both process but separately. Thank in advance. BTW this are my two codes 

 

THIS ONE IMPORT MULTIPLE FILES 

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

proc import dbms=xlsx out= merged
datafile= "C:\USERS\.....\.XLSX" replace ;
run;

%MultImp(dir=C:\Users\Usuario\Desktop\Prompts\Para_Promp_1\Originales\Empresas_Certificadas_V2\Empresas_Certificadas_Despacho_de_Mercancías_98LA,out=merged);

 

 

and THIS ONE IMPORT MULTIPLE SHEETS

 

%macro dataload(sheet);

proc import out= &sheet
datafile = 'C:\Users\...\.....xlsx'
dbms = xlsx replace;
sheet = "&sheet";
getnames = yes;
run;

%mend dataload;
%dataload(sheet1);
%dataload(sheet2);

3 REPLIES 3
Reeza
Super User

 

%macro importXLSX(path=);

libname myfile XLSX "&path";

proc copy in=myfile out=work;
run;

libname myfile;

%mend;

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

str = catt('%importXLSX(path=', fname, ');');

call execute (str);

run;

 

Your macro imports and appends the file to a single format. Are you expecting that as well. Otherwise, something like the above may work. 

 

1. Make a macro to import all files from one XLSX doc using LIBNAME

2. Call macro for each XLSX file.

 


@Mxmaverick wrote:

Hi everyone, I just wanted to know if anyone knows how to import many excel files and at the same time, import specific sheets from this files. I already have a macro that can do both process but separately. Thank in advance. BTW this are my two codes 

 

THIS ONE IMPORT MULTIPLE FILES 

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

proc import dbms=xlsx out= merged
datafile= "C:\USERS\.....\.XLSX" replace ;
run;

%MultImp(dir=C:\Users\Usuario\Desktop\Prompts\Para_Promp_1\Originales\Empresas_Certificadas_V2\Empresas_Certificadas_Despacho_de_Mercancías_98LA,out=merged);

 

 

and THIS ONE IMPORT MULTIPLE SHEETS

 

%macro dataload(sheet);

proc import out= &sheet
datafile = 'C:\Users\...\.....xlsx'
dbms = xlsx replace;
sheet = "&sheet";
getnames = yes;
run;

%mend dataload;
%dataload(sheet1);
%dataload(sheet2);


 

Mxmaverick
Calcite | Level 5

What I want is to import all files in a directory and at the same time, import specific sheets from those files 

Reeza
Super User
Modify the PROC COPY to select only the sheets of interest, using the SELECT statement.
proc copy in=mylib out=work;
select sheet1 sheet2;
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1090 views
  • 0 likes
  • 2 in conversation