DATA Step, Macro, Functions and more

Working with datetime and milliseconds

Accepted Solution Solved
Reply
Contributor
Posts: 59
Accepted Solution

Working with datetime and milliseconds

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


Accepted Solutions
Solution
‎04-05-2018 06:55 AM
Super User
Posts: 9,873

Re: Working with datetime and milliseconds

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Trusted Advisor
Posts: 1,831

Re: Working with datetime and milliseconds

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

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

Contributor
Posts: 59

Re: Working with datetime and milliseconds

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

Solution
‎04-05-2018 06:55 AM
Super User
Posts: 9,873

Re: Working with datetime and milliseconds

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 5,849

Re: Working with datetime and milliseconds

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
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 100 views
  • 2 likes
  • 4 in conversation