I ran your sample code and you omitted important information: this message was in the SAS log: "NOTE: DATA STATION was stopped due to cyclization." As a result of this post, I provided a SAS document describing the error . He suggested not using the IF statement before the SET statement, but using the OBS = dataset parameter to limit the number of observations read.
So you should change the line:
if _N_ = 1 then set BEFORE;
at
set BEFORE(obs=1);
, "Before IF:" , , . , , .