BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

Hello all,

I have, what I think is a simple of piece of code that I am asking for a simple date subsititution during a case statement.

In essence, if the main date (DISCHDT) field is blank it goes to another table (DISCHARGE_DATE_ACTUAL) and retrieves from there. If that is blank, then I want today's date-

Code is:

CASE WHEN A.DischDt NE . THEN A.DischDt

     WHEN A.DischDt=. THEN DATEPART(DIS.DISCHARGE_DATE_ACTUAL)

     ELSE TODAY() end as discharge format MMDDYY10.

The problem is that it will not substitute today's date when both fields are blank. I have tried different variations to no effect.

Any help grateful.

Thanks & have a great labor day.

~LB

1 ACCEPTED SOLUTION

Accepted Solutions
Hobbes
Calcite | Level 5

Have you checked the coalesce function. In your case, something like:

coalesce(A.DischDt, DATEPART(DIS.DISCHARGE_DATE_ACTUAL), TODAY()) as discharge

will do the job.

View solution in original post

3 REPLIES 3
Hobbes
Calcite | Level 5

Have you checked the coalesce function. In your case, something like:

coalesce(A.DischDt, DATEPART(DIS.DISCHARGE_DATE_ACTUAL), TODAY()) as discharge

will do the job.

_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

Thanks Hobbes! Worked beautifully!

Howles
Quartz | Level 8

Logic is incorrect. The two WHEN branches exhaust all possibilities, so the ELSE branch can never be triggered. The second WHEN branch of the CASE should test the alternate date; something like this ...

WHEN  DIS.DISCHARGE_DATE_ACTUAL NE . THEN DATEPART(DIS.DISCHARGE_DATE_ACTUAL)

~LB wrote:

Hello all,

I have, what I think is a simple of piece of code that I am asking for a simple date subsititution during a case statement.

In essence, if the main date (DISCHDT) field is blank it goes to another table (DISCHARGE_DATE_ACTUAL) and retrieves from there. If that is blank, then I want today's date-

Code is:

CASE WHEN A.DischDt NE . THEN A.DischDt

     WHEN A.DischDt=. THEN DATEPART(DIS.DISCHARGE_DATE_ACTUAL)

     ELSE TODAY() end as discharge format MMDDYY10.

The problem is that it will not substitute today's date when both fields are blank. I have tried different variations to no effect.

Any help grateful.

Thanks & have a great labor day.

~LB

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!

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.

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
  • 3 replies
  • 2532 views
  • 0 likes
  • 3 in conversation