BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lydiawawa
Lapis Lazuli | Level 10

Hi All,

 

I have a time variable that contains the values in the form of : 2018-04-20T20:38:12.124Z

I tried to convert it into sas date time with the following code:

 

value_year = input(substr(value,1,4), 4.);
value_month = input(substr(value,6,2), 2.);
value_day = input(substr(value,9,2), 2.);
date = MDY(value_month,value_day, value_year);
value_hour = input(substr(value,12,2), 2.);
value_minute = input(substr(value,15,2), 2.);
value_second = input(substr(value,18,6), 6.3);
sasdatetime = DHMS(date,value_hour,value_minute, value_second);

Now I'm hoping to calculate the difference between two time values after converting the original values to the form of "sasdatetime". The unit of the difference should be in minute and second (MM/SS).

 

For example the time interval between:

2018-03-23T14:19:43.046Z

and

2018-03-28T02:56:37.460Z

 

In minute and second.

 

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Then find the appropriate INFORMAT.

 

data have;
x1='2018-03-23T14:19:43.046Z';
x2='2018-03-28T02:56:37.460Z';
y1=input(x1, b8601dz.);
y2=input(x2, b8601dz.);
diff = y2-y1;
format y1 y2 datetime32.4 diff time12.;
run;

Results:

 

 
Obs x1 x2 y1 y2 diff
1 2018-03-23T14:19:43.046Z 2018-03-28T02:56:37.460Z 23MAR2018:14:19:43.0460 28MAR2018:02:56:37.4600 108:36:54

 

 

View solution in original post

8 REPLIES 8
Reeza
Super User
So your original value looks like 22018-04-20T20:38:12.124Z? What is the type and format?

I think you can use INPUT() to do a direct conversion such as:

input(value, anydtdtm.);

Where are the variables you want to calculate the difference of - in the same row? You can do the standard math and then format it as a time variable so the time as hour minutes and seconds.

diff = datevar1-datevar2;
format diff time8.;
lydiawawa
Lapis Lazuli | Level 10

Hi Reeza, I am not sure of the original time type, it only shows the length and it is in character format($250), that is why I sub stringed the value by position. The original time value looks like "2018-04-20T20:38:12.124Z". Can I still use anydtdtm?

lydiawawa
Lapis Lazuli | Level 10
anydtdtm will only give time up until minutes. I need to keep seconds too. I also made edits to my previous reply.
Reeza
Super User

Then find the appropriate INFORMAT.

 

data have;
x1='2018-03-23T14:19:43.046Z';
x2='2018-03-28T02:56:37.460Z';
y1=input(x1, b8601dz.);
y2=input(x2, b8601dz.);
diff = y2-y1;
format y1 y2 datetime32.4 diff time12.;
run;

Results:

 

 
Obs x1 x2 y1 y2 diff
1 2018-03-23T14:19:43.046Z 2018-03-28T02:56:37.460Z 23MAR2018:14:19:43.0460 28MAR2018:02:56:37.4600 108:36:54

 

 

lydiawawa
Lapis Lazuli | Level 10

Is it possible to proc tabulate the mean among the difference of several dates and still maintain the time12. format? I tried to proc tabulate by mean, it returned a decimal number instead, even after I specified the format time12.

lydiawawa
Lapis Lazuli | Level 10

Suppose I have the following set of time:

                                                           

         15APR2018:16:06:20.9270            

         15APR2018:16:06:15.5750

         15APR2018:16:05:53.9130

         15APR2018:16:02:57.8270

 

Already converted to b8601dz. You can find te difference between the next to previous term by using dif()

 

Now I hope to find out the mean difference by tabulation:

proc tabulate data = have missing out=want;
     var interval;
     table (interval = "Time difference") * (N mean std min max);
     format interval time12.;
run;

The table returns interval in decimal instead of MM:SS (time12.) format.

 

 

Tom
Super User Tom
Super User

put the format on the statistic request.

proc tabulate data = have missing out=want;
     var interval;
     table (interval = "Time difference") * (N mean*f=time12.3 std min*f=time12.3 max*f=time12.3);
run;
Tom
Super User Tom
Super User

SAS has ISO informats.

https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n0verk17pch...

 

data test;
 str='2018-04-20T20:38:12.124Z';
 date1=input(str,E8601DZ25.);
 format date1 datetime25.3;
 put (_all_) (=);
run;
str=2018-04-20T20:38:12.124Z date1=20APR2018:20:38:12.124

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 8 replies
  • 1831 views
  • 5 likes
  • 3 in conversation