BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
arunr
Calcite | Level 5

Hi,

 

I'm getting an error using following code. please help what am I missing ?

 

%let indir=//folder/path;

%PUT &indir;

%if %substr(&indir,%length(&indir),1)^=/ %then %let indir=&indir/;

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
T^=/

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

It appears that the bare / character may be treated as expecting division (numeric)

See if  forcing quotes does what you expect:

%if %quote(%substr(&indir,%length(&indir)))^= "/" %then %let indir=&indir/;

When the second parameter of Substr is the length of the variable you get the last character, so the ,1 isn't needed.

View solution in original post

6 REPLIES 6
jimbarbour
PROC Star

Something returned from your functions is coming back as character instead of numeric.  You might try doing a STRIP for the data you're trying to get the length of.

 

Jim

ballardw
Super User

It appears that the bare / character may be treated as expecting division (numeric)

See if  forcing quotes does what you expect:

%if %quote(%substr(&indir,%length(&indir)))^= "/" %then %let indir=&indir/;

When the second parameter of Substr is the length of the variable you get the last character, so the ,1 isn't needed.

Tom
Super User Tom
Super User

That cannot work.  You are comparing a one character string to a three character string.  They will never be equal.

 

Either add real quotes to both sides.

Or add macro quoting to both sides.

KentL
Obsidian | Level 7

Beautiful.  %quote() comes to the rescue!  Thanks.

Tom
Super User Tom
Super User

%EVAL() is looking for the numbers you want to divide.  Macro quoting helps.

904   %if /=/ %then %do;
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
       /=/
ERROR: Skipping to next %END statement.
905     %put equal;
906   %end;
907   %if %str(/)=%str(/) %then %do;
908     %put equal;
equal
909   %end;

So add macro quoting on both sides of the operator.

%if %qsubstr(&indir,%length(&indir))^=%str(/) %then %let indir=&indir/;

 

jimbarbour
PROC Star

My first try was not the issue.  Try wrapping the left side of the equation in %BQUOTE.  

 

This seems to be working for me:

%let indir=//folder/path;
%PUT &indir;
%if %bquote(%substr(&indir,%length(&indir),1)) ^= %bquote(/) %then 
	%DO;
		%let indir=&indir/;
	%END;
%else
	%DO;
		%put	NOTE:  No action taken;
	%END;

Jim

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 5996 views
  • 8 likes
  • 5 in conversation