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
Meteorite | Level 14

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
Meteorite | Level 14

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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 7822 views
  • 8 likes
  • 5 in conversation