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

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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 2 replies
  • 149 views
  • 1 like
  • 3 in conversation