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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 1207 views
  • 0 likes
  • 2 in conversation