Explicit versus Implicit Retaining of Values - SAS Programming

The previous example adequately demonstrates the explicit use of RETAIN. You can achieve the same result by using the following sum statement:

SUBJECT+1;

This replaces the following lines from the original program:

RETAIN SUBJECT 0; SUBJECT=SUBJECT+1;

The sum statement (not to be confused with the SUM function discussed in the next example) is one of those statements in the SAS System that you discover after you've been writing code fora while, and you sit back and say, "Hmmm. I wonder what other goodies there are that I don't know about." Here is the alternate code using the sum statement:

Example

DATA BEST;
SUBJECT+1;
INPUT SCORE1 SCORE2;
DATALINES;
3 4
5 6
7 8
/
PROC PRINT DATA=SEST;
TITLE1 'Correct Program';
RUN;

Notice there is no equal (=) sign in the SUBJECT+1; statement.This sum statement does a few things: It automatically initializes SUBJECT to 0 for the first observation, and then it implicitly retains the previous value for each new observation before performing its function (in this case, adding 1 to the retained value). The output from this program is identical .


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics