SAS做循环+滞后功能? [英] SAS do loop + lag function?

查看:18
本文介绍了SAS做循环+滞后功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,所以如果我不够清楚,请告诉我.这就是我想要做的 - 这是我的数据集.我的方法是一个有延迟的 do 循环,但结果是垃圾.

This is my first post, so please let me know if I'm not clear enough. Here's what I'm trying to do - this is my dataset. My approach for this is a do loop with a lag but the result is rubbish.

data a;
input @1 obs @4 mindate mmddyy10. @15 maxdate mmddyy10.;
format mindate maxdate date9.;
datalines;
1   01/02/2013 01/05/2013
2   01/02/2013 01/05/2013
3   01/02/2013 01/05/2013
4   01/03/2013 01/06/2013
5   02/02/2013 02/08/2013
6   02/02/2013 02/08/2013
7   02/02/2013 02/08/2013
8   03/10/2013 03/11/2013
9   04/02/2013 04/22/2013
10  04/10/2013 04/22/2013
11  05/04/2013 05/07/2013
12  06/10/2013 06/20/2013
;
run;

现在,我正在尝试根据以下逻辑生成一个新列 - 替换":

Now, I'm trying to produce a new column - "Replacement" based on the following logic:

  1. 如果记录的 Mindate 发生在其滞后的 maxdate 之前,则它不能替代它.如果不能替换,则向前跳过(所以 - 2,3,4 不能替换 1,但 5 可以).
  2. 否则...如果 mindate 少于 30 天,Replacement = Y.如果不是,replacement = N.一旦一条记录替换另一个记录(因此,在这种情况下,5 确实替换了 1,因为 02/02/2013比 01/05/2013 小于 30,它不能重复作为另一条记录的替换.但如果上面的一条记录是 N,它仍然可以是其他记录的 Y.因此,现在将 6 与 2 进行评估, 7 对 3 等.由于这两个组合都是Y",因此现在对 8 与 4 进行评估,但因为相对于 4 的最大日期,它的 Mindate >30,所以它是 N.但是,然后针对
  3. 等等……

我应该在 100 条记录的数据集中,这意味着第 100 条记录在技术上可以取代第 1 条,所以我一直在尝试循环中的滞后.非常感谢任何提示/帮助!预期输出:

I should that in a 100 record dataset, this would imply that the 100th record could technically replace the 1st, so I've been trying lags within loops. Any tips/help is greatly appreciated! Expected output:

                      obs      mindate      maxdate    Replacement

                        1    02JAN2013    05JAN2013
                        2    02JAN2013    05JAN2013
                        3    02JAN2013    05JAN2013
                        4    03JAN2013    06JAN2013
                        5    02FEB2013    08FEB2013         Y
                        6    02FEB2013    08FEB2013         Y
                        7    02FEB2013    08FEB2013         Y
                        8    10MAR2013    11MAR2013         Y
                        9    02APR2013    22APR2013         Y
                       10    10APR2013    22APR2013         N
                       11    04MAY2013    07MAY2013         Y
                       12    10JUN2013    20JUN2013         Y

推荐答案

如果提问者误认为replacement = Y for obs = 12,我认为这是正确的.

I think this is correct if the asker was mistaken about replacement = Y for obs = 12.

/*Get number of obs so we can build a temporary array to hold the dataset*/
data _null_;
    set have nobs= nobs;
    call symput("nobs",nobs);
    stop;
run;

data want;
    /*Load the dataset into a temporary array*/
    array dates[2,&NOBS] _temporary_;
    if _n_ = 1 then do _n_ = 1 by 1 until(eof);
        set have end = eof;
        dates[1,_n_] = maxdate;
        dates[2,_n_] = 0;
    end;

    set have;

    length replacement $1;

    replacement = 'N';
    do i = 1 to _n_ - 1 until(replacement = 'Y');
        if dates[2,i] = 0 and 0 <= mindate - dates[1,i] <= 30 then do;
            replacement = 'Y';
            dates[2,i] = _n_;
            replaces = i;
        end;
    end;
    drop i; 
run;

如果您愿意,可以使用散列对象 + 散列迭代器代替临时数组.我还包含了一个额外的 var,replaces,以显示每行替换的前一行.

You could use a hash object + hash iterator instead of a temporary array if you preferred. I've also included an extra var, replaces, to show which previous row each row replaces.

这篇关于SAS做循环+滞后功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆