我有一个SAS脚本,我试图在名为shop_dttm的列中查找连续日期的所有范围。此外,所有其他数据列需要相同才能将两个观察值计算为连续日期。但是,目前,我的程序只给出了成对的连续日期,而不是长度大于2天的连续日期范围。
代码:
%let indata = WORK.OFFSELL_ZE;
%let location = city_cd;
%let class = shop_car_type_cd;
%let length = lor;
%let shop_dttm = datepart(shop_dttm);
%let chkdate = shop_dttm;
%let numconday = 0;
%let outdata = WORK.shop_date_range_ZE;
proc sort data = &indata nodupkey;
by &location &class &length &chkdate;
run;
data = &indata;
data one;
set &indata;
by &location &class &length &chkdate;
tempdate = lag(&chkdate);
if (first.&location & first.&class & first.&length) then tempdate=.;
retain cflag;
if (first.&location & first.&class & first.&length) then cflag = 1;
if &chkdate ne tempdate+1 then cflag = cflag +1;
run;
data two(keep = &location &class &length cflag consecutive);
set one;
by &location &class &length cflag;
retain consecutive;
if first.cflag then consecutive = 0;
consecutive = consecutive +1;
if last.cflag and consecutive>=&numconday;
run;
data three;
merge one two(in=a);
by &location &class &length cflag;
if a;
run;
proc sort data=three;
by &location &class &length cflag &chkdate;
run;
data four;
set three;
by &location &class &length cflag &chkdate;
retain firstdate;
if first.cflag then firstdate = &chkdate;
firstdate = datepart(firstdate);
lastdate = datepart(lastdate);
format lastdate firstdate mmddyy10.;
lastdate = firstdate + consecutive;
if last.cflag;
run;
proc sort data=four(drop=cflag &chkdate tempdate) out=&outdata;
by &location &class &length firstdate;
run;
proc sql;
alter table &outdata
add advance_days num label = 'advance_days' format = BEST.
add range_length num label = 'range_length' format = BEST.
modify arv_dt date format = mmddyy10.;
update &outdata
SET advance_days = arv_dt - firstdate;
update &outdata
SET range_length = consecutive;
alter table &outdata
drop consecutive;
quit;
数据有:
city_cd shop_car_type_cd lor arv_dt shop_dttm
atl bcar 1 6/1/16 6/1/16
atl bcar 1 6/1/16 6/3/16
atl bcar 1 6/1/16 6/2/16
atl ccar 1 6/1/16 6/4/16
atl bcar 1 6/1/16 6/5/16
atl bcar 1 6/1/16 6/6/16
atl bcar 2 6/2/16 6/7/16
数据想要:
city_cd shop_car_type_cd lor arv_dt shop_start shop_end consec_days
atl bcar 1 6/1/16 6/1/16 6/3/16 3
atl bcar 1 6/1/16 6/5/16 6/6/16 2
atl bcar 2 6/2/15 6/7/16 6/7/16 0
atl ccar 1 6/1616 6/4/2016 6/4/16 0