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

How do I create a date field with a value using a CASE WHEN statement?  Here is my statement:

 

CASE WHEN t1.year = 24 then '01MAY2015:00:00:00'

END

1 ACCEPTED SOLUTION

Accepted Solutions
carmendee
Obsidian | Level 7
Problem - the date returned is today's date. Statement:
CASE WHEN t1.SCHOOL_YEAR_ID = 18 THEN DATETIME('01MAY2015:00:00:00'DT)
END

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

You probably want to use a date (or as in your example a datetime) literal. If you want a DATE then use '01MAY2015'D if you want a DATETIME value then use '01MAY2015:00:00:00'DT. You could also use a function call such as MDY(5,1,2015). You should also attach a format to the variable so the value will be displayed properly.

 

PROC SQL; 
CREATE TABLE WANT AS
SELECT *
, CASE WHEN t1.year = 24 THEN '01MAY2015:00:00:00'dt
END AS DATETIME FORMAT=DATETIME20.
FROM HAVE
;

 

carmendee
Obsidian | Level 7
Problem - the date returned is today's date. Statement:
CASE WHEN t1.SCHOOL_YEAR_ID = 18 THEN DATETIME('01MAY2015:00:00:00'DT)
END
Tom
Super User Tom
Super User

DATETIME() function just returns the current date time.  Use just the literal or another function such as INPUTN().

carmendee
Obsidian | Level 7
Oops. Removed my DATETIME and reran. Thanks Tom.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 4 replies
  • 1775 views
  • 0 likes
  • 2 in conversation