Hello,
Im trying to generate the difference between the first and last dates that a patient was on a certain medication in hours. These are listed in the same column.
Data have:
studynum med_admin_date
1 10/26/20 8:00
1 10/27/20 3:00
1 10/28/20 8:00
data want:
studynum difference
1 48
proc sql;
create table want as
select
studynum,
(max(med_admin_date) - min(med_admin_date)) / 3600 as difference
from have
group by studynum
;
quit;
Are these valid numeric SAS date/time values? What is the type of variable MED_ADMIN_DATE (numeric or character) according to PROC CONTENTS? What is the format of variable MED_ADMIN_DATE according to PROC CONTENTS?
If they are valid numeric SAS datetime value, then PROC SUMMARY gets the job done, it finds the difference in seconds, and then you can divide by 3600 to get hours.
proc summary data=have;
class studynum;
var MED_ADMIN_DATE;
output out=want range=/autoname;
run;
data want;
set want;
hours=MED_ADMIN_DATE_range/3600;
run;
proc sql;
create table want as
select
studynum,
(max(med_admin_date) - min(med_admin_date)) / 3600 as difference
from have
group by studynum
;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.