I want to display the datetime value as character as shown in format below. I tried with datetime16.format like
put(datetime(),datetime16.) but it's not producing the desired results. Any leads?
2021-05-01T22:04:42Z
@David_Billa wrote:
I want to display the datetime value as character as shown in format below. I tried with datetime16.format like
put(datetime(),datetime16.) but it's not producing the desired results. Any leads?
2021-05-01T22:04:42Z
In SAS formats convert values into text and informats convert text into values.
In the message you posted DATETIME is the only FORMAT that you mentioned. But the DATETIME format does not display dates in the style of the text you are showing. It uses the DATE format style for the date part and then appends the time part with a colon separator. And if you use a width of only 16 then it will only have room for two digits for the year part.
1219 data test; 1220 have=dhms(mdy(5,1,2021),22,04,42); 1221 put 'COMMA20. : ' have comma20. 1222 / 'DATETIME16.: ' have datetime16. 1223 / 'datetime19.: ' have datetime19. 1224 / 'E8601DZ20. : ' have E8601DZ20. 1225 ; 1226 run; COMMA20. : 1,935,525,882 DATETIME16.: 01MAY21:22:04:42 datetime19.: 01MAY2021:22:04:42 E8601DZ20. : 2021-05-01T22:04:42Z
If your source data is actual a character variable, instead of a numeric variable with a datetime value (number of seconds since 1960) then you will first need to convert the string into a datetime value. You can do that by using an INFORMAT with the INPUT() function. You will need to create a new numeric variable to store the value since you cannot store a number into a character variable. You might use the PUT() function with appropriate format to convert the datetime value back to a string, but if you already have it as a string in the style you want what is it that you want to change?
Have you tried E8601DZw.d?
data _null_;
x=put(datetime(),E8601DZ20.);
put x=;
run;
Yields the below in my environment (9.4 M6)
x=2021-07-29T12:11:29Z
If you want the actual offset, you would just extend the width of the format. For example:
data _null_;
x=put(datetime(),E8601DZ27.);
put x=;
run;
Yields:
x=2021-07-29T12:20:44+00:00
Jim
> If I want to display value with timezone(Z) then which format to use? I'm unable to find the right ISO 8601 format.
At the top of the page linked:
Z |
indicates that the time value is the time in Greenwich, England, or UTC time. |
> It's not producing the desired results.
How?
How can you say that when @jimbarbour gave you the full exact code to exactly match the output you requested?
@David_Billa wrote:
I have already tried with B8601dz. and E8601dz formats. It's not producing
the desired results.
SHOW.YOUR.CODE.AND.LOG.
And the "undesired" results.
@David_Billa wrote:
I want to display the datetime value as character as shown in format below. I tried with datetime16.format like
put(datetime(),datetime16.) but it's not producing the desired results. Any leads?
2021-05-01T22:04:42Z
In SAS formats convert values into text and informats convert text into values.
In the message you posted DATETIME is the only FORMAT that you mentioned. But the DATETIME format does not display dates in the style of the text you are showing. It uses the DATE format style for the date part and then appends the time part with a colon separator. And if you use a width of only 16 then it will only have room for two digits for the year part.
1219 data test; 1220 have=dhms(mdy(5,1,2021),22,04,42); 1221 put 'COMMA20. : ' have comma20. 1222 / 'DATETIME16.: ' have datetime16. 1223 / 'datetime19.: ' have datetime19. 1224 / 'E8601DZ20. : ' have E8601DZ20. 1225 ; 1226 run; COMMA20. : 1,935,525,882 DATETIME16.: 01MAY21:22:04:42 datetime19.: 01MAY2021:22:04:42 E8601DZ20. : 2021-05-01T22:04:42Z
If your source data is actual a character variable, instead of a numeric variable with a datetime value (number of seconds since 1960) then you will first need to convert the string into a datetime value. You can do that by using an INFORMAT with the INPUT() function. You will need to create a new numeric variable to store the value since you cannot store a number into a character variable. You might use the PUT() function with appropriate format to convert the datetime value back to a string, but if you already have it as a string in the style you want what is it that you want to change?
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.