If you want to use the value in SAS code then don't bother to format it. Just let %SYSFUNC() generate the number that SAS uses to store that datetime value.
%let datetime = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1));
If you want the value to be something a human can understand then you can convert it to a datetime literal by enclosing it in quotes and appending the letters dt.
%let datetime = "%sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1),datetime19.)"dt;
Example:
1 %let datetime1 = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1));
2 %let datetime2 = "%sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1),datetime19.)"dt;
3 %put &=datetime1 &=datetime2;
DATETIME1=2038089600 DATETIME2=" 01AUG2024:00:00:00"dt
4 data test;
5 datetime1 = &datetime1;
6 datetime2 = &datetime2;
7 if datetime1=datetime2 then put 'SAME VALUE';
8 else put 'DIFFERENT VALUE';
9 run;
SAME VALUE
NOTE: The data set WORK.TEST has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Note: The DATETIME format has a bug. If you attempt to use DATETIME18. you will get only 2 digits for the year.
... View more