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

Hello SAS Community,

 

I am wondering if there is any function, similar to Min and Max function, to identify the second or third highest or lowest value across the column.

example: We are given student_Ids with average monthly results. We have to calculate highest, second highest, third highest avg. monthly score. Something similar to the following dataset:

 

Student_IDNameClassJanFebMarAprMayJunJulAugSepOctNovDecMaxMinSecond_HighestThird_highest
A-1101John8343328272728385042442420    
A-1102Peter7363530292930405244462622    
A-1103Aslam8383732313132425446482824    
A-1104Neeraj9403934333334445648503026    
A-1105Kulsoom7424136353536465850523228    
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

SAS functions LARGEST() and SMALLEST() can do such things.

data have;
  infile datalines truncover dlm='|';
  input (Student_ID	Name) ($)	Class	Jan	Feb	Mar	Apr	May	Jun	Jul	Aug	Sep	Oct	Nov	Dec	;
  array months {*} Jan	Feb	Mar	Apr	May	Jun	Jul	Aug	Sep	Oct	Nov	Dec;
  Max=max(of months[*]);
  Min=min(of months[*]);
  Second_Highest=largest(2, of months[*]);
datalines;
A-1101|John|8|34|33|28|27|27|28|38|50|42|44|24|20| | | | 
A-1102|Peter|7|36|35|30|29|29|30|40|52|44|46|26|22| | | | 
A-1103|Aslam|8|38|37|32|31|31|32|42|54|46|48|28|24| | | | 
A-1104|Neeraj|9|40|39|34|33|33|34|44|56|48|50|30|26| | | | 
A-1105|Kulsoom|7|42|41|36|35|35|36|46|58|50|52|32|28| 
;
proc print;
run;

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

SAS functions LARGEST() and SMALLEST() can do such things.

data have;
  infile datalines truncover dlm='|';
  input (Student_ID	Name) ($)	Class	Jan	Feb	Mar	Apr	May	Jun	Jul	Aug	Sep	Oct	Nov	Dec	;
  array months {*} Jan	Feb	Mar	Apr	May	Jun	Jul	Aug	Sep	Oct	Nov	Dec;
  Max=max(of months[*]);
  Min=min(of months[*]);
  Second_Highest=largest(2, of months[*]);
datalines;
A-1101|John|8|34|33|28|27|27|28|38|50|42|44|24|20| | | | 
A-1102|Peter|7|36|35|30|29|29|30|40|52|44|46|26|22| | | | 
A-1103|Aslam|8|38|37|32|31|31|32|42|54|46|48|28|24| | | | 
A-1104|Neeraj|9|40|39|34|33|33|34|44|56|48|50|30|26| | | | 
A-1105|Kulsoom|7|42|41|36|35|35|36|46|58|50|52|32|28| 
;
proc print;
run;
awais
Obsidian | Level 7

Hi Patrick,

 

Ditto! Excellent. Never knew that :). thank you for your help!

 

awais

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1227 views
  • 7 likes
  • 3 in conversation