BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Cheesiepoof05
Obsidian | Level 7
I'm trying to limit some results to within a certain date range and struggling with the syntax of how the date should be entered in my WHERE statement.  The field is "datetime" format at the source database with numeric precision of 23.  Looks like this "2022-08-05 12:35:08.637".  Any ideas with how the date should be entered in my where statement?
 
PROC SQL;
   CREATE TABLE WORK.Results AS 
   SELECT
Fruit,
Date
 
      FROM Database.Table
 
WHERE  Date >= 2022-08-05 12:35:08.637 and <= 2022-08-06 12:35:08.637
;
QUIT;
 
 
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Cheesiepoof05 wrote:

I should have included this in my original question, but I don't care about the time portion of it.  I would prefer to just use the date portion in my where statement and capture all the various times within that date.  Is that possible?


In that case you could use the DATEPART() function and date literals.

where datepart(date) between '05AUG2022'd and "06AUG2022"d

But if the source is actually some external database then for performance reasons you might need to still need to use datettime values.  Otherwise SAS might try to pull the whole database over to SAS so it can use the DATEPART() function.

where '05AUG2022:00:00'dt <= date < "07AUG2022:00:00"dt

Notice how I used midnight on the next day for the upper bound and use < instead of <= for the comparison test.  That will make sure that timestamps that are milliseconds before midnight are included.

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

No idea what a numerical precision of 23 means with a datetime value.

Are those milliseconds in your example time points really significant?  That seems a little bit too much to ask for.

 

To enter a datetime constant it has to be something the DATETIME informat can interpret.  You then enclose it in quotes and adds the letters DT after the closing quote.  That is a datetime literal in SAS.

 

Your WHERE clause also has to use valid syntax.  You cannot have the AND operator next to the LESS THAN OR EQUAL TO operator. 

 

Perhaps you just want to use the BETWEEN syntax. 

WHERE Date between "05AUG2022:12:35:08.637"dt and "06AUG2022:12:35:08.637"dt

Note you can also use BETWEEN in a normal WHERE statements outside of PROC SQL.  That way you can skip trying use PROC SQL and just use normal SAS code.

data Results;
  set Database.Table(Keep=fruit date);
  WHERE Date between "05AUG2022:12:35:08.637"dt and "06AUG2022:12:35:08.637"dt;
run;

 

mkeintz
PROC Star

Apparently your DATE variable takes datetime values (i.e. number of seconds after Midnight at the start of Jan 1, 1960).  So if you want to express a constant for that variable you have to use the datetime-literal format.

 

I.e. your where clause should be

  where date >= '05aug2022:12:35:08.637'dt
  and
        date <= '06aug2022:12:35:08.637'dt;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Cheesiepoof05
Obsidian | Level 7

I should have included this in my original question, but I don't care about the time portion of it.  I would prefer to just use the date portion in my where statement and capture all the various times within that date.  Is that possible?

PaigeMiller
Diamond | Level 26

If variable named DATE is actually a SAS date value, and not (as we assumed) a SAS date/time value, then yes of course you can do this.

 

But they key question is about whether or not the variable named DATE is a date value or a date/time value. Which is it?

--
Paige Miller
Tom
Super User Tom
Super User

@Cheesiepoof05 wrote:

I should have included this in my original question, but I don't care about the time portion of it.  I would prefer to just use the date portion in my where statement and capture all the various times within that date.  Is that possible?


In that case you could use the DATEPART() function and date literals.

where datepart(date) between '05AUG2022'd and "06AUG2022"d

But if the source is actually some external database then for performance reasons you might need to still need to use datettime values.  Otherwise SAS might try to pull the whole database over to SAS so it can use the DATEPART() function.

where '05AUG2022:00:00'dt <= date < "07AUG2022:00:00"dt

Notice how I used midnight on the next day for the upper bound and use < instead of <= for the comparison test.  That will make sure that timestamps that are milliseconds before midnight are included.

Cheesiepoof05
Obsidian | Level 7

It is a date/time value, not just a date value.

 

I tried both methodologies and timed the return time.  They both worked and returned the same results but the first methodology returned in half the time.

 

1 - where DATE between '05JAN2023 00:00:00'dt and '05MAY2023 23:59:59'dt
2 - where datepart(DATE) between '05JAN2023'd and '05MAY2023'd
 
I appreciate all of the help everyone has provided!  As I'm still very new to SAS (and obviously using more SQL rather than SAS), anyone have a recommendation on a good learning course for beginners to build up my skillset?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 488 views
  • 3 likes
  • 4 in conversation