全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2663 3
2016-11-11
如果我要按照每一个id,汇总cns的值。data chapt4.retain1;
input id txn_cde$ cns txn_dte$;
cards;
10 101 10 20070101
10 101 20 20080402
10 201 30 20050203
20 101 40 20040105
20 201 50 20040105
20 301 60 20070806
20 201 70 20050607
30 301 80 20070501
30 401 90 20070306
;
run;

proc sql;
create table sum as
select
id
,sum(cns) as cns
,count(*) as count
,sum(case when txn_cde in("101" "201") then 1 else 0 end) as cnt_condi
,min(txn_dte) as min_txn_dte

from chapt4.retain1;
group by 1
;
quit;
proc sort data= chapt4.retain1;by id txn_dte;run;
data test1;
        set chapt4.retain1;
        by id txn_dte;
        retain min_dte sum_cns cnt cnt_condition;
        if first.id then do;
                min_dte=txn_dte;
                sum_cns=0;
                cnt=0;
                cnt_condition=0;
        end;
        min_dte=min(min_dte,txn_dte);
        sum_cns+cns;
        cnt+1;
        cnt_condition+(txn_cde in("101" "201"));
        if last.id;
run;

其中group by 1 放在这里是什么意思,然后我应该只要对id排序就好了吧,为什么还要放一个txn_dte?



二维码

扫码加我 拉你入群

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

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

全部回复
2016-11-11 15:23:39
1. group by 1是在SQL语句中对第一列变量进行分组操作,这里相当于group by id
2. 注意语句:if first.id then do; min_dte=txn_dte; min_dte 的取值是需要对 txn_dte 排序后才合理
二维码

扫码加我 拉你入群

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

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

2016-11-11 15:57:44
zhangzachary 发表于 2016-11-11 15:23
1. group by 1是在SQL语句中对第一列变量进行分组操作,这里相当于group by id
2. 注意语句:if first.id  ...
因为他的结果是存在每个id按照上述条件输出最后一条记录的,所以我看了一下好像只有group by 1程序复合这个要求,那group by 1 为什么可以达到这个目的呢?谢谢
二维码

扫码加我 拉你入群

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

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

2016-11-11 16:30:41
玄之玄 发表于 2016-11-11 15:57
因为他的结果是存在每个id按照上述条件输出最后一条记录的,所以我看了一下好像只有group by 1程序复合这 ...
并没有看懂你的问题。
SQL在conditional summary上相对简单一些,不过后面的sort+data步一样是可以实现相同需求的。
这里group by 1 = group by id, 如果你熟悉proc步可以理解为class id; 并没有其他特殊的意义。
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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