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

Hi, all, 

Greetings, 

I hope you are enjoying a snug weekend evening; 

 

Can you guys simply check why this code is not working? 

 

%MACRO import(index);
proc import datafile='C:/Users/somebody/Desktop/Country data/&index.xls'
dbms=xls out=&index replace; run; %MEND import;

 

Then, I tried to execute macro %import as follows: 


%import(file1); %import(file2); %import(brabra.... etc. 

 

However, it didn't work, and what I got was only

 

ERROR: Physical file does not exist, C:/Users/somebody/Desktop/Country data/&index.xls

 

What seems to be a problem in this coding? 

 

Thank you in advance! 

 

Sincerely, 

KS -, 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You have two problems. The major one regards quotes. Macro variables do no resolve inside single quotes.

Second, the macro language uses the . (dot) to indicate the end of a macro variable in conjunction with other text. So when you have macro variable such as &index.xls the . would be removed and the resolved text would be come File1xls with no dot before the file extension. So when using that construct you need two dots.

 

So you code likely should look like:

%MACRO import(index);
   proc import datafile="C:/Users/somebody/Desktop/Country data/&index..xls"
     dbms=xls out=&index replace; 
   run; 
%MEND import;

Please post code in a text or code box opened on the forum with the </> or running man icon. The message windows will reformat text making it harder to read and follow.

 

You can see the code generated by your macro by using OPTIONS MPRINT; before executing the macro.

 

If these XLS (really!?) files are supposed to be of the same structure for variable names, types and lengths do not be surprised that the data sets generated by Proc Import do not agree as each file is processed separately.

View solution in original post

6 REPLIES 6
ballardw
Super User

You have two problems. The major one regards quotes. Macro variables do no resolve inside single quotes.

Second, the macro language uses the . (dot) to indicate the end of a macro variable in conjunction with other text. So when you have macro variable such as &index.xls the . would be removed and the resolved text would be come File1xls with no dot before the file extension. So when using that construct you need two dots.

 

So you code likely should look like:

%MACRO import(index);
   proc import datafile="C:/Users/somebody/Desktop/Country data/&index..xls"
     dbms=xls out=&index replace; 
   run; 
%MEND import;

Please post code in a text or code box opened on the forum with the </> or running man icon. The message windows will reformat text making it harder to read and follow.

 

You can see the code generated by your macro by using OPTIONS MPRINT; before executing the macro.

 

If these XLS (really!?) files are supposed to be of the same structure for variable names, types and lengths do not be surprised that the data sets generated by Proc Import do not agree as each file is processed separately.

KS99
Obsidian | Level 7
Thank you so much, ballardw!
I wish you a very good week!

KS -
mkeintz
PROC Star

The log error message shows that the macrovar &index is not being seen by the macro processor, so it is not resolved  (e.g. you want &index to be resolved to file1).

 

That's because your macro code has single quotes in this line:

proc import datafile='C:/Users/somebody/Desktop/Country data/&index.xls'

which should probably have double quotes as in

proc import datafile="C:/Users/somebody/Desktop/Country data/&index..xls"

The double quotes will allow the macro parser to look inside the quoted text and resolve &index.

 

Also note that the single . after &index, it now has a double ..   - which allows the first . to serve as a macrovar terminator, therefore requiring a second . to be part of the final file name ending with ".xls".

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
KS99
Obsidian | Level 7
I really appreciate your comments, mkeintz!
Wish you a very good evening!

Sincerely,
KS -,
SASKiwi
PROC Star

In addition to the other issues raised:

- Windows paths typically use back slashes not forward slashes, although most likely they will still work.

- If your SAS session is running on a remote SAS server you can't write to your own desktop drives. You will have to use a folder the remote server recognises.

KS99
Obsidian | Level 7
Thank you SASkiwi!

I will keep them in mind!

Sincerely,
KS -,

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 25. 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
  • 6 replies
  • 1117 views
  • 0 likes
  • 4 in conversation