Let's make the code a bit simpler.
/* test data sets */
data table1;
input id :$8. @@;
cards;
1 2 3
;
run;
data table2;
input id :$8. @@;
cards;
4 5
;
run;
data a;
set table1 table2(rename=(id=id2));
if missing(id) then id = "000" || id2;
run;
proc print; run;
/* on lst
Obs id id2
1 1
2 2
3 3
4 0004 4
5 0004 5 <- id is not reset here
*/
The question is: why the id for the fifth obs "0004", not "0005"?
This is because when the SET statement is reading observations from TABLE2, it does not reset ID variable to missing (because it is not in the TABLE2 due to RENAME) after the initial pdv resetting as the SET starts handling TABLE2. Thus once the ID is assigned "0004", it remains "0004". Since "0004" is not missing, the IF condition evaluates to FALSE and no further assignments are done.
For more detailed explanation on how data step works, see Howard Schreier's excellent SUGI30 paper titled "Let Your Data Power Your DATA Step: Making Effective
Use of the SET, MERGE, UPDATE, and MODIFY Statements".
... View more