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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 920 views
  • 0 likes
  • 3 in conversation