当DATA步里用BY语句时,就会对每一个BY语句中的变量产生一个 first.var 和一个last.var。
当某一条观测是该BY-变量某个组(值相同)的第一条时,first.var的值是1,否则为0;
当某条观测是该BY-变量某个组的最后一条时,则last.var的值是1,否则为0.
if last.id; 相当于 if last.id=1; 所以只会输出每个组的最后一条。
288 data temp;
289 merge a b;
290 by id;
291 put _all_;
292 if last.id;
293 run;
id=1 score=100 name=nagato FIRST.id=1 LAST.id=0 _ERROR_=0 _N_=1
id=1 score=200 name=nagato FIRST.id=0 LAST.id=0 _ERROR_=0 _N_=2
id=1 score=300 name=nagato FIRST.id=0 LAST.id=1 _ERROR_=0 _N_=3
id=2 score=100 name=sakura FIRST.id=1 LAST.id=0 _ERROR_=0 _N_=4
id=2 score=200 name=sakura FIRST.id=0 LAST.id=0 _ERROR_=0 _N_=5
id=2 score=300 name=sakura FIRST.id=0 LAST.id=1 _ERROR_=0 _N_=6
NOTE: There were 6 observations read from the data set WORK.A.
NOTE: There were 2 observations read from the data set WORK.B.
NOTE: The data set WORK.TEMP has 2 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.09 seconds
cpu time 0.03 seconds
|