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?

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1368 views
  • 0 likes
  • 5 in conversation