DATA Step, Macro, Functions and more

find the minmum of two dates that is not misssing

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 110
Accepted Solution

find the minmum of two dates that is not misssing

id       date_1           date_2              date_3
1    .                     02/02/2012     01/01/2001    
2     01/11/2009     2/10/2011     01/01/2004    
3     01/08/2013      .                      2/10/2012
4    11/10/2015     01/01/1998     26/12/2014

I am trying to the find the minimum of three dates but I have some missing information so I want to retain the minmum among the one that is avaiable:

output

1 01/01/2001

2 01/01/2004

3 02/10/2012

4 01/01/1998


Accepted Solutions
Solution
‎11-08-2016 08:34 AM
PROC Star
Posts: 551

Re: find the minmum of two dates that is not misssing

[ Edited ]

The MIN function already handles this issue? So the following will do

 

data have;
informat date_1 date_2 date_3 ddmmyy10.;
input id date_1 date_2 date_3;
format date_1 date_2 date_3 ddmmyy10.;
datalines;
1 .          02/02/2012 01/01/2001
2 01/11/2009 2/10/2011  01/01/2004
3 01/08/2013 .          02/10/2012
4 11/10/2015 01/01/1998 26/12/2014
;

data want;
   set have;

   newvar = min(date_1,date_2,date_3);
   format newvar  ddmmyy10.;
run;

 

View solution in original post


All Replies
Solution
‎11-08-2016 08:34 AM
PROC Star
Posts: 551

Re: find the minmum of two dates that is not misssing

[ Edited ]

The MIN function already handles this issue? So the following will do

 

data have;
informat date_1 date_2 date_3 ddmmyy10.;
input id date_1 date_2 date_3;
format date_1 date_2 date_3 ddmmyy10.;
datalines;
1 .          02/02/2012 01/01/2001
2 01/11/2009 2/10/2011  01/01/2004
3 01/08/2013 .          02/10/2012
4 11/10/2015 01/01/1998 26/12/2014
;

data want;
   set have;

   newvar = min(date_1,date_2,date_3);
   format newvar  ddmmyy10.;
run;

 

Super User
Super User
Posts: 7,401

Re: find the minmum of two dates that is not misssing

Just to add, functions like min() can also accept ranges of variables which can make typing much easier when you have lots of variables:

newvar=min(of date_:);

Assumes of course all variables have same prefix (or you setup and array of them before).

Frequent Contributor
Posts: 110

Re: find the minmum of two dates that is not misssing

Thank you! 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 206 views
  • 5 likes
  • 3 in conversation