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

Hi,

 

I would like to know what function can i use to work with datetime and milliseconds. I know that not exist function milliseconds.

 

I try to put in a datastep using the macro but with not result.

 

data work.FILTER_FOR_SORTSORTED;
  infile datalines dsd truncover;
  input Rtu date:DATETIME. Rtu ms:BEST12.;
  format Rtu date DATETIME. Rtu ms BEST12.;
datalines4;
,,,
,,,
,,,
,,,
,,,
,,,
,,,
;;;;


What i have

Datetime                  Milliseconds
23OCT17:19:55:29	620
23OCT17:19:17:53	500
04OCT17:15:55:52	800
18OCT17:14:11:32	470
28OCT17:15:33:29	160
23OCT17:09:13:40	130
18OCT17:15:13:02	100

Datetime have datetime format and milliseconds a numeric format

 

What i want is a result like this: 18OCT17:15:13:02:100

 

I saw in other topic to this in datetime23.3 as format. I try to sum but with no result, and the function dhms( or other function) hasn't milliseconds part. Or i have to change format to string, use function cats or catx, and again to datetime23.3 format?

 

Other question: Can i calculate time intervals with datetime23.3 formats like:

 

18OCT17:15:13:02:100   -   18OCT17:15:13:02:000   = 100   OR

 

18OCT17:15:13:02:100   -   18OCT17:15:13:01:000   = 1:100 OR

 

18OCT17:15:13:02:100   -   18OCT17:15:12:02:100   =1:0:000 ?

 

Regards,
Aleixo

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Datetime values are counts of seconds, a millisecond is the 1000th part of a second, so you have to divide your milliseconds by 1000 and add them to the datetime:

data work.FILTER_FOR_SORTSORTED;
  infile datalines truncover;
  input Rtu_date:DATETIME16. Rtu_ms:BEST12.;
  format Rtu_date DATETIME23.3 Rtu_ms BEST12.;
  Rtu_date = Rtu_date + Rtu_ms / 1000;
datalines4;
23OCT17:19:55:29 620
23OCT17:19:17:53 500
04OCT17:15:55:52 800
18OCT17:14:11:32 470
28OCT17:15:33:29 160
23OCT17:09:13:40 130
18OCT17:15:13:02 100
;;;;
run;

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

Change the DATETIME.  format into DATETIME23. format then you can decide

how many milisconds you prefer and change the 23 to 22 / 21 etc.

Aleixo
Quartz | Level 8

when i change to datetime23. SAS put milliseconds to 000. And Now how i can put my variable milliseconds into the variable datetime23.?

 

data data_completa;
set data;
time_want=dhms(DATEPART(Datetime), HOUR(Datetime), MINUTE(Datetime), date(Datetime));
time_char=cats(put(time_want, datetime23.),':',Milliseconds);
format time_want datetime16. 
run;

But now i have a char with 18OCT17:15:13:02:100 and SAS does not admit a format datetime23.3 to time_char. The idea is time_char a variable type datetime23.3 or in this case datetime20.3 for example -> 30OCT17:04:20:03:100

 

Regards,

Aleixo

Kurt_Bremser
Super User

Datetime values are counts of seconds, a millisecond is the 1000th part of a second, so you have to divide your milliseconds by 1000 and add them to the datetime:

data work.FILTER_FOR_SORTSORTED;
  infile datalines truncover;
  input Rtu_date:DATETIME16. Rtu_ms:BEST12.;
  format Rtu_date DATETIME23.3 Rtu_ms BEST12.;
  Rtu_date = Rtu_date + Rtu_ms / 1000;
datalines4;
23OCT17:19:55:29 620
23OCT17:19:17:53 500
04OCT17:15:55:52 800
18OCT17:14:11:32 470
28OCT17:15:33:29 160
23OCT17:09:13:40 130
18OCT17:15:13:02 100
;;;;
run;
LinusH
Tourmaline | Level 20

Not sure what you want to achieve, but fractions of seconds is stored physically as decimals in the numerical variable.

So yes, you can do subtraction between datetime variables. You could given your examples use time12.3 or any other preferred time format for better display.

Data never sleeps

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
  • 4 replies
  • 14293 views
  • 2 likes
  • 4 in conversation