BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10

I have the following data 

id  dx_dt   rx_dt 
1  1/1/2005  
2  3/4/2005  
2  4/4/2006 
2  5/5/2007       

I want to find the second minimum date so the output I am looking for id 2 is

4/4/2006 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

And is dx_dt an actual SAS numeric date?  And what is rx_dt?  This is where posting test data in the form of a datastep helps avoid these basic questions which make us guess.  So my guess is based on dx_dt being SAS numeric date.

proc sort data=have out=want;
  by id descending dx_dt;
run;
data want;
  set want;
  retain cnt;
  by id;
  cnt=ifn(first.id,1,cnt+1);
  if cnt=2 then output;
run;
  

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

And is dx_dt an actual SAS numeric date?  And what is rx_dt?  This is where posting test data in the form of a datastep helps avoid these basic questions which make us guess.  So my guess is based on dx_dt being SAS numeric date.

proc sort data=have out=want;
  by id descending dx_dt;
run;
data want;
  set want;
  retain cnt;
  by id;
  cnt=ifn(first.id,1,cnt+1);
  if cnt=2 then output;
run;
  
lillymaginta
Lapis Lazuli | Level 10

Thank you both! 

Ksharp
Super User
data have;
input id  dx_dt : mmddyy10.;
format dx_dt mmddyy10.;
cards;
1  1/1/2005  
2  3/4/2005  
2  4/4/2006 
2  5/5/2007 
;
run;
proc sql;
create table want as
 select *
  from (
select * from have group by id having dx_dt ne max(dx_dt)
)
group by id
having dx_dt = max(dx_dt);
quit;
novinosrin
Tourmaline | Level 20
data have;
input id  dx_dt :mmddyy10.  ; 
format dx_dt mmddyy10. ;
cards;
1  1/1/2005  
2  3/4/2005  
2  4/4/2006 
2  5/5/2007 
;

proc rank data=have out=want(where=(dx_dt_rank=2)) ties=low ;
    by id;
  var dx_dt;
   ranks dx_dt_rank;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 3145 views
  • 1 like
  • 4 in conversation