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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

 

View solution in original post

13 REPLIES 13
art297
Opal | Level 21

Given those 3 records what would you want your output file to look like?

 

Art, CEO, AnalystFinder.com

 

swar
Obsidian | Level 7

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.

 

art297
Opal | Level 21

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

 

swar
Obsidian | Level 7

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

 

 

 

Reeza
Super User

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
Obsidian | Level 7
Thanks Reeza. But, I was wondering if I can integrate the tables with the data step without inputting values again...
Reeza
Super User

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

 

swar
Obsidian | Level 7
I have complex queries already performed on tables and inputting huge data through data step from tables will complicate the entire process. So I am looking out for a solution which can resolve this issue.
art297
Opal | Level 21

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

 

swar
Obsidian | Level 7
The flag variable is giving ' . ' in cases where the diff is less than 30 days.
For cases where there is no readmission and diff is null it should give flag as 0.

please help!!!
art297
Opal | Level 21

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

swar
Obsidian | Level 7
Thank you!!
Reeza
Super User

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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