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

Hi,

 

I have the following variable:

VariableTypeLenFormatInformat
DUEDATENum8DATETIME20.DATETIME20.

 

With the following query:

 

proc sql inobs=10;
select duedate from output.mydata;
quit;

 

I show 23MAY2018:12:10:56 in the first row. But when I query using the outputted data, no rows are returned.

 

proc sql ;
select * from output.mydata
where duedate = '23MAY2018:12:10:56'd;
quit;

 

Ultimately I want my where clause to be duedate between '01APR2019'd and '30APR2019'd, but right now it's not returning anything so I tried the above.  Please help.

1 ACCEPTED SOLUTION

Accepted Solutions
jffeudo86
Quartz | Level 8

Got it now, use datepart:

where datepart(duedate) = '23MAY2018'd.

View solution in original post

2 REPLIES 2
jffeudo86
Quartz | Level 8

Got it now, use datepart:

where datepart(duedate) = '23MAY2018'd.

Tom
Super User Tom
Super User

So you appear to have a DATETIME variable instead of a DATE variable.  Dates are stored as a number of days and datetime values as a number of seconds.

 

You also seem to have found a strange feature of SAS.  If you add a time part to the literal string in a DATE literal the time part is ignored when converting the string into an actual date value.  It doesn't even had to look like a time value, it can any extraneous text because SAS just ignores it once it has 9 characters to convert into a date value.

540  data test;
541   duedate = '23MAY2018:12:10:56'd;
542   put duedate= :comma15. ' ' duedate date9. ' ' duedate datetime20.;
543   duedate = '23MAY2018:blahblah'd;
544   put duedate= :comma15. ' ' duedate date9. ' ' duedate datetime20.;
545  run;

duedate=21,327  23MAY2018   01JAN1960:05:55:27
duedate=21,327  23MAY2018   01JAN1960:05:55:27

So if you want to make an actual DATETIME literal remember to use DT suffix after the quoted string and not just the letter D.

546
547  data test;
548   duedate = '23MAY2018:12:10:56'dt;
549   put duedate= :comma15. ' ' duedate date9. ' ' duedate datetime20.;
550  run;

duedate=1,842,696,656  *********   23MAY2018:12:10:56

Notice how the actual value of the DATETIME value is so much larger than the DATE value.  That is because there are 24*60*60=86,400 seconds in a single day.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 26637 views
  • 3 likes
  • 2 in conversation