Date: Wed, 27 Feb 2008 20:10:19 +0000
Reply-To: toby dunn <tobydunn@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: toby dunn <tobydunn@HOTMAIL.COM>
Subject: Re: Evaluate a substring in a macro?
In-Reply-To: <f3ed116f0802271144w266517f7m6533232e3fc7082c@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Bruce ,
ERROR: Required operator not found in expression: substr(&db,1,8) = "hdfbatch"
Is a execution time Macro error that comes from an implicit or explicit %Eval or from %SysEval .
Since you only have one of those (an implicit call to %Eval )
%if substr(&db,1,8) = "hdfbatch" %then %do; its where you need to start.
I see 3 problems here:
1st.) Substr is a Data Step function while %Substr or %QSubstr are Macro functions.
Its an easy mistake to make forgetting the leading %. Its suprising it doesnt
more often given extremely the two syntaxs closely resembling each other.
2nd.) "hdfbatch", drop the Double Quotes. In the Macro language everything is text and the programmer delenates what text they want SAS to interpret by using & and %. In the Data Step words have meanings and the programer tells SAS not to interpret certain text by using single or double quote marks around them. Making that text a Text Literal tolken in the tolkenization of the code.
What does all this mean well simply put the %Eval will use the double quotes as a valid characters in the comparixson. So even if you had used %Substr it would have compared: ( hdfbatch = "hdfbatch" ) which would have yeilded a boolean 0 or not true comparison. So as a rule of thumb is that if there arent quote marks in the value dont use any.
3rd.) When doing a comparison whether in the macro or data step always standardize the case of your values. Its just a little defensive programming and should be a requirement for a programmer to do in the case of Macros.
So this leaves us with the following code:
%Macro Test(DB = ) ; <--- Switched to KeyWord Parameters
%If ( %Substr( %UpCase( &DB ) , 1 , 8 ) = HDFBATCH ) %Then %Do ;
%Put This Works ;
%End ;
%Else %Do ;
%Put This Does Not Work ;
%End ;
%Mend ;
%Test( DB = hdfbatch4545 ) <---- Dropped the unneccessary semicolon
Toby Dunn
"Don't bail. The best gold is at the bottom of barrels of crap."
Randy Pausch
"Be prepared. Luck is where preparation meets opportunity."
Randy Pausch
> Date: Wed, 27 Feb 2008 13:44:42 -0600
> From: chimanbj@GMAIL.COM
> Subject: Evaluate a substring in a macro?
> To: SAS-L@LISTSERV.UGA.EDU
>
> What I'm trying to do is pretty basic. If the parameter being passed
> through the macro starts with a certain string, I want to do something
> specific. Here is the code I'm trying to use:
>
> %macro test(db);
> %if substr(&db,1,8) = "hdfbatch" %then %do;
> %put This works;
> %end;
> %else %do;
> %put This does not work;
> %end;
> %mend;
> %test(hdfbatch4545);
>
>
> Where am I going wrong? The error message I'm getting is:
>
> ERROR: Required operator not found in expression: substr(&db,1,8) = "hdfbatch"
> ERROR: The macro TEST will stop executing.
_________________________________________________________________
Climb to the top of the charts! Play the word scramble challenge with star power.
http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_jan