Dates in SAS

Reply
Occasional Contributor
Posts: 6

Dates in SAS

Question about date formats in SAS.

 

My coworker brought up a point that she usually sees this format when we're coding with dates:

'01Jan2015'd

 

In order to make one code work, we used this which she assumed was a string:

'01-Jan-2015'

datetime.20 was the format type

 

When do we use one over the other? And are dates always in quotes like a string?

 

Thanks.

Super Contributor
Posts: 490

Re: Dates in SAS

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.

 

 

Occasional Contributor
Posts: 6

Re: Dates in SAS

I don't believe it's a character though. When using left join, we're able to use greater than/less than to set date parameters
Super User
Super User
Posts: 6,502

Re: Dates in SAS

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.

 

Occasional Contributor
Posts: 6

Re: Dates in SAS

So there's no difference between with and without dashes in SAS?
Super User
Super User
Posts: 6,502

Re: Dates in SAS

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.

 

Super User
Posts: 6,946

Re: Dates in SAS

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 6 replies
  • 419 views
  • 0 likes
  • 4 in conversation