BookmarkSubscribeRSS Feed
jcapua2
Fluorite | Level 6

Hello,

  

I want to calculate the difference between dates in days using the same variable date [Procedure_Date] in the same patient, among only those with the same Status=Alive.

 

data have;

patient_numProcedure_DateStatus_DateStatus
patient3303/07/200202/23/2004Alive
patient3302/23/200410/01/2014Alive
patient5005/07/200712/02/2014Alive
patient5005/27/200910/08/2015Dead

 

data want;

patient_numProcedure_DateStatus_DateStatusDays_Between_Procedure
patient3303/07/200202/23/2004Alive718
patient3302/23/200410/01/2014Alive718
patient5005/07/200712/02/2014Alive.
patient5005/27/200910/08/2015Dead.

 

So for example, the difference in days of patient33's first Procedure_Date and second Procedure_Date, which equals to 718 days (same Status="Alive") and appears in the both rows.

 

Is there a way to do it in SAS datastep or in SQL?

 

Thank you!

3 REPLIES 3
ballardw
Super User

It appears that you only want the first value calculated and then retained. If your data is sorted by patient_num and procedure_date and your procedure_date and status_date variables are SAS date valued variables

data want;
    set have;
    by patient_num;
    retain  Days_between_procedure;
    if first.patient_num then Days_between_procedure= Status_date-Procedure_date;
run; 

If your dates are character variables then you will have to convert them to SAS date values

 

if first.patient_num then Days_between_procedure=  input(Status_date,mmddyy10.)- input(Procedure_date,mmddyy10.);
Astounding
PROC Star

Putting the same "days between procedures" on every observation is probably a mistake.  What values go where, if the patient actually had 3 procedures instead of 2?

 

Here's an easy way to put the "days between procedures" on the current observation only, as long as the patient is still alive.

 

proc sort data=have;

  by patient_num procedure_date;

run;

 

data want;

set have;

by patient_num procedure_date;

days_between_procedure = dif(procedure_date);

if first.patient_num or status ne 'Alive' then days_between_procedure = .;

run;

 

You may want to inspect the results to see if they seem like they would be easy enough to work with.  There are other ways, if you really need them.

novinosrin
Tourmaline | Level 20


data have;
input patient_num : $10. (Procedure_Date Status_Date) (:mmddyy10.) Status $;
format Procedure_Date Status_Date mmddyy10.;
datalines;
patient33 03/07/2002 02/23/2004 Alive
patient33 02/23/2004 10/01/2014 Alive
patient50 05/07/2007 12/02/2014 Alive
patient50 05/27/2009 10/08/2015 Dead
;

data want;
call missing(_flag);
do _n_=1 by 1 until(last.patient_num);
set have;
by patient_num Procedure_Date;
if _n_=2 then _temp=Procedure_Date;
if status='Dead' then _flag=0;
end;
do _n_=1 by 1 until(last.patient_num);
set have;
by patient_num Procedure_Date;
if _n_=1 and missing(_flag) then Days_Between_Procedure=intck('days',Procedure_Date, _temp);
output;
end;
drop _:;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 1018 views
  • 3 likes
  • 4 in conversation