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

/*Hi SAS Forum,

I have posted this same question in another forum but due to urgency I posted it here too (apologies).

I have over million records dataset like this.

In this, same cust_num (that is 1) together with same acct_num (that is 111) occur three times

and make three by-groups.

And there is a single other by group related to acct 222.*/

 

data have;

input cust_num acct_num date score_ID;

cards;

1 111 20150324 40000

1 111 20150324 50000

1 111 20150324 60000

1 111 20150324 70000

1 111 20150324 80000

1 111 20150402 40000

1 111 20150402 50000

1 111 20150402 60000

1 111 20150402 70000

1 111 20150402 80000

1 111 20150425 40000

1 111 20150425 50000

1 111 20150425 60000

1 111 20150425 70000

1 111 20150425 80000

2 222 20180302 70000

2 222 20180302 90000

;

run;

 

/*Q: I need to keep only the latest by group.

Answer data set should be this because 20150425 is the latest date out of the three by groups for acct 111.

And three is a single other by group for acct 222.

 

1 111 20150425 40000

1 111 20150425 50000

1 111 20150425 60000

1 111 20150425 70000

1 111 20150425 80000

2 222 20180302 70000

2 222 20180302 90000

 

Could you please help me to do the coding. We should not use any hard coding like

 

if date = 20150425;

 

because I do not know what are the latest dates in other million records

 

Mirisa

 

*/

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;

input cust_num acct_num date :yymmdd10. score_ID;
format date yymmdd10.; 
cards;
1 111 20150324 40000
1 111 20150324 50000
1 111 20150324 60000
1 111 20150324 70000
1 111 20150324 80000
1 111 20150402 40000
1 111 20150402 50000
1 111 20150402 60000
1 111 20150402 70000
1 111 20150402 80000
1 111 20150425 40000
1 111 20150425 50000
1 111 20150425 60000
1 111 20150425 70000
1 111 20150425 80000
2 222 20180302 70000
2 222 20180302 90000
;

run;

proc sql;
create table want as
select *
from have
group by cust_num, acct_num
having date=max(date);
quit;

View solution in original post

1 REPLY 1
novinosrin
Tourmaline | Level 20
data have;

input cust_num acct_num date :yymmdd10. score_ID;
format date yymmdd10.; 
cards;
1 111 20150324 40000
1 111 20150324 50000
1 111 20150324 60000
1 111 20150324 70000
1 111 20150324 80000
1 111 20150402 40000
1 111 20150402 50000
1 111 20150402 60000
1 111 20150402 70000
1 111 20150402 80000
1 111 20150425 40000
1 111 20150425 50000
1 111 20150425 60000
1 111 20150425 70000
1 111 20150425 80000
2 222 20180302 70000
2 222 20180302 90000
;

run;

proc sql;
create table want as
select *
from have
group by cust_num, acct_num
having date=max(date);
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1 reply
  • 632 views
  • 0 likes
  • 2 in conversation