02-25-2016 07:02 PM
Question about date formats in SAS.
My coworker brought up a point that she usually sees this format when we're coding with dates:
In order to make one code work, we used this which she assumed was a string:
datetime.20 was the format type
When do we use one over the other? And are dates always in quotes like a string?
02-25-2016 07:31 PM
This depend in the date variable format in your dataset. If it is saved as SAS Date, then when you need to compare it or assign value to it you can use the SAS Date constant 'ddmmyy'd or you can use the actual number that this date constat will be evaluate to by SAS.
If in your dataset the date variable is saved as char then you need to deal with this variable as a char without the d char at the end, when you compare it or assign value to it.
02-25-2016 08:10 PM
SAS store dates as the number of days and datetime values as the number of seconds.
You can use '01Jan2015'd or '01-Jan-2015'd as a date literal.
But to make a datetime literal you need to include the time part and also use DT instead of D.
Perhaps you can try '01JAN2015:00:00'DT.
02-25-2016 08:47 PM
For a date literal you must use what SAS recognises as a DATE format. You cannot use other formats like MMDDYY or YYMMDD. If you have string in that format then must use INPUT and the appropriate informat to convert it to a date.
str='2015-01-01'; date=input(str,yymmdd10.); format date date9.;
The format used to display a date value makes no difference in its value. Just like 1.5 is the same number as 1.500. So if you have one variable with the DATE9. format attached and another with the YYMMDD10. format attached you can compare the values in your code without worring about what they will look like when printed.
data _null_; d1='01JAN2015'd; d2='01-JAN-2015'd; format d1 date9. d2 yymmdd10.; diff = d1-d2; put diff= d1= d2 =; run; diff=0 d1=01JAN2015 d2=2015-01-01
But if your variable is actually a datetime instead of a date variable then the values are totally different. You can use the DATEPART() function to convert a datetime value to a date value.
02-26-2016 01:53 AM
To correctly write a date constant, it is obviously possible to use both methods. It's just that the method without dashes is the one usually used.
'dd-mmm-yyyy'd is equivalent to 'ddmmmyyyy'd.
How the resulting value is displayed depends on the format assigned to a variable or the format used in a put statement or function.