SAS中文论坛

标题: 一个retain和lagged variable问题 [打印本页]

作者: shiyiming    时间: 2004-6-21 10:38
标题: 一个retain和lagged variable问题
data 如下:
ID AGE A
001 1 0.05
001 2 0.06
001 3 0.10
002 1 0.06
002 2 0.03
002 3 0.07

现在需要一个新变量 B if first.id then b=1-a; else b=lag(b)*(1-a);
即:

ID AGE A B
001 1 0.05 0.95
001 2 0.06 0.95(1-0.06)
001 3 0.10 0.95 (1-0.06)*(1-0.10)
002 1 0.06 0.94
002 1 0.03 0.94*(1-0.03)
002 2 0.07 0.94*(1-0.03)*1-0.07)


我的程序如下:
data test1;
input ID AGE A;
cards;
001 1 0.05
001 2 0.06
001 3 0.10
002 1 0.06
002 2 0.03
002 3 0.07
;
run;
data test2;
set test1;
by id;
retain b;
if first.id then b=1-a;
b=lag(b)(1-a);
if first.id then b=1-a; *otherwise b missing
run;

Could anybody pls tell me what is wrong with my code? It seems no problem for the above sample, but I checked the result for a large sample and b is not decreasing monotonicaly. For example, for the 340th obs of the first id, d is 0.45 and for the 339th obs, d is 0.47.
作者: shiyiming    时间: 2004-6-21 14:15
标题: sas
你把程序改为;
[code:3d0d6]data test1;
input ID AGE A;
cards;
001 1 0.05
001 2 0.06
001 3 0.10
002 1 0.06
002 2 0.03
002 3 0.07
;
run;
data test2;
length b 8.;
set test1;
retain b;
by id;
if first.id then b=1-a;
else b=b*(1-a);
run; [/code:3d0d6]
retain已经起到了lag的作用
作者: shiyiming    时间: 2004-6-21 17:50
标题: 询问
data test1;
input ID AGE A;
cards;
001 1 0.05
001 2 0.06
001 3 0.10
002 1 0.06
002 2 0.03
002 3 0.07
;
run;
data test2;
set test1;
by id;
retain b;
[color=red:80920]if first.id then b=(1-a)*(1-a);
else b=.;[/color:80920]run;
proc print;
run;
不知是否是这个意思,满足条件,实际是执行B=()^2;不满足赋缺失值
作者: shiyiming    时间: 2004-6-22 07:44
标题: Re: Thanks
Thanks a lot to both of you. Now I understand lag is really unnecessary. You help is greatly appreciated.




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2