DATA Step, Macro, Functions and more

File name in macro variable.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

File name in macro variable.

Hello Frnds,

I have a doubt,

%let srcpath = %nrquote(/PROJECT/sourcefiles/);

%let file1 = %unquote(rohan.txt);

data XXX;

if (fileexist("&scrpath/&file1")) then do;

infile "&srcpath/&file1";

input date1 date9. number;

end;

run;

but this " fileexist("&scrpath/&file1"))" is not working. Code is not able to resole the macro variable. Please help.

Thanks.


Accepted Solutions
Solution
‎12-27-2011 03:55 AM
Super User
Posts: 10,041

File name in macro variable.

You do not need using quote function to mask some special characters for your situation.

%let path=c:\temp\;
%let file=class.txt;
data x;
 if fileexist("&path&file") then do;
 infile "&path&file"; 
 input;list;  end; 
run;

Ksharp

View solution in original post


All Replies
Solution
‎12-27-2011 03:55 AM
Super User
Posts: 10,041

File name in macro variable.

You do not need using quote function to mask some special characters for your situation.

%let path=c:\temp\;
%let file=class.txt;
data x;
 if fileexist("&path&file") then do;
 infile "&path&file"; 
 input;list;  end; 
run;

Ksharp

Frequent Contributor
Posts: 129

Re: File name in macro variable.

Ksharp is correct that the quote functions are not needed here, but that does not solve your issue.

The root problem is mis-spelling of macro variable name.  You use srcpath in the let statement and scrpath in the fileexist function.Smiley Happy

If that does not fix, then please include log for this code.

Occasional Contributor
Posts: 10

File name in macro variable.

Posted in reply to LarryWorley

Thanks Laary.... my bad Smiley Sad

Occasional Contributor
Posts: 10

File name in macro variable.

Thanks Kshrap. but i still wonder why was "%nrquote" not working

Super User
Super User
Posts: 7,070

File name in macro variable.

I do not see any trouble with the code (other than the typo in the macro variable name).  Do you have a copy of the log with the error message?

PROC Star
Posts: 7,487

File name in macro variable.

Tom and Rohan,

There is another potential problem in addition to the originally identified typo: the path is assigned ending in a "/", but another "/" is added during its use.

Frequent Contributor
Posts: 129

File name in macro variable.

All,

In this context on AIX (and HP-UX if my memory is correct), the double-slash, "//" does not cause a problem.  Directory navigation will treat as a single-quote. I think this standard behavior across unix flavors, but can not support that.  But regardless, my preference would be to leave the terminating '/' out of the macro variable definition.

To reinforce Tom's statement, a log would be nice to have for de-bugging errors. 

Super User
Posts: 10,041

Re: File name in macro variable.

Posted in reply to LarryWorley

In general, When we need to handle the macro variable, we need to use quote function to mask these special characters. But For your situation, You only pass the value of macro variable to Data Step Compiler, if you use quote function ,you will get error. Let me give an example.

%let sex=%nrbquote('F');

data female;

set sashelp.class;

where sex=&sex;

run;

This code will produce ERROR. Because you have masked single quote . when it arrived Data Step Compiler,

Compiler only take the value as F ,not 'F'. Why? because you masked this single quote.But If you use the code below, you will get right result. Why? because you didn't mask this single quote ,Compiler can recognize this single quote, Compiler think the  value you passed is 'F'.

%let sex='F';

data female;

set sashelp.class;

where sex=&sex;

run;

If you really want to use quote function around the value of macro variable ,then use %unquote to unmask single quote to let Complier know them correctly.

%let sex=%nrbquote('F');

data female;

set sashelp.class;

where sex=%unquote(&sex);

run;

Ksharp

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 294 views
  • 1 like
  • 5 in conversation