Help using Base SAS procedures

to find minimum of two date values?

Reply
Frequent Contributor
Posts: 110

to find minimum of two date values?

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?

Respected Advisor
Posts: 3,124

to find minimum of two date values?

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

Haikuo

Regular Contributor
Posts: 233

to find minimum of two date values?

PROC SQL;

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

QUIT;

Respected Advisor
Posts: 3,124

to find minimum of two date values?

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

Respected Advisor
Posts: 3,124

to find minimum of two date values?

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

PROC Star
Posts: 7,363

Re: to find minimum of two date values?

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

Respected Advisor
Posts: 3,124

Re: to find minimum of two date values?

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

Kindly Regards,

Haikuo

PROC Star
Posts: 7,363

Re: to find minimum of two date values?

Corrected it.  Thanks for pointing out my typo.

Super User
Posts: 9,681

Re: to find minimum of two date values?

Can you post some sample data.

Maybe you miss a group by clause.

Ksharp

Ask a Question
Discussion stats
  • 8 replies
  • 3270 views
  • 0 likes
  • 5 in conversation