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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register 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.

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