DATA Step, Macro, Functions and more

base sas

Reply
Contributor
Posts: 31

base sas

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
;

PROC Star
Posts: 551

Re: base sas

When you say rank, do you mean sort? Smiley Happy

Super User
Posts: 10,497

Re: base sas

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?

PROC Star
Posts: 169

Re: base sas

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

Super User
Posts: 6,936

Re: base sas

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 31

Re: base sas

[ Edited ]

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 

Trusted Advisor
Posts: 1,612

Re: base sas

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)

Super User
Posts: 10,497

Re: base sas


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?

Super User
Posts: 6,936

Re: base sas


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,612

Re: base sas


KurtBremser 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.

Super User
Posts: 10,497

Re: base sas


KurtBremser 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;
Contributor
Posts: 31

Re: base sas

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;
Super User
Posts: 6,936

Re: base sas


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!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 31

Re: base sas

Thank you Kurt..

i was just confuse .. t
Ask a Question
Discussion stats
  • 13 replies
  • 161 views
  • 2 likes
  • 6 in conversation