全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
5374 8
2013-04-18
data test;
input id x;
cards;
1 10
2 11
3 12
1 19
2 13
3 15
1 20
2 23
3 25
;
proc sort data=test; by id x;
run;
data result;
set test;
by id;
if first.id then a=x;
else a=lag(x);
run;

以上這個程序..成生的結果..每組的第二個觀察直都是lag了兩個period...一直不知道為什麼...百思不得其解..
idxa

1

10

10

1

19

 

1

20

19

2

11

11

2

13

20

2

23

13

3

12

12

3

15

23

3

25

15




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-4-18 01:13:20
208   data result;
209   set test;
210   by id;
211   put 'before_______'   _all_;
212   if first.id then a=x;
213   else a=lag(x) ;
214   put 'after_______'   _all_;
215   run;

before_______id=1 x=10 FIRST.id=1 LAST.id=0 a=. _ERROR_=0 _N_=1
after_______id=1 x=10 FIRST.id=1 LAST.id=0 a=10 _ERROR_=0 _N_=1
before_______id=1 x=19 FIRST.id=0 LAST.id=0 a=. _ERROR_=0 _N_=2
after_______id=1 x=19 FIRST.id=0 LAST.id=0 a=. _ERROR_=0 _N_=2
before_______id=1 x=20 FIRST.id=0 LAST.id=1 a=. _ERROR_=0 _N_=3
after_______id=1 x=20 FIRST.id=0 LAST.id=1 a=19 _ERROR_=0 _N_=3
before_______id=2 x=11 FIRST.id=1 LAST.id=0 a=. _ERROR_=0 _N_=4
after_______id=2 x=11 FIRST.id=1 LAST.id=0 a=11 _ERROR_=0 _N_=4
before_______id=2 x=13 FIRST.id=0 LAST.id=0 a=. _ERROR_=0 _N_=5
after_______id=2 x=13 FIRST.id=0 LAST.id=0 a=20 _ERROR_=0 _N_=5
before_______id=2 x=23 FIRST.id=0 LAST.id=1 a=. _ERROR_=0 _N_=6
after_______id=2 x=23 FIRST.id=0 LAST.id=1 a=13 _ERROR_=0 _N_=6
before_______id=3 x=12 FIRST.id=1 LAST.id=0 a=. _ERROR_=0 _N_=7
after_______id=3 x=12 FIRST.id=1 LAST.id=0 a=12 _ERROR_=0 _N_=7
before_______id=3 x=15 FIRST.id=0 LAST.id=0 a=. _ERROR_=0 _N_=8
after_______id=3 x=15 FIRST.id=0 LAST.id=0 a=23 _ERROR_=0 _N_=8
before_______id=3 x=25 FIRST.id=0 LAST.id=1 a=. _ERROR_=0 _N_=9
after_______id=3 x=25 FIRST.id=0 LAST.id=1 a=15 _ERROR_=0 _N_=9
NOTE: There were 9 observations read from the data set WORK.TEST.
NOTE: The data set WORK.RESULT has 9 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-4-18 01:35:12
Quote from SAS online documentation: "Note:   Storing values at the bottom of the queue and returning values from the top of the queue occurs only when the function is executed. An occurrence of the LAGn function that is executed conditionally will store and return values only from the observations for which the condition is satisfied"
我对这句话的理解是满足lag执行条件的observations才会排入队列。
因此对于楼主的case,排入队列的x只有
19
20
13
23
15
25
那些不是first.id的。
lag执行的时候也只在该队列lag
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-4-18 06:54:27
lag函数是个很危险的函数,因为稍不注意就会有奇怪的事情发生。楼主遇到的问题楼上已经说了。我的经验是:永远不要把lag函数放到if...else...这类的句子里。

比较稳妥的方法是 var_lag = lag(x); 然后把var_lag用到 if... then...里面。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-4-18 11:07:20
LAG函数的作用如下:
The LAG function returns the value of its argument the last time the function executed.
如果 if 语句的条件成立,在那一次iteration的时候 lag函数就不会执行,所以下一次lag函数执行的时候会返回上上次iteration时x的值。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-4-19 07:41:22
The lag function is implement by a stack. It is an executable statement. In your case, it is only executed when 'if condition' is true. Hope this solves your puzzle.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群