又是一个根据随访时间生成一人多条记录的问题:
临床问题:患者在疾病诊断后开具了一些药品。以180天为界限,生成每个时间段是否使用了某药物。
image.png
image.png
3.数据整理的逻辑如下:
1)先根据疾病诊断时间date1,开药时间pres和随访截止时间end生成时间(long)变量,并基于此生成以180天为一个时间间隔的随访时间段变量pgr,和最长的时间段变量pgr_max,以及根据prest利用函数find确定是否使用某药物。
data new;
set raw;
pdays=prest-date1;
phrase=pdays/180;
long=(end-date1)/180;
*开药所处的随访时间段;
if int(phrase)<=phrase then pgr=int(phrase)+1;
else pgr= int(phrase);
*最大随访时间段;
if int(long)<=long then pgr_max=int(long)+1;
else pgr_max= int(long);
*是否使用药物;
if find(pres,"vitamin c")>0 then vc=1;else vc=0;
run;
*求和是否用药;
proc means data=new sum max noprint;
var vc ca vb;
by id pgr;
output out=r1 sum=/ autoname;;
run;
data result;
set r1;
if vc_sum>=1 then vc=1;else vc=0;
time1=180*pgr-180;
time2=180*pgr-1;
drop _TYPE_ _FREQ_ vc_sum;
run;
2)生成最大的随访时间阶段,并建立单独的数据库
data r2;
set new;
do i=1 to pgr_max;
output;
end;
keep id pgr_max i ldays;
run;
proc sort nodup;
by id i;
run;
3)合并上生成称的两个库以及每个时间段的起止天数
proc sql;
create table result1 as
select a.*, r2.id as newid,i,pgr_max,ldays from
result as a full join r2
on a.id=r2.id and pgr=i;
quit;
data result2;
set result1;
if id=. then id=newid;
if time2=. & i^=. then time2=i*180-1;
if time1=. & i^=. then time1=i*180-180;
if time2>ldays then time2=ldays;
if pgr=. then do;
vc=0;end;
drop pgr newid pgr_max i ldays;
run;
完成~~~~