if _N_=1 then SET SmallTable;
Obs Name score Tool
1 Jack 10 screwdriver
2 Mary 11 screwdriver
However, I don't understand why the value of "tool" is still "screwdriver" for the second observation. Indeed, when the data step comes to the second record of BigTable in the first set statement, _N_ has been incremented, its value is 2 and then the if..then statement returns false; that is, the "SET SmallTable" statement should NOT be executed !
However, it is...
Well, I don't want to accomplish anything, I just want to understand how SAS works.
I agree my post wasn't clear, but I don't know how to express it better.
Basicaly, I don't understand how the "if _N_=1 then SET" works because it seems to me that the data step iterates 2 times and then the SET statement shouldn't proceed anymore because _N_=2.
Message was edited by: babaorumi
The first set BigTable statement causes the MyTable data step to loop twice through the two observations in BigTable.
the _N_ value represents the current observation to be output to MyTable.
The if condition _N_ = 1 then set SmallTable causes SAS to read a value from SmallTable. SAS now assumes that you want to retain the value of "tool" in the next iteration because you used a "Set" statement to get the value for tool. It is not re-initializing the value to missing. So, it is working like a merge, sort of.
If you had if _N_ = 1 then tool = "screwdriver"; then SAS would re-initialize the "tool" variable back to missing.
If you were to use the code "Retain tool" then the value of tool would be retained in each iteration.
Ok I understood what's going on. It's a result of the set statement which retains the variables in the PDV and the "if" subsetting statement which immediately returns at the beginning of the data step when it evaluates the condition as false.