ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand

Reply
Occasional Contributor
Posts: 17

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand

 

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.

 

CODE -------

 

 proc sql;
  drop table dirlist;
  create table dirlist
  (path char(200), filename char(200));

  delete from dirlist;
  quit;

  %macro drive(dir,ext);
  %local filrf rc did memcnt name i;

    /* Assigns a fileref to the directory and opens the directory */
    %let rc=%sysfunc(filename(filrf,&dir));
    %let did=%sysfunc(dopen(&filrf));
     *  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;
     %return;
    %end;
     /* Loops through entire directory */
     %do i = 1 %to %sysfunc(dnum(&did));

       /* Retrieve name of each file */
       %let name=%qsysfunc(dread(&did,&i));

  *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;
          %put &dir/&name;
                 proc sql;
                 insert into dirlist
                 values( "&dir","&name");
                 quit;
        %end;

       /* If directory name call macro again */
        %else %if %qscan(%str(&name),2,.) = %then %do;
          %drive(%str(&dir)/%unquote(&name),&ext)
       proc sql;
                 insert into dirlist
                 values( "&dir","&name");
                 quit;
        %end;
     %end;


    /* Closes the directory and clear the fileref */
    %let rc=%sysfunc(dclose(&did));
    %let rc=%sysfunc(filename(filrf));

  %mend drive;

%drive(Therapeutic/Study/product/sdtm,sas7bdat);

Super Contributor
Posts: 440

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper

Posted in reply to rohit_prajapati

I will have to debug your code but first thing to look at is the comma in the filename at the macro invovation:

 


rohit_prajapati wrote:

 

%drive(Therapeutic/Study/product/sdtm,sas7bdat);


 

Try %drive(Therapeutic/Study/product/sdtm.sas7bdat);

 

Hope this helps,

- Jan.

 

 

Super Contributor
Posts: 440

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper

Posted in reply to jklaverstijn

Sorry I jumped the gun on this. The macro needs two parms. Oops.

 

Smiley Frustrated

Super User
Super User
Posts: 7,955

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper

Posted in reply to rohit_prajapati

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.

Occasional Contributor
Posts: 17

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper

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 [2].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;

Super User
Super User
Posts: 7,955

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper

Posted in reply to rohit_prajapati

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. 

Super Contributor
Posts: 440

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper


RW9 wrote:

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.

 

- Jan.

Super User
Super User
Posts: 7,955

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper

Posted in reply to jklaverstijn

"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.

Occasional Contributor
Posts: 17

Re: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric oper

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 !

 

 

-Rohit

Ask a Question
Discussion stats
  • 8 replies
  • 431 views
  • 0 likes
  • 3 in conversation