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.
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.
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.
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.