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

I have wirte a macro to important all xlsx in "C:\SASMACRO\data\F2+Blank\" and read them and merged in a dataset called "a."

 

However, I have problems is that the file dataset with one variable (filenames), which cannot read the blank.

 

So, if a xlsx data called "data 01", then it cannot read. 

 

Can some one help me to improve the problems that I can important xlsx file with blank in its name. 

 

Thx. 

 

 

%macro merge();
proc sql noprint;select count(filenames) INTO :myn from File;
%do i=1 %to &myn;
proc import out=datafile&i datafile="&&file&i" dbms=xlsx /*any data type*/replace;
run;
%end;
data a;
set %do j=1 %to &myn;
datafile&j
%end;
;
run;
%mend merge;
filename lib pipe 'dir "C:\SASMACRO\data\F2+Blank\*.xlsx" /b';
data file;
length filenames $ 40;
infile lib truncover;
input filenames : $;
filenames="C:\SASMACRO\data\F2+Blank\"||filenames;
run;
data _NULL_;
set file;
call symputx('file'||put(_n_,1.),filenames,'G');
run;
options sasautos=work;
%merge()
/*data would be merged in a*/

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Simplfying your code will help you.  Also note it is never a good idea to have any special characters in filenames or paths.

filename lib pipe 'dir "C:\SASMACRO\data\F2+Blank\*.xlsx" /b';

data file;
  infile lib truncover;
  input;
  call execute('proc import datafile="C:\SASMACRO\data\F2+Blank\"'||strip(_infile_)||'" out=inter dbms=xlsx; run;');
  call execute('data want; set want inter; run;');
run;

You will note that each iteration creates inter, then that gets set to want (want should be created before this step!).  

View solution in original post

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Simplfying your code will help you.  Also note it is never a good idea to have any special characters in filenames or paths.

filename lib pipe 'dir "C:\SASMACRO\data\F2+Blank\*.xlsx" /b';

data file;
  infile lib truncover;
  input;
  call execute('proc import datafile="C:\SASMACRO\data\F2+Blank\"'||strip(_infile_)||'" out=inter dbms=xlsx; run;');
  call execute('data want; set want inter; run;');
run;

You will note that each iteration creates inter, then that gets set to want (want should be created before this step!).  

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 880 views
  • 0 likes
  • 2 in conversation