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

So I have 100k+ records. Some students have taken this test several times and have gotten different scores. I'm trying to select the max term and the score that would accompany that latest term for each student.

 

However, when I run this code:

 

proc sql;
create table test as 
select distinct stu_id, max(term) as term, score
from table
group by stu_id, score
;
quit;

 

For example, if I have this student 

 

005G0K2W 3 400

005G0K2W 5 235

 

this is what I end up getting, for example:

 

005G0K2W 5 400

005G0K2W 5 235

 

but I want this:

 

005G0K2W 5 235

 

edit: students don't have to have all terms or be enrolled up to term 5, so some students might only have term 1, some might have terms 2 and 5 or any other combination.

 

Have: 

009FX8YG 1 111

009FX8YG 4 259

010G0K2W 3 400

010G0K2W 5 235

01WG248D 2 369

01WG248D 3 326

09FX569H 2 654

 

Want:

009FX8YG 4 259

010G0K2W 5 235

01WG248D 3 326

09FX569H 2 654

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input stu_id $ term score;
cards;
009FX8YG 1 111
009FX8YG 4 259
010G0K2W 3 400
010G0K2W 5 235
01WG248D 2 369
01WG248D 3 326
09FX569H 2 654
;

proc sql;
create table want as
select *
from have
group by stu_id
having term=max(term);
quit;

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Are you missing having clause

 

proc sql;
create table test as 
select distinct stu_id,  term, score
from table
group by stu_id
having term=max(term)
;
quit;
serrld113
Obsidian | Level 7

This will not work...it keeps only the max term overall (5). So terms 1,2,3,4 are now completely gone along with the students that don't have a term 5. 

novinosrin
Tourmaline | Level 20

Ok, Can you please provide a better and comprehensive sample of what you HAVE and what you WANT, so that the community can code, test and then post plz

novinosrin
Tourmaline | Level 20

data have;
input stu_id $ term score;
cards;
009FX8YG 1 111
009FX8YG 4 259
010G0K2W 3 400
010G0K2W 5 235
01WG248D 2 369
01WG248D 3 326
09FX569H 2 654
;

proc sql;
create table want as
select *
from have
group by stu_id
having term=max(term);
quit;
serrld113
Obsidian | Level 7

Thank you!! Your previous response was correct as well I just didn't see that group by...thanks again!

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
  • 5 replies
  • 25338 views
  • 0 likes
  • 2 in conversation