BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
udupa_13
Fluorite | Level 6

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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.

Reeza
Super User
So SUM of a variable at the latest date?
udupa_13
Fluorite | Level 6
Yes
Reeza
Super User
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.

andreas_lds
Jade | Level 19

@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.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1094 views
  • 1 like
  • 4 in conversation