Finding the max value of a variable for a given student ID (several rows per student)

Reply
New User
Posts: 1

Finding the max value of a variable for a given student ID (several rows per student)

Hi,

 

New SAS user here!

 

I'm trying to create a new variable based on the max of another variable associated with unique student IDs. Here's an example of the dataset. I'm trying to create the variable titled "MaxTerm". How would I specify that I'm looking for the max term for each student ID?

 

Thanks the help!

 

Student_IDTermMaxTerm
00000114
00000124
00000134
00000144
00000215
00000225
00000235
00000245
00000255
Esteemed Advisor
Posts: 6,706

Re: Finding the max value of a variable for a given student ID (several rows per student)

proc sql;
create table want as
select *, max(term) as max_term
from have
group by student_id
;
quit;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Grand Advisor
Posts: 10,241

Re: Finding the max value of a variable for a given student ID (several rows per student)

Is your Maxterm variable numeric or character? If character you my get unexpected results.

Example:

data junk;
   x='15';   output;
   x='5';    output;
run;

proc sql;
   select max(x)
   from junk;
quit;

will return '5' as the maximum value because when comparing characters '5' is greater than '1'. The second character only comes into consideration when the first character is the same.

 

Ask a Question
Discussion stats
  • 2 replies
  • 92 views
  • 0 likes
  • 3 in conversation