Help using Base SAS procedures

difference in values of a single field

Reply
N/A
Posts: 0

difference in values of a single field

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
Trusted Advisor
Posts: 1,321

Re: difference in values of a single field

Posted in reply to deleted_user
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;
Super User
Posts: 10,023

Re: difference in values of a single field

Posted in reply to deleted_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
Ask a Question
Discussion stats
  • 2 replies
  • 99 views
  • 0 likes
  • 3 in conversation