Creating a Subject Number in the DATA Step Using RETAIN - SAS Programming

To correct the problem in , you can use a RETAIN statement to initialize the value of SUBJECT to a non-missing value (0 for the first observation), and then bring forward the previous value of SUBJECT to each new observation being added to the data set you are building.

You then increment the value of SUBJECT by 1 for each observation. If you place the following line anywhere in the DATA step, the program executes as desired and produces the results shown in Output 2:

RETAIN SUBJECT 0;

Example

DATA BETTER;
RBtAIN SUBJECT 0;
SUBJECT*SUBJECT + 1?
INPUT SCOR11 SCORE2?
DAfAI«IN£S|
3 4
5 6
7 8
t
I»ROC PRINT DATA*8ITfER;
TITLE1 'Correct Program'j
RUN;

Output from Example - Creating a Subject Number in the DATA Step Using RETAIN

Output from Example  - Creating a Subject Number in the DATA Step Using RETAIN

You may be asking yourself why you went through the trouble of producing the variable SUBJECT when the system provides OBS in the PROC PRINT output, and this is exactly the value you worked so hard to obtain.

The answer is that OBS is a display variable only.It is not included in the data set; it is only present in the PROC PRINT output.By using the method shown here, you have the value of SUBJECT as part of the data set itself. Of course, if the OBS number on the output is all you'll ever need, by all means use what the system gives you and don't create extra work for yourself.


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

SAS Programming Topics