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:)
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1454 views
  • 1 like
  • 3 in conversation