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

Hi,

 

How can I convert a datetime character string variable in the format yyyy-mm-ddThh-mm-ss.fffffffZ to a time variable to 3 or more milliseconds and have that in a readable format e.g. hh:mm:ss:mmm.

An example I have is: 2022-05-16T02:30:00.2159663Z and I want a time variable in some format similar to this for example 02:30:00:216.

I have tried various ISO formats such as E8601DZ but haven't managed to find the solution yet.

 

Appreciate any help. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Example:

 

 

data a;
    datetime=input('2022-05-16T02:30:00.2159663Z',e8601dz28.6);
    want_time=timepart(datetime);
    format want_time time12.3;
run;

 

 

Note: e8601dz in this case is an INFORMAT as it is used to read IN values. It is not a format in this case.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Example:

 

 

data a;
    datetime=input('2022-05-16T02:30:00.2159663Z',e8601dz28.6);
    want_time=timepart(datetime);
    format want_time time12.3;
run;

 

 

Note: e8601dz in this case is an INFORMAT as it is used to read IN values. It is not a format in this case.

--
Paige Miller
Tom
Super User Tom
Super User

To convert a string to a number you need to use and informat, not a format.  Formats convert values to text. Informats convert text to values.

 

You can use the E8601DZ informat to convert that string into a datetime value.  You can use the TIMEPART() function to extract just the time of day component from the datetime value.

Note that you cannot store that many decimal places in a datetime value.  The number of decimal digits required would be more than SAS can store exactly in the binary floating point format it uses to store numbers.

15   data test;
16     string = '2022-05-16T02:30:00.2159663Z';
17     dt = input(string,E8601DZ30.);
18     time = timepart(dt);
19     format dt datetime29.7 time tod16.7 ;
20     put (_all_) (=/);
21     put dt = 32.8 ;
22   run;


string=2022-05-16T02:30:00.2159663Z
dt=16MAY2022:02:30:00.2159662
time=02:30:00.2159662
dt=1968287400.21596000

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 1448 views
  • 2 likes
  • 3 in conversation