%let datetime = %sysfunc(datetime(), datetime19.); /* Get current datetime */
/* Format datetime to "YYYYMMDDHHMM" format */
%let currdate = %sysfunc(inputn(&datetime, yymmddn8.6));
%put &currdate; /* Print the value of the macro variable */
Currdate is resolving to missing value with below warning . How to correct this and get the datetime value in "YYYYMMDDHHMM" format
Argument 2 to function inputn referenced by %sysfunc or %qsysfunc macro function is out of range.mathematical operations could not be performed during %sysfunc function execution.result of operations have been set to a missing value
Just don't remove the colons.
1847 %let e8601dt=%sysfunc(datetime(),e8601dt.); 1848 %put &=e8601dt; E8601DT=2023-08-19T11:40:05 1849 %let want=%sysfunc(compress(%sysfunc(datetime(),e8601dt.),-T)); 1850 %put &=want; WANT=2023081911:40:05
But with no delimiter at all between the days and the hours it would be very difficult for humans to see that as a datetime string.
Perhaps you want a colon there?
1853 %let want=%sysfunc(translate(%sysfunc(compress(%sysfunc(datetime(),e8601dt.),-)),:,T));
1854 %put &=want;
WANT=20230819:11:44:57
It might be clearer to build the string up from the datetime value instead.
1855 %let now=%sysfunc(datetime());
1856 %let want=%sysfunc(datepart(&now),yymmddn8.):%sysfunc(timepart(&now),tod8.);
1857 %put &=want;
WANT=20230819:11:47:11
It always helps to LOOK AT the values of these macro variables.
%let datetime = %sysfunc(datetime(), datetime19.); /* Get current datetime */
%put &=datetime;
In the log, you will see
DATETIME=17AUG2023:10:31:35
so this is not in yymmddn8.6 format, and so by creating CURRDATE with the YYMMDDN8.6 format, you get nothing.
If you want a specific output format, do NOT first assign a different format to it and then convert to another format, that makes no sense. Right at the start, assign the format you want it to have.
%let currdate= %sysfunc(datetime(), b8601dt.); /* Get current datetime */
%put &=currdate;
which produces this
CURRTIME=20230817T105143
Thanks for the prompt reply.
How do I get T removed from the output and get the current date and time as YYYYMMDDHH:MM:SS
@Sathya3 wrote:
How do I get T removed from the output and get the current date and time as YYYYMMDDHH:MM:SS
It's just a text string, you can use the COMPRESS function to remove the letter T. But really, as discussed by others, use an ISO compliant format, rather than creating your own, that will be better for you and for everyone else who might have to use this data.
Why not use the same technique you use to create &DATETIME?
24 %let currdate = %sysfunc(datetime(),B8601DT19);
25 %put NOTE: &=currdate;
NOTE: CURRDATE=20230817T074257
Your macro variables look like they are properly resolved.
It is your CODE that is wrong.
There is no YYMMDDn8.6 informat. So you are saying to read 8 characters and imply a decimal point between the 3 and 4th character? That leaves only 2 digits for the integer part of the value. How would that have anything to do with a DATE (or even a DATETIME) value? And it definitely does not match the string in DDMONYY:HH:MM:SS style that you generated in the first statement.
Do you want a DATETIME value? Then just use :%sysfunc(datetime()) which will return the number of seconds since start of 1960. Which is what a DATETIME value is.
213 %let datetime = %sysfunc(datetime()); 214 %put &=datetime; DATETIME=2007888574.838
Now what do you want to do with this number? If you want to use it in calculations with other datetime values it works fine.
220 %let datetime = %sysfunc(datetime()); 221 %put Hours since start of 2023-08-16 = %sysfunc(intck(hour,"16AUG2023:00:00"dt,&datetime)); Hours since start of 2023-08-16 = 34
Do you want a STRING in YYMMDDHHMMSS style?
224 %let datetime = %sysfunc(compress(%sysfunc(datetime(),e8601dt.),-T:)); 225 %put &=datetime; DATETIME=20230817105548
But what are you going to do with that STRING?
You cannot use it as a datetime value since it is totally different (and many order of magnitude different) than the actual datetime value:
242 %let datetime = %sysfunc(datetime()); 243 %let ymdhs = %sysfunc(compress(%sysfunc(putn(&datetime,e8601dt.)),-T:)); 244 %put datetime = %sysfunc(putn(&datetime,comma22.)); datetime = 2,007,889,322 245 %put ymdhs = %sysfunc(putn(&ymdhs,comma22.)); ymdhs = 20,230,817,110,202
Thanks for the prompt reply.
How do I get the current date and time as YYYYMMDDHH:MM:SS
For this particular display, you need to roll your own format:
proc format;
picture mydt
low-high = "%Y%0m%0d%0H:%0M:%0S" (datatype=datetime)
;
run;
%let now = %sysfunc(datetime(),mydt.);
But I would recommend to use ISO standard formatting, either basic (B8601DT) or extended (E8601DT).
@Sathya3 wrote:
Thanks for the prompt reply.
How do I get the current date and time as YYYYMMDDHH:MM:SS
@Kurt_Bremser shared already how to create this custom format. Question is why would you do this and not use a ISO compliant format.
Just don't remove the colons.
1847 %let e8601dt=%sysfunc(datetime(),e8601dt.); 1848 %put &=e8601dt; E8601DT=2023-08-19T11:40:05 1849 %let want=%sysfunc(compress(%sysfunc(datetime(),e8601dt.),-T)); 1850 %put &=want; WANT=2023081911:40:05
But with no delimiter at all between the days and the hours it would be very difficult for humans to see that as a datetime string.
Perhaps you want a colon there?
1853 %let want=%sysfunc(translate(%sysfunc(compress(%sysfunc(datetime(),e8601dt.),-)),:,T));
1854 %put &=want;
WANT=20230819:11:44:57
It might be clearer to build the string up from the datetime value instead.
1855 %let now=%sysfunc(datetime());
1856 %let want=%sysfunc(datepart(&now),yymmddn8.):%sysfunc(timepart(&now),tod8.);
1857 %put &=want;
WANT=20230819:11:47:11
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!
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.