BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi All,

Could you please guide me. I have a field "TIME" and this has values (in ascending order) :

10:34:37,
10:36:03,
10:38:56,
10:47:35,
10:51:54,
10:54:47,
11:03:25,

I want to find the difference between the suucessive terms and create a field, say, NewDiff which has values 00:00:00,00:01:26 (10:36:03-10:34:37) and so on.

Kind Regards,
Kritanjli
2 REPLIES 2
MichelleHomes
Meteorite | Level 14
Hi Kritanjli,

Let me know if this is what you are after... The lag function is a great function to get the previous value into the PDV (Program Data Vector) to do calculations. Please note that the code below assumes that no time values are missing as then the result is converted to a 0. I have done that so that your first value is 0 rather than missing. Just depends on whether that is a requirement or not.

Cheers,
Michelle
-----------
data timeCalculations;
input time : time8.;
format time NewDiff time8.;
NewDiff=time-lag(time);
if NewDiff=. then NewDiff=0;
cards;
10:34:37
10:36:03
10:38:56
10:47:35
10:51:54
10:54:47
11:03:25
;
run;
//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
Ksharp
Super User
Hi.
Function dif() is identical with time-lag(time)



[pre]
data timeCalculations;
input time : time8.;
format time NewDiff time8.;
NewDiff=dif(time);
if NewDiff=. then NewDiff=0;
cards;
10:34:37
10:36:03
10:38:56
10:47:35
10:51:54
10:54:47
11:03:25
;
run;
proc print;run;
[/pre]





Ksharp

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 589 views
  • 0 likes
  • 3 in conversation