Chapter Contents |
Previous |
Next |
RETAIN |
Valid: | in a DATA step |
Category: | Information |
Type: | Declarative |
Syntax |
RETAIN <element-list(s)
<initial-value(s) |
(initial-value-1) | (initial-value-list-1) > < . . . element-list-n <initial-value-n | (initial-value-n ) | (initial-value-list-n)>>>; |
Without Arguments |
If you do not specify an argument, the RETAIN statement causes the values of all variables that are created with INPUT or assignment statements to be retained from one iteration of the DATA step to the next.
Arguments |
Tip: | If you specify _ALL_, _CHAR_, or _NUMERIC_, only the variables that are defined before the RETAIN statement are affected. |
Tip: | If a variable name is specified only in the RETAIN statement and you do not specify an initial value, the variable is not written to the data set, and a note stating that the variable is uninitialized is written to the SAS log. If you specify an initial value, the variable is written to the data set. |
Tip: | If you omit initial-value, the initial value is missing. Initial-value is assigned to all the elements that precede it in the list. All members of a variable list, therefore, are given the same initial value. |
See Also: | (initial-value) and (initial-value-list) |
Element values are enclosed in quotation marks. To specify one or more initial values directly, use the following format:
(initial-value(s))
To specify an iteration factor and nested sublists for the initial values, use the following format:
<constant-iter-value*> <(>constant value | constant-sublist<)>
Restriction: | If you specify both an initial-value-list and an element-list, then element-list must be listed before initial-value-list in the RETAIN statement. |
Tip: | You can separate initial values by blank spaces or commas. |
Tip: | You can assign initial values to both variables and temporary data elements. |
Tip: | If there are more variables than initial values, the remaining variables are assigned an initial value of missing and SAS issues a warning message. |
Details |
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.
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.
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:
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_.
Comparisons |
Examples |
retain month1-month5;
retain month1-month5 1 year 0 a b c 'XYZ';
The values of MONTH1 through MONTH5 are set initially to 1; YEAR is set to 0; variables A, B, and C are each set to the character value
XYZ
.
retain month1-month5 (1);
Variables MONTH2 through MONTH5 are set to missing initially.
retain _all_;
retain var1-var4 (1 2 3 4);
retain var1-var4 (1,2,3,4);
This example shows how to use variable names and array names as elements in the RETAIN statement and shows assignment of initial values with and without parentheses:
data _null_; array City{3} $ City1-City3; array cp{3} Citypop1-Citypop3; retain Year Taxyear 1999 City ' ' cp (10000,50000,100000); file file-specification print; put 'Values at beginning of DATA step:' / @3 _all_ /; input Gain; do i=1 to 3; cp{i}=cp{i}+Gain; end; put 'Values after adding Gain to city populations:' / @3 _all_; datalines; 5000 10000 ;
The initial values assigned by RETAIN are as follows:
Here are the lines written by the PUT statements:
Values at beginning of DATA step: City1= City2= City3= Citypop1=10000 Citypop2=50000 Citypop3=100000 Year=1999 Taxyear=1999 Gain=. i=. _ERROR_=0 _N_=1 Values after adding GAIN to city populations: City1= City2= City3= Citypop1=15000 Citypop2=55000 Citypop3=105000 Year=1999 Taxyear=1999 Gain=5000 i=4 _ERROR_=0 _N_=1 Values at beginning of DATA step: City1= City2= City3= Citypop1=15000 Citypop2=55000 Citypop3=105000 Year=1999 Taxyear=1999 Gain=. i=. _ERROR_=0 _N_=2 Values after adding GAIN to city populations: City1= City2= City3= Citypop1=25000 Citypop2=65000 Citypop3=115000 Year=1999 Taxyear=1999 Gain=10000 i=4 _ERROR_=0 _N_=2 Values at beginning of DATA step: City1= City2= City3= Citypop1=25000 Citypop2=65000 Citypop3=115000 Year=1999 Taxyear=1999 Gain=. i=. _ERROR_=0 _N_=3
The first PUT statement is executed three times, while
the second PUT statement is executed only twice. The DATA step ceases execution
when the INPUT statement executes for the third time and reaches the end of
the file.
In this example, the data set ALLSCORES contains several observations for each identification number and variable ID. Different observations for a particular ID value may have different values of the variable GRADE. This example creates a new data set, CLASS.BESTSCORES, which contains one observation for each ID value. The observation must have the highest GRADE value of all observations for that ID in BESTSCORES.
libname class 'SAS-data-library'; proc sort data=class.allscores; by id; run; data class.bestscores; drop grade; set class.allscores; by id; /* Prevents HIGHEST from being reset*/ /* to missing for each iteration. */ retain highest; /* Sets HIGHEST to missing for each */ /* different ID value. */ if first.id then highest=.; /* Compares HIGHEST to GRADE in */ /* current iteration and resets */ /* value if GRADE is higher. */ highest=max(highest,grade); if last.id then output; run;
See Also |
Statements:
|
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.