Hi Team,
write sas code to find out 3rd max weight on each sex group wise from sashelp.class by using data step programming
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?
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.
@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"?
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
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?
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.
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.
Ready to level-up your skills? Choose your own adventure.