需要实现的场景,cd48指标连续两次测量结果为阳性才诊断为阳性,且在基线检查时不能为阳性
*step1:cd48=1表示第一次的值小于1;
data temp;
set temp;
by pid;
if first.pid=1 and .<cd48<1 then
cd48x=1;
run;
proc sort;
by pid cd4_date;
run;
*step2 产生两次检查的时间差
data temp1;
set temp;
by pid;
*if first.pid^=1 then datedif=dif(cd4_date);
datedif=dif(cd4_date)/12;
format datedif 7.3;
if first.pid=1 then
datedif=.;
run;
*step3 对cd48g赋值;
data temp2;
set temp1;
by pid;
if (first.pid^=1 and cd48>=1) then
cd48g=1;
else if ( first.pid^=1 and .<cd48<1) then
cd48g=0;
run;
*使用lag函数进行累加,连续两次为阳性则累加值=2,及两次检查为阳性患者;
data temp3;
set temp2;
cd48g_lag=lag(cd48g);
if first.pid=1 then cd48g_lag=.;
run;
data temp4;
set temp3;
sum=cd48g+cd48g_lag;
run;
*最后再需要对 阳性和阴性人群分别生成数据库,去重、排序,设定随访终点(阳性者为最早出现阳性的时间,阴性为最后一次随访检测时间)
Done!