全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
7707 9
2010-12-03
各位大虾:
在sas/hash中碰到下面一个问题,一直苦于无法解决,请各位大虾帮忙:
例如(实际客户上万个):A客户有三条记录,每条记录的金额均小于200,;B客户有5条记录,每条记录的金额均大于150,但小于400。
在hash中,如何实现下面条件?
条件:如果某个客户的总金额(该客户所有记录的金额加和)<600,则将该客户的所有记录删除。
如上例中,A客户总金额<600,则删除A客户。

不用sql解决,只用sas/hash解决,并且一步实现
二维码

扫码加我 拉你入群

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

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

全部回复
2010-12-3 17:27:58
data test;
     input id $ amount;
     datalines;
A  189
B  160
B  188
B  165
A  145
A  199
B  198
B  345
;


data wanted;
    if 0 then set test;
    if _n_=1 then do;
            declare hash h();
                    h.definekey('id');
                        h.definedata('id','total');
                        h.definedone();
        end;
        do until(last);
            set test end=last;
                if h.find()=0 then do;
            total+amount;
                        h.replace();
                end;
                else do;
                     total=amount;
             h.add();
                end;
         end;
         do until(last1);
         set test end=last1;
                 if h.find()=0 and total>=600 then output;
         end;
         drop total;
         stop;
run;
二维码

扫码加我 拉你入群

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

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

2010-12-7 10:14:10
非常感谢,再多问一句,如果有另一个表(test1),内有字段id、name等,想用一个data步实现关联和筛选,我写的程序如下,结果是name一栏全为空?:
data wanted;
    if 0 then set test1;
    if _n_=1 then do;
        declare hash h();
                h.definekey('id');
                h.definedata('id','name','total');
                h.definedone();
    end;
    do until(last);
        set test end=last;
        if h.find()=0 then do;
            total+amount;
            name=name;
            h.replace();
         end;
         else do;
             total=amount;
             name=name;
             h.add();
         end;
     end;
     do until(last1);
         set test end=last1;
         if h.find()=0 and total>=600 then output;
     end;
     drop total;
     stop;
run;
二维码

扫码加我 拉你入群

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

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

2010-12-7 10:47:17
data test;
     input id $ amount;
     datalines;
A  189
B  160
B  188
B  165
A  145
A  199
B  198
B  345
;

data test1;
    input id $ name $;
datalines;
A Jose
B Hose'B'
;

data wanted;
    if 0 then set test test1;
    if _n_=1 then do;
            declare hash h();
                    h.definekey('id');
                        h.definedata('id','total');
                        h.definedone();

            declare hash h_name(dataset:'test1');
                    h_name.definekey('id');
                        h_name.definedata('id','name');
                        h_name.definedone();
        end;
        do until(last);
            set test end=last;
                if h.find()=0 then do;
            total+amount;
                        h.replace();
                end;
                else do;
                     total=amount;
             h.add();
                end;
         end;
         do until(last1);
               set test end=last1;
               if h.find()=0 and total>=600 then do;
                        h_name.find();
                        output;
                end;
         end;
         drop total;
         stop;
run;
二维码

扫码加我 拉你入群

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

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

2010-12-7 11:46:27
谢谢!
二维码

扫码加我 拉你入群

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

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

2010-12-7 20:32:01
学习了
hash我还是停留在小表连大表速度快的应用上
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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