I am trying to group by USERID to know MAX of TRANSACTION_DATE and on top of that want to sum AMOUNT only for those MAX of TRANSACTION_DATE. Any optimal solution?
Try
proc sql;
create table want as
select
userid,
transaaction_date,
amount_sum
from (
select
userid,
transaction_date,
sum(amount) as amount_sum
from have
group by userid, transaction_date
)
group by userid
having transaction_date = max(transaction_date)
;
quit;
Untested; for tested code, please supply source data in a data step with datalines.
Try
proc sql;
create table want as
select
userid,
transaaction_date,
amount_sum
from (
select
userid,
transaction_date,
sum(amount) as amount_sum
from have
group by userid, transaction_date
)
group by userid
having transaction_date = max(transaction_date)
;
quit;
Untested; for tested code, please supply source data in a data step with datalines.
proc sql;
create table want as
select
userid,
transaaction_date,
sum(amount) as amount_sum
from have
group by userid
having transaction_date = max(transaction_date)
;
quit;
You can simplify @Kurt_Bremser query to the above in SAS at least.
@udupa_13 wrote:
I am trying to group by USERID to know MAX of TRANSACTION_DATE and on top of that want to sum AMOUNT only for those MAX of TRANSACTION_DATE. Any optimal solution?
Optimal depends on the perspective: an administrator will consider a solution optimal if it does not use to much of the precious resources a server has, someone else will want as few lines of code as possible and another one does not care about the number of lines, but wants code that can be read easily.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.