|
|
楼主

楼主 |
发表于 2008-8-6 19:13:47
|
只看该作者
lag函数的运行机制
找了一些资料,发现lag函数的运行机制大概是这样:
1.lag(x)是一个队列(queue)函数,每次执行,从队列的起始读取数值并将该值移除出队列,队列中的剩余值上移,x的当前值进入队列末尾。因此首次执行后,lag(x)值为missing,队列中只有1个值--即x的当前值。
2.只有当lag(x)被执行后,x的当前值才会被读入队列中。因此,如果使用条件语句,不符合条件时,lag(x)不被执行,当前的x值也不会进入队列,从而不会出现在lag(x)的结果中。
3.SAS语句内的lag函数是无关的,即使执行的是同样的lag(x)。
<!-- m --><a class="postlink" href="http://support.sas.com/documentation/cdl/en/lrdict/59540/HTML/default/a000212547.htm">http://support.sas.com/documentation/cd ... 212547.htm</a><!-- m -->
<!-- m --><a class="postlink" href="http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/etsug_tsdata_sect048.htm">http://support.sas.com/documentation/cd ... ect048.htm</a><!-- m -->
<!-- m --><a class="postlink" href="http://support.sas.com/forums/thread.jspa?messageID=11673&#11673">http://support.sas.com/forums/thread.js ... 673&#11673</a><!-- m -->
Example:
[code:9r9ctlh0]
data test ;
input x @@;
y1=lag(x);
y2=1;
y3+1;
if y1>0 then z1=y1;
if y1>0 then z2=lag(x);
if y2>0 then z3=lag(x);
if lag(x)>0 then z4=y1;
if lag(x)>0 then z5=lag(x);
if y3 in(1,2,5,6) then z6=lag(x);
datalines;
1 2 3 4 5 6
;
run;
输出结果:
Obs x y1 y2 y3 z1 z2 z3 z4 z5 z6
1 1 . 1 1 . . . . . .
2 2 1 1 2 1 . 1 1 . 1
3 3 2 1 3 2 2 2 2 2 .
4 4 3 1 4 3 3 3 3 3 .
5 5 4 1 5 4 4 4 4 4 2
6 6 5 1 6 5 5 5 5 5 5
[/code:9r9ctlh0] |
|