| 
 | 
楼主
 
 
 楼主 |
发表于 2004-6-21 10:38:41
|
只看该作者
 
 
 
一个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. |   
 
 
 
 |