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

This is easy but I am struggling with it.

 

Field t1.TrnDt is a date but the following syntax gives an error.  I am try to create a new computed column.  What's wrong with this?

 

 

CASE  WHEN   t1.TrnDt >  "01/01/2011" THEN "Late"  ELSE  "Early"   END

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

SAS requires literal dates in the form of ddMONyy or ddMONyyyy, in quotes and followed by D to indicate date

 

So 

CASE  WHEN   t1.TrnDt >  '01Jan2011'd ....

You can use '1Jan2011'd but I generally use the 01Jan so I know I didn't actually mean 10Jan and just missed a digit.

 

There are similar rules for Time or DATETIME literals.

View solution in original post

5 REPLIES 5
ballardw
Super User

SAS requires literal dates in the form of ddMONyy or ddMONyyyy, in quotes and followed by D to indicate date

 

So 

CASE  WHEN   t1.TrnDt >  '01Jan2011'd ....

You can use '1Jan2011'd but I generally use the 01Jan so I know I didn't actually mean 10Jan and just missed a digit.

 

There are similar rules for Time or DATETIME literals.

PadraigODea
Fluorite | Level 6

Easy when you know how !!  Thanks again.

PadraigODea
Fluorite | Level 6

Aaagh!  I spoke too soon.

 

The following date (cut and pasted from SAS)

 

t1.TrnDt = 12JUN2007:00:00:00.000

 

The above date gives an "After" result to the following calculated field;

CASE WHEN  t1.TrnDt >=  '01Jan2011'd      THEN "After" ELSE  "Before" END

 

But surely 2007 is BEFORE 2011 ???   Why does my case statement fail??

ballardw
Super User

The value 12JUN2007:00:00:00.000 is a DATETIME value. SAS datetimes are counts of seconds and SAS dates are counts of days.

So either use something like "12JUN2007:00:00:00"DT OR compare the datetpart of the value to literal, which would probably be your intention:

 

CASE WHEN  datepart(t1.TrnDt)  >=  '01Jan2011'd      

one of my pet peeves are programmers that call datetime values dates or follow what seems to be a Microslop approach of defaulting things to datetime when you actually want dates.

 

PadraigODea
Fluorite | Level 6
Thanks - That's it. Much appreciated.

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
  • 5 replies
  • 20143 views
  • 3 likes
  • 2 in conversation