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

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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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