@cosmid wrote:
I am still unclear about question 1. The code used in the book is also a SET statement, why didn't that retain the value of the variables?
I don't have the book, but variables read by a SET statement are automatically retained until their values are overwritten by the next execution of the SET statement (or any other statement changing those values).
Here is an example (log) using a PUT statement to show variable values in each iteration of a DATA step, before assignment statements and a SET statement are executed:
118 data _null_;
119 put _all_;
120 retvar='Ret';
121 newvar=123;
122 set sashelp.class(obs=3);
123 retain retvar;
124 run;
retvar= newvar=. Name= Sex= Age=. Height=. Weight=. _ERROR_=0 _N_=1
retvar=Ret newvar=. Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 _ERROR_=0 _N_=2
retvar=Ret newvar=. Name=Alice Sex=F Age=13 Height=56.5 Weight=84 _ERROR_=0 _N_=3
retvar=Ret newvar=. Name=Barbara Sex=F Age=13 Height=65.3 Weight=98 _ERROR_=0 _N_=4
As you can see, in the second, third and fourth iteration of the DATA step (_N_=2, 3, 4) the variables from SASHELP.CLASS still contain the values read by the SET statement in the previous iteration of the DATA step. Similarly, starting with the second iteration, the value of the explicitly retained variable RETVAR is available before the assignment statement refreshing it. Variable NEWVAR, however, is not retained, hence reset to missing when the DATA step iterates and these newly created missing values are written to the log for _N_=2, 3, 4. Initially (_N_=1), the values of all variables (except the automatic variables _ERROR_ and _N_) were missing.
... View more