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

Hi Team,

 

write sas code to find out 3rd max weight on each sex group wise from sashelp.class by using data step programming

 

1 ACCEPTED SOLUTION

Accepted Solutions
ghosh
Barite | Level 11
proc sort data=sashelp.class out=class;
by  sex descending weight;
run;

proc rank data=class out=rank_3(where=(weight_rank=3)) descending;
by sex;
var weight;
ranks weight_rank;
run;

If there are ties, how would you like to resolve them?

 

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Sort by sex and descending weight.

In a data step with

by sex;

use

if first.sex

to set a counter to 1, otherwise increment counter with

counter + 1;

Use a subsetting if

if counter = 3;

to only output the third observation for each group. I intentionally leave writing the whole code to you, so you have opportunity for learning.

PaigeMiller
Diamond | Level 26

@nayab_shaik it would be helpful to all of us if you could use meaningful titles for your posts, instead of "SAS". For example, could you please change the title of this thread to "Find 3rd highest in a group"?

--
Paige Miller
anushreebiotech
Obsidian | Level 7

Hi,

 

proc sort data= sashelp.class out= class_check;
by sex descending Weight ;
run;

 

data class_check2;
set class_check;
by sex descending Weight;
if first.sex then counter=0;
counter+1;
if counter=3 then output;
drop name age counter;
run;

 

Regards,

Anushree

ghosh
Barite | Level 11
proc sort data=sashelp.class out=class;
by  sex descending weight;
run;

proc rank data=class out=rank_3(where=(weight_rank=3)) descending;
by sex;
var weight;
ranks weight_rank;
run;

If there are ties, how would you like to resolve them?

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 4 replies
  • 1767 views
  • 0 likes
  • 5 in conversation