楼主: vieri32
5444 8

SAS Lag function的問題 [推广有奖]

  • 1关注
  • 0粉丝

硕士生

22%

还不是VIP/贵宾

-

威望
0
论坛币
7 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
1062 点
帖子
88
精华
0
在线时间
110 小时
注册时间
2009-12-6
最后登录
2021-7-12

楼主
vieri32 发表于 2013-4-18 00:52:17 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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




二维码

扫码加我 拉你入群

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

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

关键词:function CTI Fun lag UNC function

沙发
rdwalk 发表于 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

藤椅
yuerqieqie 发表于 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
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
pobel + 2 + 2 + 2 精彩帖子

总评分: 学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

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

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

报纸
matchlessboy 发表于 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的值。

地板
bobguy 发表于 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.

7
clatyz 发表于 2013-4-19 11:28:28
Hi vieri32, I encountered the same problem months ago, and the solution is very simple:
just reverse the order of your codes, like:

proc sort data=test; by id x; run;
data result;
       set test;
       by id;
       a=lag(x);
       if first.id then a=.;
run;

Try if this works.

Best,

8
hamsik11 发表于 2013-4-19 12:26:09
如果有if语句或者循环 慎用lag函数 会出现不少问题

9
hellofuture 发表于 2013-4-19 19:52:34
我今天也遇到了类似的问题,解决的办法是,a=lag(某变量),然后下边用a来代替。你可以试下~

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-21 15:46