【发布时间】:2021-02-14 20:00:24
【问题描述】:
我有一个数据集,每个患者有一行,它包含有关患者服用 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 循环是没有意义的。如果我不得不猜测,解决方案可能与数组有关,但我不确定从哪里或如何开始。
【问题讨论】: