BookmarkSubscribeRSS Feed
SASEnthusiast
Obsidian | Level 7

I am trying to import a lot of data files from a single directory. My code is as below.

Every time I am running the program I get the following error. Where Am I going wrong.

 

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
%scan((&RC_Files),&st,&x) ne
ERROR: The condition in the %DO %WHILE loop, %scan((&RC_Files),&st,&x) ne, yielded an invalid or missing value, . The macro will
stop executing.
ERROR: The macro READALLRATECARDFILES will stop executing.

 


%macro get_filenames(location);
filename _dir_ "%bquote(&location.)";
data filenames(keep=memname);
handle=dopen( '_dir_' );
if handle > 0 then do;
count=dnum(handle);
do i=1 to count;
memname=dread(handle,i);
output filenames;
end;
end;
rc=dclose(handle);
run;
filename _dir_ clear;
%mend;

%macro ReadInputFiles(filePath, Fformat, sheetName, outDS);

proc import out=inlib.&outDS DATAFILE="&filePath"
dbms=&Fformat replace;
getnames=yes;
%if %length(%cmpres(&sheetName)) > 0 %then %do;
Sheet=&sheetName;
%end;
run;
data inlib.&outDS;
set inlib.&outDS;
if compress(cats(of _all_),,'kad') = '' then delete;
run;
%mend;

%macro ReadAllRateCardFiles;
%get_filenames(&inXLRCDir);
proc sql;
select memname into :RC_Files separated by ','
from filenames;
quit;
%put &RCFiles;
%let st = 1; 
%let DSNamePrefix = Std_RC_Set_;
%let x = %str(,) ;
%do %while(%scan((&RC_Files),&st,&x) ne );
%let FileName = %cmpres(%scan(%bquote(&RC_Files),&st,%str(,)));
data ReadFile_Status1;
/* if exists('ReadFile_Status','DATA') then 
set ReadFile_Status;
end;*/
length path $5000 DSName $50;

%let FilePath = "%trim(&inXLDir./&FileName)";
path = &FilePath;
FileExists = fileexist(path);
DSName = "&DSNamePrefix.&st";
%put &FilePath;
if FileExists > 0 then do;
call execute('%ReadExcelInput('||path||',csv,,'||DSName||')');
end;

run;

%let st = %sysevalf(&st + 1); 
%end;
%mend;
%ReadAllRateCardFiles;

 

 

1 REPLY 1
Astounding
PROC Star

Here's my suspicion.  This statement is the culprit:

 

%do %while(%scan((&RC_Files),&st,&x) ne );

 

Some paths to files may contain a dash or a slash (- or /).  When macro language evaluates whether the %DO %WHILE condition is still satisfied, it applies %EVAL to the comparison.  And when it sees a dash or slash, it interprets those as "minus" or "divide". 

 

Try switching from %SCAN to %QSCAN.  The %SCAN function returns an unquoted result, even when the incoming string is quoted.  Switching to %QSCAN will return a quoted result, treating - or / as text.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1350 views
  • 0 likes
  • 2 in conversation