BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
inyli
Calcite | Level 5

Hi, I'm having a question regarding how to enumerate unique date by group - id, see below:

 

id.       date           rank

123.   20190101.    1

123.   20190101.    1

123.   20190102.    2 

123    20190102.    2

 

how do I get the rank column? 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input id       date  :yymmdd8.; 
format date yymmdd10.; 
cards;
123   20190101    1
123   20190101    1
123   20190102    2 
123    20190102    2
;

proc rank data=have out=ranked ties=dense ; 
 by id;
 var date;
 ranks ranked_date;
run;

 

 

123 2019-01-01 1
123 2019-01-01 1
123 2019-01-02 2
123 2019-01-02 2

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20
data have;
input id       date  :yymmdd8.; 
format date yymmdd10.; 
cards;
123   20190101    1
123   20190101    1
123   20190102    2 
123    20190102    2
;

proc rank data=have out=ranked ties=dense ; 
 by id;
 var date;
 ranks ranked_date;
run;

 

 

123 2019-01-01 1
123 2019-01-01 1
123 2019-01-02 2
123 2019-01-02 2

inyli
Calcite | Level 5
Thanks!!!!
novinosrin
Tourmaline | Level 20

Alternatively, since it is sweetly sorted, you could have some fun like

 



data have;
input id       date  :yymmdd8.; 
format date yymmdd10.; 
cards;
123   20190101    1
123   20190101    1
123   20190102    2 
123    20190102    2
;

data want_ranked;
 do _n_=1 by 1 until(last.id);
  do until(last.date);
   set have;
    by id date;
	ranked_date=_n_;
	output;
  end;
 end;
run;

/*Or*/

data want_ranked;
 set have;
 by id date;
 if first.id then ranked_date=1;
 else if first.date then ranked_date+1;
run;
proc sql;
create table want_ranked as
select  a.* ,count(distinct  b.date) as ranked_date
from have a inner join (select distinct id,date from have) b
on a.id=b.id and b.date<=a.date	
group by a.id,a.date
having max(b.date)=b.date
order by a.id,a.date;
quit;

 

ed_sas_member
Meteorite | Level 14

Hi @inyli 

 

data want;
	set have;
	by id date;
	if first.id then flag = 0;
	if first.date then flag + 1;
run;
inyli
Calcite | Level 5
Thank you! It works:)

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 756 views
  • 1 like
  • 3 in conversation