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
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;
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;
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.
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
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;
Thank you!! Your previous response was correct as well I just didn't see that group by...thanks again!
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.
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.