944 2

[学习分享] 请熟悉SAS的大神指导一下程序 [推广有奖]

  • 0关注
  • 0粉丝

硕士生

74%

还不是VIP/贵宾

-

威望
0
论坛币
166 个
通用积分
5.7001
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
946 点
帖子
49
精华
0
在线时间
273 小时
注册时间
2013-1-10
最后登录
2024-4-2

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;


关键词:sas的 purchase Customer missing Repair
沙发
l1i2n3i4n5g 在职认证  发表于 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;"

使用道具

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)的计算出现了错误,但是其他大部分计算都是正确的,请问这是什么原因呢?

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 08:51