Date: Wed, 19 Jun 1996 15:07:45 +0100
Reply-To: John Whittington <johnw@MAG-NET.CO.UK>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: John Whittington <johnw@MAG-NET.CO.UK>
Subject: Retain statement
There have recently been some lively exchanges about RETAIN which may have
seemed confused and/or confusing to those with less experience of SAS. I
therefore thought I would briefly summarise the situation as I understand it:
1...Variables deriving from datasets which are SET or MERGEd (*even if* they
are SET or MERGEd in conditional statements which are never actually
executed) are effectively RETAINed, even when there is no RETAIN statement.
There is thus never any point in RETAINing such variables (unless one wishes
to use RETAIN for its incidental effect of determing the order of those
variables in the PDV).
2...The usual situation is for a list of variable names to be specified
explicitly in a RETAIN statement - in which case the position of the RETAIN
statement is of NO importance to its 'RETAIN' functionality. All the
specified variables named WILL be RETAINed, provided that they appear
somewhere in the DATA step, either coming from a dataset that is SET or
MERGEd, or being created by assignment statements within the DATA step. If
the RETAIN statement represents the *only* mention of a variable (i.e. it is
not SET, MERGEd or created by assignment, LENGTH, ATTIBUT or ARRAY), then a
basic RETAIN statement will *not* cause the variable to be created. This
all remains true REGARDLESS of where the RETAIN statement is placed within
the DATA step.
3...If the variables to be RETAINed are specified implicitly (e.g. RETAIN
_ALL_), then the positioning of that statement IS important. Only those
variables already in the PDV (whether coming from a SET/MERGE, or created
within the DATA step) when the RETAIN statement is encountered will be
RETAINed. The RETAIN statement therefore must be placed *after* the first
appearance of any variables that one wishes to be RETAINed.
4...One other situation in which position of RETAIN can be of importance is
if one uses the option to specify an initial value in the RETAIN statement.
This effectively acts as an assignment statement, as well as invoking the
RETAIN functionality - and, as such, can therefore establish the attributes
of the variable (char/numeric, length) if the RETAIN statement represents
the first encounter with a variable in the DATA step. If other occurrences
of the variable (from SET/MERGE or statements within the DATA set) have
different attributes from those stated/implied in the RETAIN statement
(usually a programming error), behaviour will depend upon the relative
placement of the RETAIN statement.
5...RETAIN with an explist list of variables can be used to dictate the
order these variables will appear in the PDV (even if they have come from
SET/MERGE). Since RETAIN cannot &change* the position of a variable in the
PDV after its position has been established by some other mention, the
safest place to put a RETAIN statement being used for this purpose is at the
very top of the DATA step. The same can be achieved in other ways (e.g.
with LENGTH or ARRAY statements), but RETAIN is the simplest unless one
actually wants the functionality of these other statement.
Dr John Whittington, Voice: +44 1296 730225
Mediscience Services Fax: +44 1296 738893
Twyford Manor, Twyford, E-mail: email@example.com
Buckingham MK18 4EL, UK CompuServe: 100517,3677