Date: Wed, 15 Oct 2003 13:31:43 -0400
Reply-To: Bruce.Gilsen@FRB.GOV
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Bruce Gilsen <Bruce.Gilsen@FRB.GOV>
Subject: Re: if(), while (), until() - short circuit evaulation ?
Content-type: text/plain; charset=US-ASCII
I realize this thread is kinda old, but I fell behind on SAS-L until now.
The one reference I've seen to this from SI was in the paper "DATA Step
Efficiency and Performance," by Jeffrey Polzin (who worked at SI), in the
SUGI 19 (1994) proceedings, pages 1574 - 1580. This paper says that
starting in V6.07, boolean short circuiting will be used in an IF or
DO/WHEN/SELECT that has OR or AND operators. Consistent with what Ron says
below, the paper notes that function calls w/side effects that require them
to execute in every obs, such as ranuni(), always execute, and prevent
boolean short circuiting.
I've always assumed that this continued to be the case until the present,
and advised users accordingly (including in my "SAS Program Efficiency for
Beginners" paper that I've presented at SUGI and NESUG), so I'm curious.
Bruce Gilsen
speaking only for myself (and my fantasy baseball team that *almost* won
for the 2nd straight year - I finished 2nd)
In article
<7BB320A15C3A62438497BE5D0A7484683928AC@m-phppo-1.phppo.cdc.gov>,
"Fehd, Ronald J. (PHPPO)" <rjf2@CDC.GOV> writes:
> From: Little Birdie
> Ron writes:
> > > From: Richard A. DeVenezia [mailto:radevenz@IX.NETCOM.COM] In DATA
> > > Step, does the while () clause (or I suppose eval in generation ...
> > > if() until(), etc...) use short circuit testing ?
> > >
> > > i.e.
> > > A=1
> > > B=2
> > >
> > > if (A=1 or B=2) then ... else ...
> > > if short-circuit evaluation then the then clause should run without
> > > having to test B=2
> > >
> > > if (A=0 and B=2) then ... else ...
> > > if short-circuit evaluation then the else clause should run without
> > > having to test B=2
> >
> > I'm not sure, probably need a Little Birdy to comment on that.
>
> I think the answer is "Usually".
> Things can get tricky with function calls.
>
> > however, one of the optimizing tricks in
> > SAS Programming Tips: A Guide to Efficient SAS Processing
>
> [copyright: 1990]
>
> > is
> > to replace
> > if A=1 and B=2 then
> > with
> > if A=1 then
> > if B=2 then ...
>
> That tip is incorrect in SAS 8 and SAS 9.
> I don't know about earlier SASes.
>
> [I would guess a 1990 copyright would be v6.]
>
> Here is SAS 9 on Windows XP:
>
> 1 %let na=10000;
> 2 %let nb=10000;
> 3 options stimer;
> 4 data _null_; run;
>
> NOTE: DATA statement used (Total process time):
> real time 0.04 seconds
> cpu time 0.03 seconds
>
>
> 5 data _null_; do a=1 to &na; do b=1 to &nb; if a=1 & b=1 then
k+1;
> end; end; put k=;
> 5 ! run;
>
> k=1
> NOTE: DATA statement used (Total process time):
> real time 3.03 seconds
> cpu time 3.01 seconds
>
>
>
> 6 data _null_; do a=1 to &na; do b=1 to &nb; if a=1 then if b=1
> then k+1; end; end; put
> 6 ! k=; run;
>
> k=1
> NOTE: DATA statement used (Total process time):
> real time 3.04 seconds
> cpu time 3.04 seconds
>
> [.sig with standard disclaimers]
|