BookmarkSubscribeRSS Feed
sassharp
Calcite | Level 5

I need to find minimum of two dates.

I know I can use min function.

q.what precautions do I need to follow?

Ex min(d1,d2)

q.d1,d2 should be in same format?

Here is the requirement.

proc sql;

create table l1

select

          termdate format=datetime21.

from t1;

quit;

proc sql;

select min(today()-1,termdate) as testdate format=datetime21.

from l1,l2;

where l1.id=l2.id;

quit;

termdate output                                              testdate

1)

31DEC9999:00:00:00                                     20FEB2012

2)

30APR2006:00:00:00                                    20FEB2012

Q) here there are no errors. But for two different termdate inputs I am getting the same output?

8 REPLIES 8
Haikuo
Onyx | Level 15

As long as they are date (numeric), format does not matter.

Haikuo

Hima
Obsidian | Level 7

PROC SQL;

SELECT CASE WHEN DATE1<DATE2 THEN DATE1 ELSE DATE2 END AS MIN_DATE FORMAT MMDDYY10. FROM TEST;

QUIT;

Haikuo
Onyx | Level 15

I think using min() as OP suggested is safer. '<' will capture missing values as the smaller date, which I suppose not OP's intention.

Kindly Regards,

Haikuo

Haikuo
Onyx | Level 15

Comparing datetime. and date directly will be missleading, saying that I would like to retreat my original comments on the format part. Try the following:

proc sql;

select min(today()-1,datepart(termdate)) as testdate format=datetime21.

from l1,l2;

where l1.id=l2.id;

quit;

Regards,

Haikuo

art297
Opal | Level 21

Or, if the time is also important,

select min(datetime()-(24*60*60),termdate) as testdate format=datetime21.

otherwise,make sure that the format is consistent

Haikuo
Onyx | Level 15

Considerably the best approach so far, Art! if ')' was added would be perfect!Smiley Wink

Kindly Regards,

Haikuo

art297
Opal | Level 21

Corrected it.  Thanks for pointing out my typo.

Ksharp
Super User

Can you post some sample data.

Maybe you miss a group by clause.

Ksharp

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 16481 views
  • 0 likes
  • 5 in conversation