BookmarkSubscribeRSS Feed
shivamarrora0
Obsidian | Level 7

Hi All,

I want to rank the below id according to marks using only data step first dot last dot , Kindly help

 

data ma;
input id$ marks;
cards;
a 20
b 33
c 13
d 44
e 13
f 33
;

13 REPLIES 13
PeterClemmensen
Tourmaline | Level 20

When you say rank, do you mean sort? 🙂

ballardw
Super User

And what would the output look like?

Ranking often involves adding a variable that indicates the "rank" a specific record belongs to. But there should be some idea for how those ranks are assigned and what the number of ranks would be. Do you want to rank on a 1 to 5 scale, a 1 to 10 scale, a 1 to 100 or something else?

novinosrin
Tourmaline | Level 20

Where are the groups to use by variables in your sample. Your id values are all unique. The marks has ties. The point of using first and last variables would not make sense your requirement. To use first and last, you need repeats on by values. For example,

1. a transaction dataset

2. Patients multiple visits to hospitals

3. Repeat customers in a store like a loyatly program

 

If you understand the business, the code will come to your mind automatically. Anyways, let us the know the correct req, you will get a neat solution. Also, you should let us know how to handle ties in ranks, i.e b and f in your example, both having scored 33

Kurt_Bremser
Super User

Using "base sas" as the subject of a question in the Base SAS forum is not very bright. Please be more descriptive on your next question.

 

Use proc rank for ranking, BTW.

shivamarrora0
Obsidian | Level 7

The out put i want to sort the marks in order then rank them ;

if the marks are same then the rank should tie  and if the null value then it skip that rank

 

for example

 

marks rank

30       1

22        2

22        2

20        3

 

Like this 

PaigeMiller
Diamond | Level 26

all that can be done in PROC RANK

 

No need to sort the data before running PROC RANK (unless you need it sorted for some other reason)

--
Paige Miller
ballardw
Super User

@shivamarrora0 wrote:

The out put i want to sort the marks in order then rank them ;

if the marks are same then the rank should tie  and if the null value then it skip that rank

 

for example

 

marks rank

30       1

22        2

22        2

20        3

 

Like this 


Since the data step doesn't make it easy to "look ahead" in the data your requirement for ties would add a lot of complexity to data step logic. WHY is the requirement not to use Proc Rank which is designed to do this?

Kurt_Bremser
Super User

@ballardw wrote:

@shivamarrora0 wrote:

The out put i want to sort the marks in order then rank them ;

if the marks are same then the rank should tie  and if the null value then it skip that rank

 

for example

 

marks rank

30       1

22        2

22        2

20        3

 

Like this 


Since the data step doesn't make it easy to "look ahead" in the data your requirement for ties would add a lot of complexity to data step logic. WHY is the requirement not to use Proc Rank which is designed to do this?


I guess that, because proc rank would give this result:

marks rank
30    1
22    2
22    2
20    4

To count equal values into just one rank, I would

- sort the dataset by descending marks

- in a data step with by descending marks, retain rank (starting with 0) and increment it at every first.

PaigeMiller
Diamond | Level 26

@Kurt_Bremser wrote:

@ballardw wrote:

@shivamarrora0 wrote:

The out put i want to sort the marks in order then rank them ;

if the marks are same then the rank should tie  and if the null value then it skip that rank

 

for example

 

marks rank

30       1

22        2

22        2

20        3

 

Like this 


Since the data step doesn't make it easy to "look ahead" in the data your requirement for ties would add a lot of complexity to data step logic. WHY is the requirement not to use Proc Rank which is designed to do this?


I guess that, because proc rank would give this result:

marks rank
30    1
22    2
22    2
20    4

To count equal values into just one rank, I would

- sort the dataset by descending marks

- in a data step with by descending marks, retain rank (starting with 0) and increment it at every first.


PROC RANK will give average ranks to ties if you want, and you can easily reverse the rankings if they're in the wrong order.

--
Paige Miller
ballardw
Super User

@Kurt_Bremser wrote:

@ballardw wrote:

@shivamarrora0 wrote:

The out put i want to sort the marks in order then rank them ;

if the marks are same then the rank should tie  and if the null value then it skip that rank

 

for example

 

marks rank

30       1

22        2

22        2

20        3

 

Like this 


Since the data step doesn't make it easy to "look ahead" in the data your requirement for ties would add a lot of complexity to data step logic. WHY is the requirement not to use Proc Rank which is designed to do this?


I guess that, because proc rank would give this result:

marks rank
30    1
22    2
22    2
20    4

To count equal values into just one rank, I would

- sort the dataset by descending marks

- in a data step with by descending marks, retain rank (starting with 0) and increment it at every first.


Or use the TIES= option for either Low or high

data junk;
   input marks;
datalines;
22
30
22
20
;
run;

proc rank data=junk descending ties=low out=ranked;
   var marks;
   ranks rank;
run;
shivamarrora0
Obsidian | Level 7
I think the below code will work


proc sort data=test;
by descending marks;
run;

data rank;
set test;
by descending marks;
if first.marks then count+1;
run;
Kurt_Bremser
Super User

@shivamarrora0 wrote:
I think the below code will work


proc sort data=test;
by descending marks;
run;

data rank;
set test;
by descending marks;
if first.marks then count+1;
run;

Exactly the solution I was pointing you at!

shivamarrora0
Obsidian | Level 7
Thank you Kurt..

i was just confuse .. t

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 13 replies
  • 1332 views
  • 2 likes
  • 6 in conversation