BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

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 !

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.);

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
%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.

--
Paige Miller
SASdevAnneMarie
Barite | Level 11
Thank you,

I have the date 20240208T160242,
could I have the same date without T ?

Yes, I use the variable for the file name.
yabwon
Onyx | Level 15

I agree that 08022024 can be interpreted as August 2 or February 8, but no way for 20240208. 🙂

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

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.);
Tom
Super User Tom
Super User

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:-));

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 651 views
  • 7 likes
  • 5 in conversation