BookmarkSubscribeRSS Feed
shellp55
Quartz | Level 8

Hello Previously I had asked SAS Communities how to create a data set so that I had one line of data per day of an inpatient visit and Reeza kindly provided the code below: data want; set have; do date=admit_date to discharge_date; output; end; run; proc freq data=want; table have; run; What I would like to do now is the same sort of thing but for ED visits and I'd like a line of data per HOUR of the visit.  So if the patient was in the ED from noon to 5:00 I'd like one line of data each for the hours of 12, 13, 14, 15, 16, and 17.  Any assistance greatly appreciated.

8 REPLIES 8
Reeza
Super User

Same idea but change your do loop to have a record for each hour, assuming you have date time variables that represent the date/time of the visit and using the fact that 60 seconds * 60 minutes = 3600 seconds/hour

data want;

set have;

do date_hour=admit_datetime to discharge_datetime by 3600;

output;

end;

run;


Then proc freq on date_hour.

shellp55
Quartz | Level 8

You are amazing, Reeza, thanks so much.  One more question:  how do I format the date_hour to be 01, 02 to 2300?  Thanks.

Reeza
Super User

May as well get the hour out using hour and timepart function.

hour(timepart(date_hour))

shellp55
Quartz | Level 8

Sorry Reeza, I just checked some of the data and I'm not sure it's working as I expected.  For instance, a chart with an admit date of 02Apr14:04:41:00 and a disposition date of 02Apr14:15:10:00 should have visits for hours 04 to 15 but only has from 04 to 14 in the database.  Why would it not also include 15? It seems to be prevalent in the resulting dataset that the end hour is not included.  Thanks.

Reeza
Super User

You'll probably need to increment the disposition date for the loop

I don't have time to test it now, but you can try something like the following to change the end date.

Another option is to change the loop to a do while loop and use the intnx function to increment the time.

Both untested:

date=admit_date;

output;

do while(date<disposition_date);

date=intnx('hour', date, 1);

output;

end;

loop_end_dt=round(disposition_date+1800, 3600);

ballardw
Super User

And a second bit depending on how and where your create and compare the end value: MINUTES

If you increment the admit date/time of 02Apr14:04:41:00 by two hours (02Apr14:06:41:00) and compare to a disposition datetime of 02Apr14:06:10:00 then you might be terminating your loop early using

do while(date<disposition_date); because the date skips over the disposition data and the dependent statements (such as OUTPUT) are not executed for the 06:41:00 value.

You may need to get a comparison value for your base comparison admit date to drop minutes before looping. and I think you may want le instead of < .

shellp55
Quartz | Level 8

Hi Thanks to you both...but ballardw I'm not sure what you mean.  What would you change in Reeza's code?  Thanks.

shellp55
Quartz | Level 8

Hi Okay, I've got it and it works as I wanted. I used the loop_end_dt version and then used that as the end date in my "data want" data set i.e. do date_hour=adm_date to loop_end_dt by 3600.  Thanks to you both.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 866 views
  • 1 like
  • 3 in conversation