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

Hello Fellow SAS Programmers,

 

I am trying to classify ranked observations in my dataset. I am interested in identifying the top 10 performing observations. The challenge is that there can potentially be tied values among more than the top 10 performing observations.  

 

In my dataset, I have performance score (X variable) for each of 270 observations. Some observations have the same score. After I conduct a descending sort of the file using the variable X,  I  want to identify the top 10 scoring observations in a way that permits tied observations can be included in the top 10 performance category (i.e., Top_10 = 'Yes').

 

Below, I display an example of what I am trying to achieve.  Can someone please help me by suggesting SAS code that can achieve what I want. It would be great if it were part of a data step, but it doesn't have to be. For instance, I could probably create a file that has no duplicate performance scores, then create a counting variable, and then merge that file with the initial dataset. 

 

             X          Top_10

            99             Yes

            90             Yes

            83             Yes

            83             Yes

            72             Yes

            71              Yes

            71              Yes

            65             Yes

            52             Yes

            43             Yes

            31              Yes

            20            Yes

 

Thank you,

Rover17

1 ACCEPTED SOLUTION

Accepted Solutions
john_mccall
SAS Employee

Hi.  Consider this sample program.

/* Create a sample table that is ordered By descending X*/
Data Sample;
infile datalines;
input X ;
datalines;
99
90
83
83
72
71
71
65
52
43
31
20
20
19
18
;
run;
/* Count 1 for a single BY value and for count 1 for duplicate BY values*/
data Top10;
set sample;
by descending x;
If last.x then count+1;
if count<=10 then do;
Top10='Yes';
output;
end;
else do;
Top10='No';
output;
end;
run;

proc print data=top10;
title "Counting duplicates as 1 in Tp 10 table";
run;

View solution in original post

2 REPLIES 2
john_mccall
SAS Employee

Hi.  Consider this sample program.

/* Create a sample table that is ordered By descending X*/
Data Sample;
infile datalines;
input X ;
datalines;
99
90
83
83
72
71
71
65
52
43
31
20
20
19
18
;
run;
/* Count 1 for a single BY value and for count 1 for duplicate BY values*/
data Top10;
set sample;
by descending x;
If last.x then count+1;
if count<=10 then do;
Top10='Yes';
output;
end;
else do;
Top10='No';
output;
end;
run;

proc print data=top10;
title "Counting duplicates as 1 in Tp 10 table";
run;

PaigeMiller
Diamond | Level 26

Use PROC RANK to identify top X observations, including ties. I guess you could write DATA step code to do this, but why bother when SAS has already given you the tools to do it very quickly and easily?

 

proc rank data=have descending ties=low out=want;
    var x;
    ranks x_ranked;
run;


If TIES=LOW isn't what you want, there are other options.

 

PaigeMiller_0-1689535655649.png

 

--
Paige Miller

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!

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
  • 2 replies
  • 612 views
  • 1 like
  • 3 in conversation