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!
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 |
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?
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 |
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.
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.
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;
SAS has ISO informats.
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
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!
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.
Ready to level-up your skills? Choose your own adventure.