Date: Tue, 27 Nov 2007 11:35:59 -0600
Reply-To: "data _null_," <datanull@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "data _null_," <datanull@GMAIL.COM>
Subject: Re: SAS Puzzle: Evaluating Logic in a Character Field
Content-Type: text/plain; charset=ISO-8859-1
I agreed and agree that using the data step to evaluate the expression
is best because "it has all the tools".
I was just wondering if "many" calls to CALL EXECUTE would ever be a
problem. I expect, if it ever was a problem it would be for a very
large number LOGIC expressions, producing a very large number of calls
to CALL EXECUTE.
On Nov 27, 2007 11:28 AM, Terjeson, Mark <Mterjeson@russell.com> wrote:
> <<I wonder if there were "many" instructions if there would
> be problems with call execute.>>
> No, that would be the beauty in using CALL EXECUTE().
> A couple of the other suggestions provided using SCAN(),
> etc. have to be built "knowing" how many possible elements
> there is going to be and all the types of delimiters. The
> call execute is exactly the same thing as if you wrote out
> the data step manual by hand.
> Thus, if you have a short expression
> flag = 0<15;
> or a long, or complex, expression
> flag = ((0+x)/y)+(3456*3/w)<((4+f)/q)<r<h<o<m<0.5<p;
> the call execute merely copys the whole string intact, so
> nothing gets broken by parsing that is not robust enough.
> You may have a mixture of comparison operators to custom
> handle, i.e. not just a single lessthan delimiter. You
> may also have all the comparison tokens such as le,gt,lt,eq,
> etc. so unless your "rules" are very strict and few, the
> amount of custom logic to be built to handle parsing the
> elements could have a wide range of stuff to be dealt with.
> After considering all that, if your rules are *not* rigid,
> the call execute becomes a quick sigh of relief.
> PS: yes, I know the long expression above doesn't make sense
> but you get the idea, that if you build some parsing logic,
> then you have to build it robust enough. The call execute
> doesn't even have to consider any of that, it is all
> automatically all-encompassing in regard to handling all
> the pieces. You just pass along the entire expression string.
> -----Original Message-----
> Sent: Tuesday, November 27, 2007 9:07 AM
> To: Terjeson, Mark
> Subject: Re: SAS Puzzle: Evaluating Logic in a Character Field
> I wonder if there were "many" instructions if there would be problems
> with call execute.
> On Nov 27, 2007 10:55 AM, Terjeson, Mark <Mterjeson@russell.com> wrote:
> > Hi Paul,
> > The resolve() function works only if the
> > string expression is a string literal. It
> > can't take the content of a string variable
> > and resolve the expression on the fly. So,
> > the next best thing is to use CALL EXECUTE().
> > Which merely writes datastep code on the fly
> > using the contents of your dataset variables.
> > data sample;
> > Logic = '0<0.9<1'; output;
> > Logic = '0<15'; output;
> > Logic = '0<1.3<1'; output;
> > run;
> > data _null_;
> > length stmp $200;
> > set sample end=done;
> > if _N_ eq 1 then call execute('data result;');
> > call execute("Logic = '"||Logic||"';");
> > call execute("flag = "||Logic||';');
> > call execute('output;');
> > if done then call execute('run;');
> > run;
> > Hope this is helpful.
> > Mark Terjeson
> > Senior Programmer Analyst, IM&R
> > Russell Investments
> > Russell Investments
> > Global Leaders in Multi-Manager Investing
> > -----Original Message-----
> > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> > Paul Walker
> > Sent: Tuesday, November 27, 2007 8:38 AM
> > To: SAS-L@LISTSERV.UGA.EDU
> > Subject: SAS Puzzle: Evaluating Logic in a Character Field
> > I have a one-column dataset of the following form:
> > Logic
> > -------
> > 0<0.9<1
> > 0<15
> > 0<1.3<1
> > I would like to append a numeric 0/1 flag to this dataset which
> > indicates
> > whether the expression contained in the column "Logic" is true (=1) or
> > false (=0).
> > Logic Flag
> > ------- ------
> > 0<0.9<1 1
> > 0<15 1
> > 0<1.3<1 0
> > Does anyone know how to do this?
> > TIA,
> > Paul