BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AMFR
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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;

View solution in original post

13 REPLIES 13
Kurt_Bremser
Super User

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.

AMFR
Quartz | Level 8

Ohh sorry, i ddinot know that

Shmuel
Garnet | Level 18

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;

AMFR
Quartz | Level 8

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

 

 

AMFR
Quartz | Level 8

Never mind, i figure this out. I am having a bad day. My brain is not working.

 

Thanks again for the solution

Shmuel
Garnet | Level 18

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;

 

AMFR
Quartz | Level 8

This is excatly what i did. Thanks again, i am really grateful!

Reeza
Super User

Please don't post the same question multiple times. Use RETAIN to hold a value across rows. 

AMFR
Quartz | Level 8

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;

Shmuel
Garnet | Level 18

@AMFR, no need to duplicate your query. You could post this test data within the original query.

See my answer to the previous one.

AMFR
Quartz | Level 8

I am so sorry i do not know how this is happening.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1523 views
  • 3 likes
  • 4 in conversation