Hi all,
I am trying to calculate number of hours from surgery time out and time stamp when heart rate is recorded. I have time stamp for heart rate in all the rows but not for the surgery time out. I know how to calculate number of hours from two time stamps but since time out is not recorded for each row, I am not able to do it. I have tried lag fuction but no help. Can some one please help me. Attached is the fabricated data. I want to calculate the difference in hours from time_out to time heart rate recorded for each row.
Thanks
data hr;
infile datalines missover;
input encounter date mmddyy10. hr_ts : datetime. hr time_in : datetime. time_out : datetime.;
format date mmddyy10. hr_ts datetime. time_in datetime. time_out datetime.;
datalines;
5307 4/18/2014 18APR14:14:28:00 85 18APR14:07:12:00 18APR14:13:15:00
5307 4/18/2014 18APR14:17:00:00 124 18APR14:07:12:00 18APR14:13:15:00
5307 4/19/2014 19APR14:01:13:00 125
5307 4/19/2014 19APR14:04:00:00 130
5307 4/20/2014 20APR14:13:05:00 99
1602 1/06/2012 06JAN12:14:00:00 89 06JAN12:07:20:00 06JAN12:13:09:00
1602 1/06/2012 06JAN12:15:00:00 109 06JAN12:07:20:00 06JAN12:13:09:00
1602 1/07/2012 07JAN12:23:00:00 99
1602 1/07/2012 07JAN12:01:00:00 90
1602 1/07/2012 07JAN12:23:00:00 99
1602 1/07/2012 07JAN12:02:00:00 89
;
run;
proc print data=work.hr;
run;
As much as I see, time_in and time_out are the same in all first rows and suddenly (from line 24 on) they are missing.
Assuming the value in first row are same for all rows, then you can do:
data want;
set have;
retain t_in t_out;
if _N_ = 1 then do;
t_in = time_in; t_out=time_out;
end; else do;
time_in = t_in;
time_out = t_out;
end;
.... our code to comute gap hours to timestamp ...
run;
Please post your example data in a data step with datalines. Excel files can contain malware and are therefore blocked by most corporate firewalls. And sensible people won't even open them if they can. They are also totally unstructured and cannot preserve the attributes of SAS datasets.
Data steps allow us to recreate your data with a simple copy/paste and run. See this as a matter of basic courtesy.
Ohh sorry, i ddinot know that
As much as I see, time_in and time_out are the same in all first rows and suddenly (from line 24 on) they are missing.
Assuming the value in first row are same for all rows, then you can do:
data want;
set have;
retain t_in t_out;
if _N_ = 1 then do;
t_in = time_in; t_out=time_out;
end; else do;
time_in = t_in;
time_out = t_out;
end;
.... our code to comute gap hours to timestamp ...
run;
Hi, thank you very much for your reply.
Time in and time out is different for each encounter. I have thousands of encounters with diffrent time in and time out.
Above codes are changing time in and time out to one time in and time out for all the encounters
Thanks again
Never mind, i figure this out. I am having a bad day. My brain is not working.
Thanks again for the solution
I haven't seen colomn encounter or ID in your data.
You can change the code to:
data want;
set have;
by encounter ;
retain t_in t_out;
if first.encounter then do;
t_in = time_in; t_out=time_out;
end; else do;
time_in = t_in;
time_out = t_out;
end;
.... our code to comute gap hours to timestamp ...
run;
This is excatly what i did. Thanks again, i am really grateful!
Please don't post the same question multiple times. Use RETAIN to hold a value across rows.
Hi all,
I am trying to calculate number of hours from surgery time out and time stamp when heart rate is recorded. I have time stamp for heart rate in all the rows but not for the surgery time out. I know how to calculate number of hours from two time stamps but since time out is not recorded for each row, I am not able to do it. I have tried lag fuction but no help. Can some one please help me. Attached is the fabricated data. I want to calculate the difference in hours from time_out to time heart rate recorded for each row.
Thanks
data hr;
infile datalines missover;
input encounter date mmddyy10. hr_ts : datetime. hr time_in : datetime. time_out : datetime.;
format date mmddyy10. hr_ts datetime. time_in datetime. time_out datetime.;
datalines;
5307 4/18/2014 18APR14:14:28:00 85 18APR14:07:12:00 18APR14:13:15:00
5307 4/18/2014 18APR14:17:00:00 124 18APR14:07:12:00 18APR14:13:15:00
5307 4/19/2014 19APR14:01:13:00 125
5307 4/19/2014 19APR14:04:00:00 130
5307 4/20/2014 20APR14:13:05:00 99
1602 1/06/2012 06JAN12:14:00:00 89 06JAN12:07:20:00 06JAN12:13:09:00
1602 1/06/2012 06JAN12:15:00:00 109 06JAN12:07:20:00 06JAN12:13:09:00
1602 1/07/2012 07JAN12:23:00:00 99
1602 1/07/2012 07JAN12:01:00:00 90
1602 1/07/2012 07JAN12:23:00:00 99
1602 1/07/2012 07JAN12:02:00:00 89
;
run;
proc print data=work.hr;
run;
@AMFR, no need to duplicate your query. You could post this test data within the original query.
See my answer to the previous one.
I am so sorry i do not know how this is happening.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.