全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1044 2
2017-11-01
悬赏 20 个论坛币 已解决
有这样一个数据集,是关于顾客购买记录以及商品维修记录,如下:
(订单编号 顾客编号     价格     购买产品的品牌  购买日期      维修日期)

orderID  customerID   price    purchase_brand   buy_date       repair_date      
1              001           1253       西门子            2005/10/05     .
2              002           1745       dell                2001/10/05      2012/11/04
3              003          4598        小米               2002/10/05      2011/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


现在想要计算出:(1)有维修记录的顾客在维修日期之后的购买记录的次数,以及在维修日期之后购买的总金额


                       (2)有维修记录的顾客在维修日期之后的购买同一品牌的次数,以及在维修日期之后购买同一品牌的总金额。

因急着向导师汇报,请熟悉SAS的朋友帮忙写一个程序,感激不尽

最佳答案

l1i2n3i4n5g 查看完整内容

/*建立测试数据集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 ...
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-11-1 09:26:54
/*建立测试数据集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;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-11-2 05:56:21
l1i2n3i4n5g 发表于 2017-11-1 09:26
/*建立测试数据集test*/
data test;
input orderID $ customerID $ price purchase_brand $ (buy_date re ...
谢谢指导,已经成功运行。
再麻烦问一个小问题,为什么跑出来的结果每个customerID 只保留了一条观测(每个顾客可能有多个orderID或者维修记录),如果我想只保留每个顾客第一次的维修的观测,该怎么做呢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群