| Date: | Thu, 3 Mar 2005 05:15:21 +0000 |
| Reply-To: | toby dunn <tobydunn@HOTMAIL.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | toby dunn <tobydunn@HOTMAIL.COM> |
| Subject: | Re: SUSPECT: Re: Retain |
| In-Reply-To: | <5EFEE0A95CCEEC408CCE5BFD2AF87FB00103BDF9@cba0e2k04.CBA0.centerbeam.com> |
| Content-Type: | text/plain; format=flowed |
|---|
Default DATA Step Behavior
Without a RETAIN statement, SAS automatically sets variables that are
assigned values by an INPUT or assignment statement to missing before each
iteration of the DATA step.
Assigning Initial Values
Use a RETAIN statement to specify initial values for individual variables, a
list of variables, or members of an array. If a value appears in a RETAIN
statement, variables that appear before it in the list are set to that value
initially. (If you assign different initial values to the same variable by
naming it more than once in a RETAIN statement, SAS uses the last value.)
You can also use RETAIN to assign an initial value other than the default
value of 0 to a variable whose value is assigned by a sum statement.
Redundancy
It is redundant to name any of these items in a RETAIN statement, because
their values are automatically retained from one iteration of the DATA step
to the next:
variables that are read with a SET, MERGE, MODIFY or UPDATE statement
a variable whose value is assigned in a sum statement
the automatic variables _N_, _ERROR_, _I_, _CMD_, and _MSG_
variables that are created by the END= or IN= option in the SET, MERGE,
MODIFY, or UPDATE statement or by options that create variables in the FILE
and INFILE statements
data elements that are specified in a temporary array
array elements that are initialized in the ARRAY statement
elements of an array that have assigned initial values to any or all of the
elements on the ARRAY statement.
You can, however, use a RETAIN statement to assign an initial value to any
of the previous items, with the exception of _N_ and _ERROR_.
Toby Dunn
From: John McQuown <John.McQuown@OTNNET.COM>
Reply-To: John McQuown <John.McQuown@OTNNET.COM>
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: SUSPECT: Re: Retain
Date: Wed, 2 Mar 2005 20:15:00 -0800
Strange that no one has mentioned that arrays, when initialized or
temporary, are automatically retained... A stealth RETAIN.
Cheers, john
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
toby dunn
Sent: Wednesday, March 02, 2005 7:35 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: SUSPECT: Re: Retain
Okay I have but my tongue long enough on where this thread is going:
Two things:
The retain statement does not, cannot, and will not (as long as the
underlying code is written currently) retain a thing. Nor will it
create a
counter or accumulator variable, nor will it create a lag variable. The
retain function will stop the SAS supervisor from resetting certain
variables stated in the retain statement from being reset to missing
with
each iteratoin of the data step. Now one can use the retain statement
with
other coding techniques or functions to create variables that
effectively
act like a accumulator, count, or lag variable.
The lag function doesn't even perform a true lag, it merely creates teh
illusion of a lag variable.
Why is this so important to get straight, well not knowing what the
retian
statement or lag function does has lead more programmers down the path
of
insanity and baldness than probrably any other topic in SAS other than
maybe
Ian's macro design or Pauls hash programs. once one knows how they do
what
they do, then one can use them to their advantage.
Toby Dunn
From: Frank Berger <frank.d.berger@DAL.FRB.ORG>
Reply-To: Frank Berger <frank.d.berger@DAL.FRB.ORG>
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: Retain
Date: Wed, 2 Mar 2005 16:41:31 -0600
"Grado" <grado2005@gamespy.com> wrote in message
news:FgqVd.1078$WC1.1055@tornado.fastwebnet.it...
>
> "Stat" <stat@upenn.edu> ha scritto nel messaggio
> news:d057sp$7l7j$1@netnews.upenn.edu...
> > Hi,
> >
> > What are your top 5 reasons for using the RETAIN function?
> >
> > - SH
> >
>
> 1) to count or cumulate something in a datastep
> 2) to arrange variables order in a dataset
>
> are you carrying out a market research on the topic?
>
>
3. To create a constant.
4. To create a lag variable without using the lag function. As in:
data temp2;
retain lx;
set temp1 (keep=x);
if _n_ ne 1 then
if x ne lx then output;
lx=x;
run;
|