BookmarkSubscribeRSS Feed
stancemcgraw
Obsidian | Level 7

Hi all,

 

   I am trying to calculate the time from the last tube feed (tube_stopped) to the time of surgery. Each patient has multiple tube stop times and also multiple surgeries (date_procedure). I want to create a new variable that calculates this called "Time from last tube feed to procedure" in hours....

 

Data have:

ID                    tube_stopped                date_procedure

7                      12Jul17:02:00:00           13JUL17:13:00:00

7                      01AUG17:15:00:00         01AUG17:22:00:00

7                     20JUL17:07:35:00            20JUL17:22:00:00

1                      29JUL16:01:00:00            29JUL16:23:00:00

1                     12AUG16:16:00:00           12AUG16:22:00:00

 

Data want:

 

ID       tube_stopped                 date_procedure               time from last tube feed to procedure

7        01AUG17:15:00:00         01AUG17:22:00:00          2.4

1        12AUG16:16:00:00         12AUG16:22:00:00          5.7

 

 

 

4 REPLIES 4
Reeza
Super User
Use LAG() and basic math, the difference is returned in seconds. To get hours divide by 60 twice.
data want;
set have;
by ID;
Time_Diff = (lag(date_procedure) - tube_stopped)/60/60;
if first.id then call missing(Time_diff);
run;

Reeza
Super User
Actually upon re-reading this, I may have over complicated this, you can just take the difference of each line and don't need the LAG() function.
stancemcgraw
Obsidian | Level 7

What does the LAG function do?

Reeza
Super User
LAG gets the previous X row values, I was thinking you need to be looking at the value in the previous row. Lag4 refers to 4 rows back and Lag refers to the previous value

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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