Help using Base SAS procedures

Visit Hour Per of ED Visit?

Reply
Regular Contributor
Posts: 152

Visit Hour Per of ED Visit?

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.

Super User
Posts: 17,750

Re: Visit Hour Per of ED Visit?

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.

Regular Contributor
Posts: 152

Re: Visit Hour Per of ED Visit?

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

Super User
Posts: 17,750

Re: Visit Hour Per of ED Visit?

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

hour(timepart(date_hour))

Regular Contributor
Posts: 152

Re: Visit Hour Per of ED Visit?

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.

Super User
Posts: 17,750

Re: Visit Hour Per of ED Visit?

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);

Super User
Posts: 10,466

Re: Visit Hour Per of ED Visit?

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 < .

Regular Contributor
Posts: 152

Re: Visit Hour Per of ED Visit?

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

Regular Contributor
Posts: 152

Re: Visit Hour Per of ED Visit?

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.

Ask a Question
Discussion stats
  • 8 replies
  • 256 views
  • 1 like
  • 3 in conversation