So it IS a datetime value, not a date. You need to use DATEPART to extract the date from it:
data have;
input date_column datetime19.;
format date_column dtdate9.;
datalines;
01sep2021:01:02:03
01oct2021:04:05:06
;
proc contents data=have;
run;
proc sql;
select *
from have
where datepart(date_column) = intnx('month',today(),-1,'b');
quit;
Partial result:
# Variable Typ Länge Ausg.Format 1 date_column Num 8 DTDATE9. date_column 01SEP2021
I'll take a guess that the values 01SEP2021 are not numeric (they are not datetime as you said, they are possibly dates if they are numeric).
If that's not it, then we would need to see a portion of your data as a SAS data step (instructions) and the full SQL code.
Always best to handle dates as numeric. However, I don't think you have explained enough for me to advise actual code. And, as I requested, it would be helpful if you provided a portion of your data in the requested format (and not in any other format).
A date value is a date value, regardless of the format applied.
But we MUST now know what we are dealing with. Run PROC CONTENTS on your dataset, and post the line from the output that describes your date column.
If you have the MONYY format in use, you will not see the actual dates; change the format to yymmdd10. and see what you get.
Since you mentioned a datetime once in your initial post, also make sure that you do not have fractions involved; Use the FLOOR function to convert a possible date with fractions to an integer.
So it IS a datetime value, not a date. You need to use DATEPART to extract the date from it:
data have;
input date_column datetime19.;
format date_column dtdate9.;
datalines;
01sep2021:01:02:03
01oct2021:04:05:06
;
proc contents data=have;
run;
proc sql;
select *
from have
where datepart(date_column) = intnx('month',today(),-1,'b');
quit;
Partial result:
# Variable Typ Länge Ausg.Format 1 date_column Num 8 DTDATE9. date_column 01SEP2021
Maxim 3 says Know Your Data, and there is a reason why it is so high up on the list 😉
Being fluent in data types, especially in the way SAS deals with dates and types, is a must.
If you use this
WHERE date_column = intnx(‘month’, today(), -1, ‘same’);
today (2021-10-15), it will look for date_column values with a date of 2021-09-15, and not 2021-09-01. If you want the first day of the previous month, use "b" as the fourth parameter of the INTNX function.
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.