在SAS中循环带来最新价值

时间:2017-08-02 09:26:51

标签: sas

我正在尝试找到与给定参考天数匹配的天数,或者查找接近参考天数的天数。

我编码到这里,但不知道如何前进。

ID Date ref_days lags total_days
1  2017-02-02 224 . 0
1  2017-02-02 224 84 84
1  2017-02-02 224 84 168
2  2015-01-21 213 300 388 
3  2016-02-12 560 95 .
3  2016-02-12 560 86 181
3  2016-02-12 560 82 263
3  2016-02-12 560 69 332
3  2016-02-12 560 77 409

所以现在我想提出接近参考日的最后一个值。 并且下一个total_days应该再次从ZERO开始以找到下一个窗口。我怎样才能做到这一点?

这是我写的代码

data want; 
do until (totaldays <= ref_days); 
set have; 

by ID ref_days notsorted;

    if first.id then totaldays=0; 
    else totaldays+lags; 
    end; 
run;

必需输出:

ID Date ref_days lags total_days
1  2017-02-02 224 . 0
1  2017-02-02 224 84 84
1  2017-02-02 224 84 168
2  2015-01-21 213 300 388 
3  2016-02-12 560 95 .
3  2016-02-12 300 86 181
3  2016-02-12 300 82 263
3  2016-02-12 300 69 .
3  2016-02-12 300 77 146

1 个答案:

答案 0 :(得分:0)

前段时间我通过Proc sql做了类似的事情。它计算所有距离并采用最接近的距离。它适用于中等大小的数据集。希望它有一些用处。

proc sql;
    select * from 
    (
        select *, 
        abs(t1.link-t2.link) as dist /*In your case these would be dateVars*/
        from test1 t1
        left join test2 t2 
        on 1=1) group by system1 having dist=min(dist); 
;
quit;

有人说,1 = 1的左连接有点傻(因为完全的外连接就足够了,或者其他什么。)但是这对于有问题的问题很有用。

相关问题