首页 > 解决方案 > 如何计算SAS中连续值的最大数量

问题描述

我有一个数据集,每个患者有一行,它包含有关患者服用 11 剂药物的日期(格式为 SAS 日期)的信息。在数据集中,每天最多服用一剂药物。患者可以填写 1 到 11 剂之间的任何日期,并且没有缺少信息的中间剂量(例如,如果填写了 Dose5,则根据定义填写了 Dose1-Dose4)。我有兴趣获得患者服用一剂药物的最大连续天数。这里有 5 行样本数据。

data have;
    input PATIENT_ID Dose1 :ddmmyy10. Dose2 :ddmmyy10. Dose3 :ddmmyy10. Dose4 :ddmmyy10. Dose5 :ddmmyy10. Dose6 :ddmmyy10. Dose7 :ddmmyy10. Dose8 :ddmmyy10. Dose9 :ddmmyy10. Dose10 :ddmmyy10. Dose11;
    format Dose1 Dose2 Dose3 Dose4 Dose5 Dose6 Dose7 Dose8 Dose9 Dose10 Dose11 ddmmyy10.;
    cards;
          1          01/01/2020 01/02/2020 01/03/2020 01/04/2020 01/05/2020 01/06/2020 01/07/20  1/08/2020 01/09/2020 01/10/2020 01/11/2020
          2          01/01/2020 01/02/2020 01/03/2020 01/05/2020 01/06/2020
          3          01/02/2020 01/04/2020 01/06/2020 01/08/2020 01/10/2020 01/12/2020 01/14/2020 01/16/2020 01/18/2020
          4          01/03/2020 01/04/2020 01/05/2020 01/06/2020 01/07/2020 01/08/2020 01/09/2020  1/10/2020 01/12/2020 01/13/2020
          5          01/01/2020 01/07/2020 01/08/2020 01/10/2020
;
run;

我想获得变量 MAX_CONSECUTIVE_DAYS:

data want;
    input PATIENT_ID MAX_CONSECUTIVE_DAYS Dose1 :ddmmyy10. Dose2 :ddmmyy10. Dose3 :ddmmyy10. Dose4 :ddmmyy10. Dose5 :ddmmyy10. Dose6 :ddmmyy10. Dose7 :ddmmyy10. Dose8 :ddmmyy10. Dose9 :ddmmyy10. Dose10 :ddmmyy10. Dose11;
    format Dose1 Dose2 Dose3 Dose4 Dose5 Dose6 Dose7 Dose8 Dose9 Dose10 Dose11 ddmmyy10.;
    cards;
          1          11                  01/01/2020 01/02/2020 01/03/2020 01/04/2020 01/05/2020 01/06/2020 01/07/20  1/08/2020 01/09/2020 01/10/2020 01/11/2020
          2          3                   01/01/2020 01/02/2020 01/03/2020 01/05/2020 01/06/2020              
          3          1                   01/02/2020 01/04/2020 01/06/2020 01/08/2020 01/10/2020 01/12/2020 01/14/2020 01/16/2020 01/18/2020
          4          8                   01/03/2020 01/04/2020 01/05/2020 01/06/2020 01/07/2020 01/08/2020 01/09/2020  1/10/2020 01/12/2020 01/13/2020
          5          2                   01/01/2020 01/07/2020 01/08/2020 01/10/2020
run;

到目前为止,我只能通过蛮力零碎地弄清楚如何做到这一点。

data bruteforce;
    set have;
    if Dose2 =. then MAX_CONSECUTIVE_DAYS=1;
      else if Dose3=. then
      do;
        if Dose2-Dose1=1 then MAX_CONSECUTIVE_DAYS=2;
          else MAX_CONSECUTIVE_DAYS=1;
      end;
      else if Dose4=. then
      do;
        if Dose3-Dose1=2 then MAX_CONSECUTIVE_DAYS=3;
          else if (Dose2-Dose1=1) or (Dose3-Dose2=1) then MAX_CONSECUTIVE_DAYS=2;
          else MAX_CONSECUTIVE_DAYS=1;
      end;
      else if Dose5=. then
      do;
        if Dose4-Dose1=3 then MAX_CONSECUTIVE_DAYS=4;
          else if (Dose3-Dose1=2) or (Dose4-Dose2=2) then MAX_CONSECUTIVE_DAYS=3;
          else if (Dose2-Dose1=1) or (Dose3-Dose2=1) or (Dose4-Dose3=1) then MAX_CONSECUTIVE_DAYS=2;
          else MAX_CONSECUTIVE_DAYS=1;
      end;
     /*And so on and so forth until accounting for rows where Dose10 is populated*/
 run;

然而,在我的实际工作中,有超过 200 剂的药物,所以用 if-then-else 语句做一系列的 do 循环是没有意义的。如果我不得不猜测,解决方案可能与数组有关,但我不确定从哪里或如何开始。

标签: arrayscountsas

解决方案


首先,感谢您清楚地解释您的问题以及您迄今为止所做的尝试:-)

只是一个注释。我将您的输入数据更改为具有 mmddyy10 信息/格式的日期。我认为你想要的是计算连续的天数而不是数月。

无论如何,试试这个。随意问

data have;
infile datalines missover;
input PATIENT_ID (Dose1 - Dose11)(:mmddyy10.);
format Dose: mmddyy10.;
cards;
1 01/01/2020 01/02/2020 01/03/2020 01/04/2020 01/05/2020 01/06/2020 01/07/2020 1/08/2020 01/09/2020 01/10/2020 01/11/2020
2 01/01/2020 01/02/2020 01/03/2020 01/05/2020 01/06/2020
3 01/02/2020 01/04/2020 01/06/2020 01/08/2020 01/10/2020 01/12/2020 01/14/2020 01/16/2020 01/18/2020
4 01/03/2020 01/04/2020 01/05/2020 01/06/2020 01/07/2020 01/08/2020 01/09/2020 1/10/2020 01/12/2020 01/13/2020
5 01/01/2020 01/07/2020 01/08/2020 01/10/2020
;

data want(drop=c i);
   set have;
   array dose {*} Dose:;
   c = 1;
   do i = 2 to dim(dose);
      if dose[i] - dose[i-1] = 1 then c + 1;
      else do;
         if c > mc then mc = c;
         c = 1;
      end;
   end;
   if mc = . then mc = c;
run;

结果:

PATIENT_ID Dose1...Dose11 mc 
1 ... 11 
2 ... 3 
3 ... 1 
4 ... 8 
5 ... 2 

推荐阅读