DATA Step, Macro, Functions and more

Date time question

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Date time question

[ Edited ]

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;


Accepted Solutions
Solution
‎01-31-2017 09:15 AM
Trusted Advisor
Posts: 1,574

Re: Date time question

[ Edited ]

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


All Replies
Super User
Posts: 7,827

Re: Date time question

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 34

Re: Date time question

Posted in reply to KurtBremser

Ohh sorry, i ddinot know that

Solution
‎01-31-2017 09:15 AM
Trusted Advisor
Posts: 1,574

Re: Date time question

[ Edited ]

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;

Contributor
Posts: 34

Re: Date time question

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

 

 

Contributor
Posts: 34

Re: Date time question

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

 

Thanks again for the solution

Trusted Advisor
Posts: 1,574

Re: Date time question

[ Edited ]

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;

 

Contributor
Posts: 34

Re: Date time question

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

Super User
Posts: 19,822

Re: Date time question

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

Contributor
Posts: 34

Date time question

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;

Trusted Advisor
Posts: 1,574

Re: Date time question

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

See my answer to the previous one.

Super User
Posts: 7,827

Re: Date time question

I merged the threads.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 34

Re: Date time question

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

Super User
Posts: 7,827

Re: Date time question

Use @Shmuels suggestion as a blueprint.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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