SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 30485 views
  • 0 likes
  • 2 in conversation