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
;
When you say rank, do you mean sort? 🙂
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?
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
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.
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
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)
@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?
@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.
@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 4To 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.
@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 4To 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 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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.