In reply to Lianbo.

Hi Lianbo,
Thank you for your comment. What you experience here is an automatic, implicit RETAIN which is perfectly OK and in line with expectations. Let me explain why. Even though you dropped the HEIGHT variable when created dataset CLASS, this variable is NOT NEW in the last data step (data test;). It is not new because when you apply data template using "if 0 then set template;" it comes to the data step from that dataset TEMPLATE and therefore automatically retained. As explained in READY, SET, RETAIN, AND THEN MAYBE RESET SGF paper, see section CASES IN WHICH A VARIABLE IS AUTOMATICALLY RETAINED at the end of page 6: "there are several types of variables that are automatically retained. These include temporary array variables (variables from arrays named _TEMPORARY_), automatic variables (e.g., _N_, _ERROR_) and SAS data set variables (variables read from a SAS data set using SET, MERGE, UPDATE, and MODIFY). It would be redundant to use the RETAIN statement in these cases where variables are automatically retained or in other words are not initialized before each new observation is read."

If you do not want this HEIGHT variable to retain its values, you would need to explicitly reset it to missing, e.g.:

data test;
if 0 then set template;
set class;
if sex='M' then height=2; else height=.;
run;

or

data test;
if 0 then set template;
set class;
height = ifn(sex='M',2,.);
run;

Another option would be to drop this variable from the TEMPLATE dataset; in that case this variable will indeed be NEW in the last data step and therefore not retained automatically by default.

Hope this helps.
Best regards,
Leonid