- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!! Your previous response was correct as well I just didn't see that group by...thanks again!