BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

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
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@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?

View solution in original post

10 REPLIES 10
Ksharp
Super User
data _null_;
x=put(datetime(),e8601dt.);
put x=;
run;
David_Billa
Rhodochrosite | Level 12

@Ksharp @ChrisNZ If I want to display value with timezone(Z) then which format to use? I'm unable to find the right ISO 8601 format.

Desired value is,

 

2021-05-01T22:04:42Z
jimbarbour
Meteorite | Level 14

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

ChrisNZ
Tourmaline | Level 20

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

David_Billa
Rhodochrosite | Level 12
I have already tried with B8601dz. and E8601dz formats. It's not producing
the desired results.
ChrisNZ
Tourmaline | Level 20

> 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?

 

jimbarbour
Meteorite | Level 14
Hmmm. That's the correct Format. I'm scratching my head here. I don't understand why it's not working.

Can you show us the code, the results your getting, and the log?

What are your locale settings? Maybe your locale setting is altering how the format works.

What width are you specifying with the format? Maybe try at least 27, maybe more if you want fractions of seconds.

Jim
Tom
Super User Tom
Super User

@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?

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
  • 10 replies
  • 3247 views
  • 12 likes
  • 6 in conversation