简体   繁体   中英

Grouping by calculated field not working as expected

In the below query, I'm attempting to group by ID, and a calculated date field. I'd like to do this in a single proc sql, it works if I do 2 separate SQL calls, but for some reason I'm not able to group by my calculated column. I even tried to change the group by to: "group by 1,2" and that didn't work. I've confirmed that both date fields are SAS dates, not date/time fields. Am I missing something completely obvious?

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;

I think the documentation is pretty clear on this:

CALCULATED enables you to use the results of an expression in the same SELECT clause or in the WHERE clause. It is valid only when used to refer to columns that are calculated in the immediate query expression.

That doesn't say GROUP BY . So, use a subquery or repeat the logic -- and remove the distinct while you are at it:

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;

It works for me. Perhaps there was something else wrong?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM