我的示例代码如下:
ID DX S_DT
1 VV 12/20/2013
1 VV 12/21/2013
2 VV 3/11/2006
2 VV 6/20/2008
2 VV 6/4/2009
2 VV 6/5/2009
3 VV 11/12/2001
3 VV 11/13/2001
3 VV 1/1/2014
我希望我的最终数据看起来像这样,除非将粗体日期设置为零:
ID d_dt1 d_dt2 d_dt3 d_dt4
1 12/20/2013 **12/21/2013**
2 3/11/2006 6/20/2008 **6/4/2009** **6/5/2009**
3 11/12/2001 **11/13/2001** 1/1/2014
这是我的代码:
data data_out;
SET data_in;
by ID S_dt;
retain d num_d prev_d_dt d_dt1-d_dt4;
array d_dt(4) d_dt1-d_dt4;
array dur(3) dur1-dur3;
if first.ID then do;
d=0;
num_d=0;
prev_d_dt=.;
do i = 1 to 4;
d_dt(i)=.;
end;
end;
if (DX='VV' and prev_d_dt ne S_dt ) then do;
d=1;
num_d+1;
prev_d_dt=S_dt;
d_dt(num_D)=S_dt;
end;
end;
end;
if (last.ID) then do;
current_ddt=.;
do i = 1 to 3;
dur(i)=d_dt(i+1) - d_dt(i);
if ( dur(i) >= 270) then do;
current_ddt=d_dt(i+1);
dur(i) = d_dt(i+1) - d_dt(i);
end;
if (dur(i) < 270) then do;
d_dt(i+1)=.;
current_ddt=d_dt(i+2);
dur(i+1)=d_dt(i+2) - d_dt(i);
end;
end;
output;
end;
keep patid num_d d_dt1-d_dt4 dur1-dur3 current_ddt;
format d_dt1-d_dt4 current_ddt mmddyy10.;
run;
我无法将这些不需要的日期设置为零。
答案 0 :(得分:0)
一种方法是转置然后使用数组进行计算。
data have;
input ID DX $ S_DT:mmddyy10.;
format s_dt mmddyy10.;
datalines;
1 VV 12/20/2013
1 VV 12/21/2013
2 VV 3/11/2006
2 VV 6/20/2008
2 VV 6/4/2009
2 VV 6/5/2009
3 VV 11/12/2001
3 VV 11/13/2001
3 VV 1/1/2014
;
/* sorting the data*/
proc sort data = have;
by id s_dt;
run;
/* transposing the data*/
proc transpose data =have out =have1 (drop =_name_) prefix =date;
by id ;
var s_dt;
run;
/* picking all the date variables and counting number of variables
for array processing in next step*/
proc sql;
select name , count(name)-1 into :date SEPARATED by ' ',
:cnt
from dictionary.columns
where memname = 'HAVE1'
and upcase(name) like 'DATE%';
%put &date &cnt;
/* data processing*/
data have2;
set have1;
array date(*) &date;
dateno =date1;
format dateno mmddyy10.;
do i = 1 to &cnt;
if date(i) -date(i+1) = -1 then do ;
call missing(date(i));
call missing (date(i+1));
end;
end;
drop date1 i;
run;
/* putting variables in the order*/
data have_final;
retain id &date;
set have2(rename =dateno=date1);
run;