Date: Wed, 28 Apr 2004 16:46:56 -0700
Reply-To: Dale McLerran <stringplayer_2@YAHOO.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Dale McLerran <stringplayer_2@YAHOO.COM>
Subject: Re: "Feature" of Implied Retain
In-Reply-To: <200404282216.i3SMGI809457@listserv.cc.uga.edu>
Content-Type: text/plain; charset=us-ascii
Don,
This is not correct. If the accumulator variable is set to
missing during execution of the datastep, then it is missing at
the time that the accumulator is next required. The following
code demonstrates:
1 data test;
2 do i=1 to 5;
3 /* Set addend to missing for observations 1 */
4 /* and 4. Otherwise, addend is nonmissing. */
5 if i=1 | i=4 then x=.;
6 else x=ceil(10*ranuni(1234579));
7 output;
8 end;
9 run;
NOTE: The data set WORK.TEST has 5 observations and 2 variables.
NOTE: DATA statement used:
real time 1.43 seconds
cpu time 0.00 seconds
10
11 data _null_;
12 set test;
13 /* initialize accumulator to missing at 4th observation */
14 if _n_=4 then y=.;
15 put "Before sum statement: " _n_= y= x=;
16 y + x;
17 put "After sum statement: " _n_= y= x= /;
18 run;
Before sum statement: _N_=1 y=0 x=.
After sum statement: _N_=1 y=0 x=.
Before sum statement: _N_=2 y=0 x=10
After sum statement: _N_=2 y=10 x=10
Before sum statement: _N_=3 y=10 x=2
After sum statement: _N_=3 y=12 x=2
Before sum statement: _N_=4 y=. x=.
After sum statement: _N_=4 y=. x=.
Before sum statement: _N_=5 y=. x=7
After sum statement: _N_=5 y=7 x=7
During compilation, SAS initializes the accumulator variable to
zero so that prior to the first execution of y+x, y is already 0.
The addend for the first observation is missing. Because the
accumulator variable was initialized during compilation to 0, we
have 0+. = 0 for observation 1. When the fourth observation is
reached, the accumulator is initialized to missing and the
addend is also missing. The result of .+. is ., not zero. The
accumulator is not somehow set to zero during execution of the
sum statement.
This leads me to question whether SAS should initialize the
accumulator to zero. If the first addend encountered is missing,
should we return the result zero or missing? A strong case could
be made for returning a missing value, but SAS presently returns
the value 0. If only missing values have been recorded thus far,
should we return the accumulated value 0? I would argue not.
Dale
--- Don Stanley <don_stanley@PARADISE.NET.NZ> wrote:
> Probably didn't phrase that very well, what I meant was that in the
> circumstance of this thread (as it originally started), when a
> variable
> used as a sum accumulator is set to missing in datastep code, it is
> effectively treated as though it is reset to zero before the next
> accumulation.
>
> Don
=====
---------------------------------------
Dale McLerran
Fred Hutchinson Cancer Research Center
mailto: dmclerra@fhcrc.org
Ph: (206) 667-2926
Fax: (206) 667-5977
---------------------------------------
__________________________________
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs
http://hotjobs.sweepstakes.yahoo.com/careermakeover