I have the following data
patient_no claim_no dt_admission dt_discharge
1 1 1/1/2012 1/3/2012
2 1 1/1/2011 1/3/2011
1 2 2/2/2016 2/3/2016
Patient 1 is readmitted and the difference between second dt_admission and dt_discharge in days
Need to find the difference in days.
Any help is much appreciated.
Thanks
I think that is easier in a datastep:
data have; informat dt_admission dt_discharge mmddyy10.; format dt_admission dt_discharge date9.; input patient_no claim_no dt_admission dt_discharge; cards; 1 1 1/1/2012 1/3/2012 2 1 1/1/2011 1/3/2011 1 2 2/2/2016 2/3/2016 ; proc sort data=have out=want; by patient_no dt_admission; run; data want (drop=start); set want; retain start; by patient_no; if first.patient_no then do; start=dt_discharge; counter=1; end; else counter+1; if last.patient_no and counter gt 1 then do; diff=dt_admission-start; if diff gt 30 then flag=1; end; run;
Art, CEO, AnalystFinder.com
Given those 3 records what would you want your output file to look like?
Art, CEO, AnalystFinder.com
That's just a sample health data .
I would like to create a flag variable in proc sql.
I am trying to calculate the difference in date from the first discharge date and the readmission date. If it is less than 30 days then the flag should be 1.
intck('day',dt_admission, dt_discharge) would give the difference for each particular row but not readmission date.
I think that is easier in a datastep:
data have; informat dt_admission dt_discharge mmddyy10.; format dt_admission dt_discharge date9.; input patient_no claim_no dt_admission dt_discharge; cards; 1 1 1/1/2012 1/3/2012 2 1 1/1/2011 1/3/2011 1 2 2/2/2016 2/3/2016 ; proc sort data=have out=want; by patient_no dt_admission; run; data want (drop=start); set want; retain start; by patient_no; if first.patient_no then do; start=dt_discharge; counter=1; end; else counter+1; if last.patient_no and counter gt 1 then do; diff=dt_admission-start; if diff gt 30 then flag=1; end; run;
Art, CEO, AnalystFinder.com
Thanks.
But I already have all the data as tables inputted in sas and this is a new variable in the select statement along with 5 other variables which are flagged.
So I am specifically inclined in trying a solution in proc sql statement.
SQL doesn't have a concept of rows so looking back and forward is more difficult, especially when you're learning and don't quite understand the base concepts yet. You would need to redesign your entire query so you're better off using two steps. Note that a datastep was provided previously so you could add this solution to that step if you're concerned with multiple passes of the data.
Obviously there nothing stopping you from ignoring this advice, it's ultimately your choice.
@swar wrote:
Thanks Reeza. But, I was wondering if I can integrate the tables with the data step without inputting values again...
I don't know what that means.
You don't need to re-input the data. Just bring it into the datastep with the set statement. The last datastep I suggested did just that. The proc sort, before it, was to sort your data in the event that it wasn't already sorted.
Art, CEO, AnalystFinder.com
Just initialize the flag variable at 0 when creating it. e.g.:
data want (drop=start); set want; retain start; by patient_no; flag=0; if first.patient_no then do; start=dt_discharge; counter=1; end; else counter+1; if last.patient_no and counter gt 1 then do; diff=dt_admission-start; if diff gt 30 then flag=1; end; run;
Art, CEO, AnalystFinder.com
http://support.sas.com/training/tutorial/
The first e course for basic SAS programming is free as well as numerous video tutorials on accomplishing basic tasks in SAS.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.