@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.
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;
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;
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?
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?
@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.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.