BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jcapua2
Fluorite | Level 6

Hello,

 

data have;

patient_numProcedure DatePatient Re-AdmittedStatus DateDays Until Graft Failure
patient3203/07/2002No02/23/2004718
patient3202/23/2004Yes10/01/20143873

 

data want;

patient_numProcedure DatePatient Re-AdmittedStatus DateDays Until Graft FailureDays Until Graft Failure if Re-Admitted
patient3203/07/2002No02/23/2004718.
patient3202/23/2004Yes10/01/201438734591

 

For the variable "Days Until Graft Failure if Re-Admitted," I want to calculate the days using the earliest procedure date instead of the procedure date corresponding to that row for anyone who was "Patient Re-Admitted"='Yes'.

 

Is there way to calculate this in SAS datastep?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Do you want something like this:

 

data have;
input patient_num : $10. Procedure_date :mmddyy10. PatientRe_Admitted $ StatusDate :mmddyy10. DaysUntilGraftFailure;
format Procedure_date StatusDate mmddyy10.;
datalines;
patient32 03/07/2002 No 02/23/2004 718
patient32 02/23/2004 Yes 10/01/2014 3873
;
data want;
set have;
by patient_num;
retain temp;
if first.patient_num then temp=Procedure_date;
else if PatientRe_Admitted='Yes' then DaysUntilGraftFailifReAdmitted=intck('days', temp, StatusDate);
drop temp;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Do you want something like this:

 

data have;
input patient_num : $10. Procedure_date :mmddyy10. PatientRe_Admitted $ StatusDate :mmddyy10. DaysUntilGraftFailure;
format Procedure_date StatusDate mmddyy10.;
datalines;
patient32 03/07/2002 No 02/23/2004 718
patient32 02/23/2004 Yes 10/01/2014 3873
;
data want;
set have;
by patient_num;
retain temp;
if first.patient_num then temp=Procedure_date;
else if PatientRe_Admitted='Yes' then DaysUntilGraftFailifReAdmitted=intck('days', temp, StatusDate);
drop temp;
run;

jcapua2
Fluorite | Level 6

Thank you! It works!

Reeza
Super User

Sort your data so that the earliest procedure date is first. Then you can use RETAIN and first to keep it and use it for further calculations.  

 

proc sort data=have;
by patient_num procedure_date;
run;

data want;
set have;
retain first_procedure_date;

if first.patient_num then first_procedure_date = procedure_date;


***do calculations with first procedure date here****;

run;
mkeintz
PROC Star

If the data are sorted only by id, and sorting by procdate within id is expensive, then this is a good case to use auto-interleaving,  i.e. interleaving data set HAVE with itself, on an ID by ID basis.  It leaves the data in original order:

 

data want;
  set have (in=firstpass)
      have (in=secondpass);
  by id;

  retain min_procdate;
  if first.id then min_procdate=procdate;
  else if firstpass then min_procdate=min(min_procdate,procdate);

  if secondpass;
  if readmitted='Y' then days_to_failure_readmit=status-min_procdate;
run;

 

For each id, all the records are read for a first pass, calculating the minimum procdate.  Then keep only records from the second pass ("if secondpass;") for the same id, which is when the days_to_failure variable is calculated.

 

Auto-interleaving is also frequently done by use of two "do until (last.id)" loops, commonly called "double dow".  But this code is simpler, in that no do groups need be constructed, nor do you need an explicit OUTPUT statement in the second do group.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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