06-17-2016 07:18 AM
i am trying to read below code to read sas7bdat file from study path, while my study path having special charecter like " ' [ ` $" as file name as well as folder name. I have already used %BQUOTE, %NRSTR, %QCMPRES, %EVAL.... but still same error occurs. Code not able to read this special characters. Please help with this error.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is
required. The condition was: %qupcase(%qscan(&name,-1,.)) = %qupcase(&ext)
ERROR: The macro DRIVE will stop executing.
drop table dirlist;
create table dirlist
(path char(200), filename char(200));
delete from dirlist;
%local filrf rc did memcnt name i;
/* Assigns a fileref to the directory and opens the directory */
* rs_data = &rc;
*main_data = &did;
/* Make sure directory can be open */
%if &did eq 0 %then %do;
%put Directory &dir cannot be open or does not exist;
/* Loops through entire directory */
%do i = 1 %to %sysfunc(dnum(&did));
/* Retrieve name of each file */
*to stored name in sas dataset;
/* Checks to see if the extension matches the parameter value */
/* If condit1ion is true print the full name to the log */
%if %qupcase(%qscan(%str(&name),-1,.)) = %qupcase(%str(&ext)) %then %do;
insert into dirlist
/* If directory name call macro again */
%else %if %qscan(%str(&name),2,.) = %then %do;
insert into dirlist
/* Closes the directory and clear the fileref */
06-17-2016 07:48 AM
I will have to debug your code but first thing to look at is the comma in the filename at the macro invovation:
Hope this helps,
06-17-2016 08:01 AM
The real question here is why do you have special characters in filename or path, it is highly advised not to do this - a good reason being what you have found below. Coding round it will not solve the problem.
Second question is why this mass of code?
filename tmp pipe 'dir "<path>" /b'; data want; length buff $200; infile tmp; input buff $; run;
Will give you a directory listing. If thats not what you want, post a scenario of have and want.
06-17-2016 08:06 AM
I want to read the sas7bdat files located in studies location, where for some of my already existed file & folder name has special characters like "open cdisc_20160524 .pdf" or folder name "Neil's", So the error occurs for perticular code only - %if %qupcase(%qscan(%nrstr(&name),-1,.)) = %qupcase(%nrstr(&ext)) %then %do;
06-17-2016 08:15 AM
What do you mean "read"? In your code all you are doing is compiling a directory listing of files in a given location. That can be achieved by the code I gave, if you need a specific filetype then put:
filename tmp pipe 'dir "<path>\*.sas7bdat" /b'; data want; length buff $200; infile tmp; input buff $; run;
This will work regardless of special chaaracters and such like, no need to try and code round it yourself.
06-17-2016 08:15 AM
The real question here is why do you have special characters in filename or path, i
Yes that's causing the mayhem. If a directory contains a file or directory with a single quote the th eprogram borks. Happened to me because I have a subdir called "Video's". It is advised against but oftentimes real life does have a mind of its own apart from the SAS best practices.
I must admit that I have not yet found a way to program around it.
06-17-2016 08:22 AM
"It is advised against but oftentimes real life does have a mind of its own apart from the SAS best practices."
No, its not just good practice in SAS terms, it is best practice in all computing terms. Nowhere in the history of computing has it been written that adding special characters to a path or filename is a good idea. But you are right, in the real world people who don't understand or don't have to deal with it do put things like this in, and it needs to be communicated backwards to be fixed. Otherwise everyone using that will have problems.
06-20-2016 12:27 AM
Thanks you all for the wonderfull discussion.
But it seems there are no solution for same.
Anyone anywhere used to read Special characters in this type of senerio ever in your past carrier?
Appreciate your help !