在下面的查询中,我尝试按ID分组,并计算日期字段。我想在单个proc sql中执行此操作,如果我执行2次单独的SQL调用,它会起作用,但由于某种原因,我无法按计算列进行分组。我甚至尝试将小组更改为:" group by 1,2"那不起作用。我确认两个日期字段都是SAS日期,而不是日期/时间字段。我错过了一些完全明显的东西吗?
proc sql;
create table work.test as
select distinct
t1.id,
(case when t1.date1>t2.date2 then t2.date2 else t1.date1 end) format mmddyys10.0 as new_date,
sum(t1.amt) format dollar8.2 as new_amt
from
work.cr_2 t1
inner join work.cr_1 t2 on t1.id=t2.id
group by
t1.id,
calculated new_date;
quit;
答案 0 :(得分:2)
我认为documentation非常明确:
CALCULATED使您可以使用表达式的结果 SELECT子句或WHERE子句。它仅在习惯时有效 引用在立即查询中计算的列 表达
这并不是说GROUP BY
。因此,使用子查询或重复逻辑 - 并在您访问时删除distinct
:
proc sql;
create table work.test as
select tid, new_date,
sum(t1.amt) format dollar8.2 as new_amt
from (select t1.*,
(case when t1.date1 > t2.date2 then t2.date2 else t1.date1 end) format mmddyys10.0 as new_date
from work.cr_2 t1 inner join
work.cr_1 t2
on t1.id = t2.id
) tt
group by id, new_date;
quit;
答案 1 :(得分:0)
它对我有用。也许还有其他问题?
data cr_2 ;
input id date1 amt ;
informat date1 yymmdd10. amt $dollar32.;
format date1 yymmdd10. ;
cards;
1 2015-12-01 100
;;;;
data cr_1 ;
input id date2 amt ;
informat date2 yymmdd10. amt $dollar32.;
format date2 yymmdd10. ;
cards;
1 2015-12-01 100
;;;;
proc sql;
create table work.test as
select t1.id
, case when t1.date1>t2.date2 then t2.date2 else t1.date1
end format yymmdd10. as new_date
, sum(t1.amt) format dollar8.2 as new_amt
from work.cr_2 t1
inner join work.cr_1 t2
on t1.id=t2.id
group by t1.id
, calculated new_date
;
quit;
proc print; run;