全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1048 2
2017-11-05
悬赏 20 个论坛币 未解决

有这样一个数据集test,是某零售商关于顾客购买记录以及商品维修记录:

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)有维修记录的顾客在第一次维修日期之后的购买同一品牌(与第一次维修时的品牌相同)的次数,以及在维修日期之后购买同一品牌的总金额。


现在的问题是:计算出有的顾客购买同一品牌的次数大于其购买次数,按理来说购买同一品牌的次数应该小于等于其购买次数的,请问这是什么原因呢。这种情况占了样本量百分之一左右。请大神帮忙看看是哪里不对。

程序如下:

proc sortdata=test;
   by customerID descending repair_date;
run;

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) andbuy_date>repair_date_temp then
   do;
      purchase_times+1;
      sum_price+price;
   end;
   if last.customerID;
run;

proc sort data=test;
   by customerID purchase_brand descendingrepair_date;
run;

/*(2)有维修记录的顾客在维修日期之后的购买同一品牌的次数,以及在维修日期之后购买同一品牌的总金额。*/
data test2;
   set test;
   by customerID purchase_brand descendingrepair_date;
   retain purchase_times sum_price repair_date_temp;
   if first. customerID and first.purchase_brandthen
   do;
      purchase_times=0;
      sum_price=0;
      repair_date_temp=repair_date;
   end;
   if not missing(repair_date_temp) andbuy_date>repair_date_temp then
   do;
      purchase_times+1;
      sum_price+price;
   end;
   if last. customerID and last.purchase_brand;
run;


二维码

扫码加我 拉你入群

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

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

全部回复
2017-11-5 17:12:25
因为(2)你用了
"if first. customerID and first.purchase_brand then"
"if last. customerID and last.purchase_brand;"
而不是
"if first.purchase_brand then"
"if last.purchase_brand;"
二维码

扫码加我 拉你入群

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

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

2017-11-5 23:19:37
l1i2n3i4n5g 发表于 2017-11-5 17:12
因为(2)你用了
"if first. customerID and first.purchase_brand then"
"if last. customerID and last ...
我最开始时是按照您的指导写的,即
if first.purchase_brand then
if last.purchase_brand;
但是出来的结果是(某个customerID)这样:
customerID  purchase_times   sum_price
1                     0                        0
1                     3                       1560
1                     0                       0
1                     0                       0
1                     0                       0
程序(1)没有出现这种情况,每个customerID只有一行观测,程序(2)就出现了每个customerID有多行观测,并且值不一样。
此外,按照您正确程序计算出来的同样有一小部分 顾客购买同一品牌的次数大于其购买次数,我挑出这些自己手算发现对于这部分顾客,程序(1)的计算出现了错误,但是其他大部分计算都是正确的,请问这是什么原因呢?
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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