Hello Experts,
I use %sysfunc(today(), yymmddn8.) for creating the macro variable.
Do you know please how to get the dates in format :
20240208115859 and 2024020811 ?
Thank you !
Use COMPRESS:
%let dt =%sysfunc(compress(%sysfunc(datetime(),b8601dt19.),T));
or roll your own PICTURE format with PROC FORMAT.
Or build it piecemeal:
%let dt = %sysfunc(today(),yymmddn8.)%sysfunc(time(),b8601tm6.);
%let dt = %sysfunc(datetime(),b8601dt.);
%put &=dt;
Of course, if you don't like the b8601dt. format, pick another one that works better for you. Or modify &dt, it's just text at this point.
CAUTION: formatting macro variables like this is normally not required and just extra work in most cases. If this macro variable is going to be used for arithmetic or logical operations, no need to format it (in fact, formatting it in these cases would not produce the desired results). Unless this macro variable is going to be used in a title or label or file name (such as a title that says "Data as of &dt"), don't bother formatting macro variables.
CAUTION 2: 20240208115859 can be interpreted as August 2 or February 8, its never clear which and can be confusing. The datetime19. format eliminates this possible confusion. But it depends on what you are going to do with this macro variable.
So that's twice I mention that it depends on what you are going to do with this macro variable. Please tell us how it will be used. Context is everything, and you have given us no context.
I agree that 08022024 can be interpreted as August 2 or February 8, but no way for 20240208. 🙂
Bart
Use COMPRESS:
%let dt =%sysfunc(compress(%sysfunc(datetime(),b8601dt19.),T));
or roll your own PICTURE format with PROC FORMAT.
Or build it piecemeal:
%let dt = %sysfunc(today(),yymmddn8.)%sysfunc(time(),b8601tm6.);
As your subject line makes clear those last two strings are not DATE values. They are DATETIME values.
Use the DATETIME() function to get the current datetime.
%let dt=%sysfunc(datetime());
Or use some other datetime value you have available, such as when the current SAS session started which is recorded in SYSDATE9 and SYSTIME macro variables.
%let dt="&sysdate9:&systime"dt;
Now you can use a number of methods to build your digit string.
You might use a format that has the right digits in the right order and then remove the punctuation it uses to make the values human readable.
%let dtstring = %sysfunc(compress(%sysfunc(putn(&dt,e8601dt19.)),T:-));
Or perhaps replace them with some character like _ that is valid in a filename.
%let dtstring = %sysfunc(translate(%sysfunc(putn(&dt,e8601dt19.)),___,T:-));
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.