@python_user wrote:
SAS (as you point out) does not complete the merge until the 'run' line, and over writes the merged values affected by if for all subsequent rows until the merge value changes.
Actually, the "overwriting" takes place only in the observation where the conditional assignment statement is executed. In subsequent observations of the same BY group the value is just retained (i.e. repeated). Overwriting is also what makes your final DATA step (using the SET statement) work.
The key fact is that
"The variables read using the MERGE statement are retained in the PDV." (documentation of the MERGE statement)
In the DATA step creating dataset DF3 the MERGE statement reads var3=10 from dataset DF2 while processing the first observation of the BY group var1='A'. Dataset DF3, having only one observation with var1='A', does not contribute a new value of var3 for the second observation of that BY group. Due to the implied RETAIN, the value of var3 from the previous observation is repeated. While this is the desired behavior in the absence of the IF-THEN statement, the repetition of value 30 introduced by the conditional assignment statement seems to contradict the IF condition. But the issue is that the original value 10 has been overwritten by the assignment statement var3=30 and hence is no longer available. (Analogous behavior in the second BY group.)
This is even similar in the DATA step updating dataset DF4. The SET statement also implies a RETAIN for var1, var2 and var3, but this time the retained value var3=30 from the first observation with var1='A' is overwritten by the value var3=10 read from the second observation with var1='A' in dataset DF4.
... View more