SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2211 views
  • 2 likes
  • 3 in conversation