BookmarkSubscribeRSS Feed
Tom
Super User Tom
Super User

@Jyuen204 wrote:

 

TABLE1

 

ID          Name               Manager           DATE

XXX1     Smith, John     Doe, Jane         15NOV2019

XXX1     Smith, John     Doe, Jane         13NOV2019

XXX1     Smith, John     Crane, Frasier   3NOV2019

 

I am needing to rank a table of data like here. so that the most recent date is ranked the highest.

I need to create a secondary table of this data where RANK = 1

 

so of the data examples above, I want to rank the data in the table, and create a table of the rank 1 rows.

So I am only left with :
XXX1   Smith, John    Doe, Jane


Yet another reason not to use SQL. With a data step you can generate two datasets in one step (if you really need them).

data all most_recent;
  set have;
  by id descending date;
  if first.id then rank=0;
  rank+1;
  if rank=1 then output most_recent;
  output all;
run;
Jyuen204
Obsidian | Level 7

If my table is names TableX how do i reference this in your example to get the data?

novinosrin
Tourmaline | Level 20

Hi @Jyuen204  If I understand you correctly, you want to compute rank using PROC SQL ?

 


data have;
input ID     $      Name     & $20.         Manager & $20. DATE:date9.;
format date date9.;
cards;
XXX1     Smith, John     Doe, Jane         15NOV2019
XXX1     Smith, John     Doe, Jane         13NOV2019
XXX1     Smith, John     Crane, Frasier   3NOV2019
;

proc sql;
create table want as
select a.*,count(b.date) as rank
from have a, have b
where a.id=b.id and a.date<=b.date
group by a.id,a.name,a.manager,a.date
order by id,rank;
quit;

proc print noobs;run;
ID Name Manager DATE rank
XXX1 Smith, John Doe, Jane 15NOV2019 1
XXX1 Smith, John Doe, Jane 13NOV2019 2
XXX1 Smith, John Crane, Frasier 03NOV2019 3
PaigeMiller
Diamond | Level 26

@novinosrin wrote:

Hi @Jyuen204  If I understand you correctly, you want to compute rank using PROC SQL ?

 


data have;
input ID     $      Name     & $20.         Manager & $20. DATE:date9.;
format date date9.;
cards;
XXX1     Smith, John     Doe, Jane         15NOV2019
XXX1     Smith, John     Doe, Jane         13NOV2019
XXX1     Smith, John     Crane, Frasier   3NOV2019
;

proc sql;
create table want as
select a.*,count(b.date) as rank
from have a, have b
where a.id=b.id and a.date<=b.date
group by a.id,a.name,a.manager,a.date
order by id,rank;
quit;

proc print noobs;run;
ID Name Manager DATE rank
XXX1 Smith, John Doe, Jane 15NOV2019 1
XXX1 Smith, John Doe, Jane 13NOV2019 2
XXX1 Smith, John Crane, Frasier 03NOV2019 3

What about the case of ties, where two records have the same date? I'm not sure I want to use this code in the case of ties, as I don't like the results.

 

Of course, the original poster ought to weigh in on this, and specify if ties can happen, and what should happen in the case of ties. PROC RANK gives you many options to handle ties.

--
Paige Miller
novinosrin
Tourmaline | Level 20

Sir @PaigeMiller  True and agreed that Proc SQL is probably no robust nor best suited for the stated objective, however the ties can be handled with some extra logic,/syntax yet I admit not so neat like the solution suggested by you or Tom

 

So a simple "Distinct" in case of simple ties on dates, assuming OP's sample is a good representative of his/her real

select a.*,count(distinct b.date) as rank

alone will do. So,


data have;
input ID     $      Name     & $20.         Manager & $20. DATE:date9.;
format date date9.;
cards;
XXX1     Smith, John     Doe, Jane         15NOV2019
XXX1     Smith, John     Doe, Jane         15NOV2019
XXX1     Smith, John     Doe, Jane         13NOV2019
XXX1     Smith, John     Doe, Jane         13NOV2019
XXX1     Smith, John     Doe, Jane         13NOV2019
XXX1     Smith, John     Crane, Frasier   3NOV2019
XXX1     Smith, John     Crane, Frasier   3NOV2019
;

proc sql;
create table want as
select a.*,count(distinct b.date) as rank
from have a, have b
where a.id=b.id and a.date<=b.date
group by a.id,a.name,a.manager,a.date
order by id,rank;
quit;

proc print noobs;run;

 

 

 

 

Jyuen204
Obsidian | Level 7
i am assuming "have" is the table name you are using in reference to my TABLE1?
novinosrin
Tourmaline | Level 20

Hi @Jyuen204   HAVE is the input sample table/dataset you or other Original posters provide us with., WANT is the expected output or the target to accomplish. This is just how the forum questions are framed. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 21 replies
  • 11374 views
  • 3 likes
  • 6 in conversation