|
Avi - SAS is full of surprises! ;-)
This retain action happens because it's the default action of the data
step. Variables that are automatically retained are those that are
reloaded with each iteration, so the retain action isn't usually noticed
- as you indicate. It's a useful feature for a variety of reasons, but
the fact that it is default on some and not all variables might be
surprising.
The RETAIN statement is redundant for:
- Variables defined by SET, MERGE, MODIFY or UPDATE statements,
- Variables defined by statement options in SET, MERGE, MODIFY, UPDATE,
FILE and INFILE statements,
- Sum statements (i.e. "I+1;"),
- Initialized array statement variables
Search the list/SUGI papers on the DOW Loop, the SAS Supervisor, and the
PDV to get more information on how the data step works. Start by
reading the onlinedoc sections on the data step:
http://support.sas.com/onlinedoc/913/getDoc/en/lrcon.hlp/a000985872.htm
There's a little table that resides alongside the PDV called the ITMV
(Initialize to Missing Vector) - this flags the retain action on the
variables. It's a one-way process - variables can be set to "retain" but
not set to "don't retain". There's another 1-1 table with the PDV
called the DKT - (Drop Keep Table) - it has a similar sort of purpose.
If you _really_ want to clear all variables brought in with a set
statement then something like this could do the trick:
data sasadata(drop=:_);
if 0 then set sasdata;
array nums _numeric_;
array chars $ _char_;
do _i = 1 to dim(nums);
nums(_i)=.;
end;
do _j = 1 to dim(chars);
chars(_j)='';
end;
*program statements;
(conditionally) set sasdata;
*more program statements;
run;
This reads the structure of the incoming data set (but no data because 0
is false) to build the PDV correctly and give the arrays the variables
to reset. The arrays reset the number vars to missing and the character
vars to blank. Then the data is conditionally read in.
Paul Choate
DDS Data Extraction
(916) 654-2160
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of avi
Sent: Tuesday, October 03, 2006 3:47 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Surprising retain feature
Hello,
Though an quite experienced user of SAS, i was quite surprised to learn
that in some cases, observations retain their values from one obs to
another, without specifying the RETAIN statement
("When variables are read with a SET, MERGE, or UPDATE , The variables
retain their values until new values become available").
Could someone explain exactly what and why it happens and the way to
overcome the problem thaht has caused some disturbing results in one of
my applications?
Thaks a lot
Avi
|