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

I want to combine outputs of sysdate and systime

and get them in below format. I want the output to be stored in macro variable

 

DDMMYYYY_HH:MM:SS

 

I tried doing like this but it is not working

 

%let dt=%sysfunc(put(&sysdate9,ddmmyy8.))||"_"||%sysfunc(put(&systime,tod8.));

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You don't need to do much to combine macro variable values.  Just put the reference where you want the value to appear.

%let dt=&sysdate9._&systime.;

Example:

1    %put dt=&sysdate9._&systime.;
dt=21AUG2023_22:20

The reason your code failed is you tried to run code like:

%let dt=%sysfunc(put(&sysdate9,ddmmyy8.))||"_"||%sysfunc(put(&systime,tod8.));

First issue is that %SYSFUNC() cannot call the PUT() function (or the INPUT() function).  You can only use the type specific functions of PUTN() or PUTC() and INPUTN() or INPUTC().

 

Second issue is that the PUTN() function needs a NUMERIC VALUE.  But you gave it the STRING value 21AUG2023 instead.  If you want to have SAS treat that string as a date you need to add quotes and the letter D to convert it into a date literal instead of just a string in the style generated by the DATE format.  Similarly for the time value.

 

Third issue is I don't think you really want to put double quote characters and pipe characters into the value of your new macro variable.  Instead I suspect you just wanted the underscore character.

 

And finally I doubt you want to use only TWO digits for the YEAR value. So either use 10 for the width of the date format. Or use a format that does not use a delimiter so that 8 characters is enough to include the century number.

 

And and it is best to avoid MDY or DMY ordering for date value. Whichever one you pick will cause confusion for half of your audience.  Was that July eight or the seventh of August?

6    %put dt=%sysfunc(putn("&sysdate9"d,yymmddn8.))_%sysfunc(putn("&systime"t,tod8.));
dt=20230821_22:20:00

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You don't need to do much to combine macro variable values.  Just put the reference where you want the value to appear.

%let dt=&sysdate9._&systime.;

Example:

1    %put dt=&sysdate9._&systime.;
dt=21AUG2023_22:20

The reason your code failed is you tried to run code like:

%let dt=%sysfunc(put(&sysdate9,ddmmyy8.))||"_"||%sysfunc(put(&systime,tod8.));

First issue is that %SYSFUNC() cannot call the PUT() function (or the INPUT() function).  You can only use the type specific functions of PUTN() or PUTC() and INPUTN() or INPUTC().

 

Second issue is that the PUTN() function needs a NUMERIC VALUE.  But you gave it the STRING value 21AUG2023 instead.  If you want to have SAS treat that string as a date you need to add quotes and the letter D to convert it into a date literal instead of just a string in the style generated by the DATE format.  Similarly for the time value.

 

Third issue is I don't think you really want to put double quote characters and pipe characters into the value of your new macro variable.  Instead I suspect you just wanted the underscore character.

 

And finally I doubt you want to use only TWO digits for the YEAR value. So either use 10 for the width of the date format. Or use a format that does not use a delimiter so that 8 characters is enough to include the century number.

 

And and it is best to avoid MDY or DMY ordering for date value. Whichever one you pick will cause confusion for half of your audience.  Was that July eight or the seventh of August?

6    %put dt=%sysfunc(putn("&sysdate9"d,yymmddn8.))_%sysfunc(putn("&systime"t,tod8.));
dt=20230821_22:20:00

 

whymath
Lapis Lazuli | Level 10

I think you are looking for

%let dt=%sysfunc(date(),ddmmyyn8.)_%sysfunc(time(),tod8.);

You can test it with

%put &=dt;

The output will be

DT=22082023_11:18:21

There are several points you may want to know:

1. put() function is not supported to be used in %sysfunc(), the replacement is putn() and putc() function;

2. the macro function %sysfunc() has the second arguement, which allows us to write a format name here then %sysfunc() would return formatted value as we specified;

3. the syntax is kind of different between macro language and data step language, connecting character can be very simple in macro language;

4. ddmmyy8. will format date with slash, like 22/08/23, ddmmyyn8. will format date without slash, like 22082023;

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
  • 2 replies
  • 492 views
  • 1 like
  • 3 in conversation