Date: Thu, 21 Sep 2000 03:37:10 +0100
Reply-To: John Whittington <John.W@MEDISCIENCE.CO.UK>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: John Whittington <John.W@MEDISCIENCE.CO.UK>
Subject: Re: Difference between IF and WHERE
In-Reply-To: <E13bnlA-0002fS-00@relay1.netnames.net>
Content-Type: text/plain; charset="us-ascii"
At 13:38 20/09/00 -0400, Schechter Robert RS wrote (in part):
>>From the manual (online help):
>The subsetting IF and the WHERE statements are not equivalent. The two
>statements work differently and produce different output data sets in some
>cases. The most important differences are summarized as follows:
There seems to be at least one important omission from the list of 'danger
situations' that follows, and I haven't yet seen anyone else mention this ....
... whilst a WHERE statement effectively aborts an entire iteration of the
DATA step if the WHERE condition is 'false', with a subsetting IF statement
all executable statements prior to that IF statement are executed, even if
the IF condition is 'false'. In that situation, nothing will be written to
the output data set for that iteration BUT if any of the statements (prior
to IF) which were executed result in changes to variables which are
RETAINed (or stored in a temporary array), this changed value will appear
in the subsequent iteration(s) and may affect the output of those
iterations. For example, given test data:
data test ; do x = 1 to 10 ; output ; end ; run ;
... then the following code ....
data try ;
retain newvar 100 ;
set test ;
if x = 4 then newvar = 200 ;
where x in (2, 5, 7) ;
run ;
proc print ; run ;
data try ;
retain newvar 100 ;
set test ;
if x = 4 then newvar = 200 ;
if x in (2, 5, 7) ;
run ;
proc print ; run ;
... will give different outputs in the two cases:
OBS NEWVAR X
1 100 2
2 100 5
3 100 7
OBS NEWVAR X
1 100 2
2 200 5
3 200 7
... the reason, of course, being that with a WHERE statement, the iteration
for which X=4 never occurs, so that the conditional reassignment of the
value of NEWVAR (from 100 to 200) never takes place.
Kind Regards,
John
----------------------------------------------------------------
Dr John Whittington, Voice: +44 (0) 1296 730225
Mediscience Services Fax: +44 (0) 1296 738893
Twyford Manor, Twyford, E-mail: John.W@mediscience.co.uk
Buckingham MK18 4EL, UK mediscience@compuserve.com
----------------------------------------------------------------