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

Hi Everyone,

 

I have 300 Excels files (file1, file2, file3.....file300) need to be read into SAS. Here is a hard code I tested and it runs well:

 

PROC IMPORT Out=output1

Datafile= "data\file1.xlsx"

DBMS=XLSX REPLACE;

SHEET='Sheet1';

GETNAMES=YES;

Run;

 

Since it's not efficient to call the Macro variable 300 times by using:

%Readin(file1);

%Readin(file2);

%Readin(file3);

   ...

   ...

%Readin(file300);

 

So I use a do-loop in this case, but the code doesn't work. Do you have any suggestions that where I did incorrectly? 

 

%macro Readin;

%do i = 1 %to 300;

      PROC IMPORT Out=output&i

      Datafile= "data\file&i..xlsx"

      DBMS=XLSX REPLACE;

      SHEET='Sheet1';

      GETNAMES=YES;

      Run;

%end

%mend;

%Readin;

 

 

Thank you,

R.B

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Restart SAS and try it in a new session.
If not, then re-type each quote in the code. This can happen if you copied the cost from Word/PDF which add some invisible characters to the text sometimes.

View solution in original post

4 REPLIES 4
Reeza
Super User
options mprint symbolgen;

%macro Readin;

%do i = 1 %to 3;

      PROC IMPORT Out=output&i

      Datafile= "data\file&i..xlsx"

      DBMS=XLSX REPLACE;

      SHEET='Sheet1';

      GETNAMES=YES;

      Run;

%end


%mend;

%Readin;


What does "not work" mean? Can you post the log from the code above?

 


@runrunbunny wrote:

Hi Everyone,

 

I have 300 Excels files (file1, file2, file3.....file300) need to be read into SAS. Here is a hard code I tested and it runs well:

 

PROC IMPORT Out=output1

Datafile= "data\file1.xlsx"

DBMS=XLSX REPLACE;

SHEET='Sheet1';

GETNAMES=YES;

Run;

 

Since it's not efficient to call the Macro variable 300 times by using:

%Readin(file1);

%Readin(file2);

%Readin(file3);

   ...

   ...

%Readin(file300);

 

So I use a do-loop in this case, but the code doesn't work. Do you have any suggestions that where I did incorrectly? 

 

%macro Readin;

%do i = 1 %to 300;

      PROC IMPORT Out=output&i

      Datafile= "data\file&i..xlsx"

      DBMS=XLSX REPLACE;

      SHEET='Sheet1';

      GETNAMES=YES;

      Run;

%end

%mend;

%Readin;

 

 

Thank you,

R.B

 


 

runrunbunny
Obsidian | Level 7

Hi Reeza,

Thank you so much for your reply. Below is the log says I have unbalanced quotation marks:

 

NOTE: The quoted string currently being processed has become more than 262 characters long. You might have unbalanced quotation

marks.

 

But I don't see any unmatched quotation marks in the code. Could you please kindly advise? @Reeza 

 

Thank you,

R.B

Reeza
Super User
Restart SAS and try it in a new session.
If not, then re-type each quote in the code. This can happen if you copied the cost from Word/PDF which add some invisible characters to the text sometimes.
runrunbunny
Obsidian | Level 7

It works! I really appreciate your solution, Reeza. Have good day!

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
  • 4 replies
  • 2962 views
  • 0 likes
  • 2 in conversation