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
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.
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.
Easy when you know how !! Thanks again.
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??
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.