Date: Fri, 1 Feb 2008 13:15:58 -0500
Reply-To: Don Henderson <donaldjhenderson@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Don Henderson <donaldjhenderson@HOTMAIL.COM>
Subject: Re: checking for missing parameters
In-Reply-To: <020120081549.25265.47A33F83000054EC000062B1220076139405029A06CE9907@comcast.net>
Content-Type: text/plain; charset="us-ascii"
Ian,
I absolutely positively agree with you on the following points and I think
it is worth calling them out more specifically. Quoting from your note
below, they are:
- Quoting when you don't know what you are trying to hide can cause more
trouble than not quoting.
- if you really have to worry about such fine distinctions then you are
probably trying to use macro in way that it was never intended to be used
Another way to view this is that when writing macro code you need to
understand the context in which your macro is being invoked.
Regards,
-donh
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Ian
Whitlock
Sent: Friday, February 01, 2008 10:49 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: checking for missing parameters
Scott,
Yes. However it is important to understand what you want to test.
Consider:
data _null_ ;
call symput ( "X" , '%m' ) ;
run ;
%macro m ;
%mend m ;
%put >>&x<< ;
%put >>%superq(x)<< ;
The first %PUT shows that the final value of X is empty. The second shows
that X represents a macro call. Do you want to test the result of the macro
or the fact that there is a macro call?
I guess my real point is that I would want to know what I intend to hide
when making the test with %SUPERQ. If I were testing for state
abbreviations or arithmetic expressions, I would use %SUPERQ. If I had no
reason to expect difficulties with %EVAL, I would not. Quoting when you
don't know what you are trying to hide can cause more trouble than not
quoting.
On the other hand, if you really have to worry about such fine distinctions
then you are probably trying to use macro in way that it was never intended
to be used, i.e. you are using the wrong tool.
Ian Whitlock
-------------- Original message ----------------------
From: "Scott Bucher" <ir.bucher@gmail.com>
> Thanks Ian, Toby, and Ed,
>
> So if I want a string of blanks to issue an error message, generally
> the safest choice would typically be:
> %if %superq(x) = %str() %then %put ERROR: x is missing (or a
> string of blanks);
>
>
> Whereas if I do not want a string of blanks to issue an error message,
> I could use:
> %if %length(%superq(x)) = 0 %then %put ERROR: x is missing;
> or:
> %if not %length(%superq(x)) %then %put ERROR: x is missing;
>
> -Scott
>
>
>
> On Jan 31, 2008 9:48 PM, <iw1junk@comcast.net> wrote:
>
> > Summary: Difference between length and equality tests.
> > #iw-value=1
> >
> > Toby,
> >
> > It might be more important to correct Scott's basic premise.
> >
> > Blanks and nothing compare equal. Length is used to test the
> > difference.
> >
> > 1 %let x = %str( ) ;
> > 2 %put %eval(%length(&x)=0) ;
> > 0
> > 3 %put %eval(&x=) ;
> > 1
> > 4 %put %eval(&x=%str()) ;
> > 1
> >
> > If you want blanks to mean nothing %LENGTH(%SUPERQ(X))=0 if far from
> > safe.
> >
> > Ian Whitlock
> > ==============
> >
> > Date: Thu, 31 Jan 2008 22:47:03 +0000
> > Reply-To: toby dunn <tobydunn@HOTMAIL.COM>
> > Sender: "SAS(r) Discussion"
> > From: toby dunn <tobydunn@HOTMAIL.COM>
> > Subject: Re: checking for missing parameters
> > Comments: To: Scott Bucher <ir.bucher@gmail.com>
> > In-Reply-To:
> > <1ba1507b0801311359m5a43cc94w87189bfa64c5d3e@mail.gmail.com>
> > Content-Type: text/plain; charset="iso-8859-1"
> >
> > You missed a Method:
> >
> > ( %Length( %SuperQ(X) )> 0 )
> >
> > Which by the way is the safest way to check whether a parameter has
> > a specified value.
> >
> > 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: Thu, 31 Jan 2008 16:59:40 -0500 > From: ir.bucher@GMAIL.COM
> > > >
> > Subject: checking for missing parameters > To: SAS-L > > Hi All, >
> > > I have been reviewing a few macros presented in conference papers,
> > and > noticed the relatively simple act of checking for missing
> > parameters has > been done a number of different ways.
> > Below, I have presented five different > methods. I am wondering
> > what is generally the most effective method, (which > may not be
> > presented above)? > > %macro test(x = ); > %if &x = %then %put
> > ERROR: x is missing (method 1); > > %if &x = %str() %then %put
> > ERROR: x is missing (method 2); > > %if %length(&x) = 0 %then %put
> > ERROR: x is missing (method 3); > > %if %superq(x) = %then %put
> > ERROR: x is missing (method 4); > > %if %superq(x) = %str() %then
> > %put ERROR: x is missing (method 5); > > %if
> > %length(%sysfunc(compress(&x, ' '))) = 0 %then %put ERROR: x is >
> > missing (method 6); > %mend test; > > %test(); > > As far as I can
> > tell, #2 and #3 are equivalent, both being attempts to avoid > the
> > confusion of the ostensibly unbalanced (=) found in #1; or is there
> > some > practical distinction between using %str v. %length in this
> > context? > However, method 4 (using %superq) would generally be the
> > safest and simplest > method, as it would execute properly if the
> > value of the parameter had > special characters. Method #5 combines
> > the virtues of being effective and > avoiding conversion. I'm not
> > sure what #6 achieves beyond #3. > I hope this is not
> > hair-splitting, but I expect I may be missing some subtle >
> > (probably glaringly obvious for others), non-trival differences. My
> > goal is > to understand the best approach so I can consistently
> > apply one method (i.e. > #5) across my macros. > > Thanks, > Scott
> > Bucher > Associate Education Analyst > NYC Dept. of Education
> >
> >
> >
|