/*建立测试数据集test*/
data test;
input orderID $ customerID $ price purchase_brand $ (buy_date repair_date) (:yymmdd10.);
format buy_date repair_date yymmdd10.;
cards;
1 001 1253 西门子 2005/10/05 .
2 002 1745 dell 2001/10/05 2012/11/04
3 003 4598 小米 2002/10/05 2001/01/03
4 003 321 海信 2004/10/05 .
5 004 546 海尔 2003/10/05 2015/05/01
6 005 2154 海信 2001/10/05 .
7 006 999 海尔 2005/10/05 2013/10/25
8 003 10000 小米 2005/10/05 2001/01/03
;
run;
/*根据customerID升序、repair_date降序排序*/
proc sort data=test;
by customerID descending repair_date;
run;
/*(1)有维修记录的顾客在维修日期之后的购买记录的次数,以及在维修日期之后购买的总金额*/
data test1;
set test;
by customerID descending repair_date;
retain purchase_times sum_price repair_date_temp;
if first.customerID then
do;
purchase_times=0;
sum_price=0;
repair_date_temp=repair_date;
end;
if not missing(repair_date_temp) and buy_date>repair_date_temp then
do;
purchase_times+1;
sum_price+price;
end;
if last.customerID;
run;
/*根据customerID升序、purchase_brand升序、repair_date降序排序*/
proc sort data=test;
by customerID purchase_brand descending repair_date;
run;
/*(2)有维修记录的顾客在维修日期之后的购买同一品牌的次数,以及在维修日期之后购买同一品牌的总金额。*/
data test2;
set test;
by customerID purchase_brand descending repair_date;
retain purchase_times sum_price repair_date_temp;
if first.purchase_brand then
do;
purchase_times=0;
sum_price=0;
repair_date_temp=repair_date;
end;
if not missing(repair_date_temp) and buy_date>repair_date_temp then
do;
purchase_times+1;
sum_price+price;
end;
if last.purchase_brand;
run;


雷达卡





京公网安备 11010802022788号







