标题: lag 函数求教 [打印本页] 作者: shiyiming 时间: 2003-10-30 22:50 标题: lag 函数求教 数据集:myset
Obs STKCD ACCPER
1 000003 19911231
2 000003 19921231
3 000003 19981231
4 000003 19991231
5 000003 20001231
6 000003 20011231
7 000004 19921231
8 000004 19931231
9 000004 19941231
10 000004 19951231
11 000004 19961231
12 000004 19971231
data myset1;
set myset;
if stkcd=lag(stkcd) and accper-lag(accper)=10000 then do;
stkcd1=lag(stkcd);
accper1=lag(accper);
end;
run;
结果为
Obs STKCD ACCPER stkcd1 accper1
1 000003 19911231 .
2 000003 19921231 .
3 000003 19981231 .
4 000003 19991231 000003 19921231
5 000003 20001231 000003 19991231
6 000003 20011231 000003 20001231
7 000004 19921231 .
8 000004 19931231 000003 20011231
9 000004 19941231 000004 19931231
10 000004 19951231 000004 19941231
11 000004 19961231 000004 19951231
12 000004 19971231 000004 19961231
为什么第4个观测和第8个观测的stkcd1、accper1不是上一个观测的stkcd和accper值呢?作者: shiyiming 时间: 2003-10-31 00:48
Because the LAG function stores values on the queue only when it is called, [b:bc182]you must call LAG unconditionally to get the correct answers[/b:bc182].
Example: Storing Every Other Lagged Value This example shows the difference in output when you use conditional and unconditional logic in your program.
options pagesize=25 linesize=64 nodate pageno=1;
title 'Store Every Other Lagged Value';
data test;
input x @@;
if mod(x,2)=0 then a=lag(x);
b=lag(x);
if mod(x,2)=0 then c=b;
label a='(WRONG) a' c='(RIGHT) c';
datalines;
1 2 3 4 5 6 7 8
;
proc print label data=test;
run;