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

## Calculating date difference

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
Opal | Level 21

## Re: Calculating date difference

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

13 REPLIES 13
Opal | Level 21

## Re: Calculating date difference

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

Art, CEO, AnalystFinder.com

Obsidian | Level 7

## Re: Calculating date difference

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.

Opal | Level 21

## Re: Calculating date difference

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

Obsidian | Level 7

## Re: Calculating date difference

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.

Super User

## Re: Calculating date difference

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.

Obsidian | Level 7

## Re: Calculating date difference

Thanks Reeza. But, I was wondering if I can integrate the tables with the data step without inputting values again...
Super User

## Re: Calculating date difference

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

Obsidian | Level 7

## Re: Calculating date difference

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.
Opal | Level 21

## Re: Calculating date difference

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

Obsidian | Level 7

## Re: Calculating date difference

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!!!
Opal | Level 21

## Re: Calculating date difference

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

Obsidian | Level 7

Thank you!!
Super User

## Re: Calculating date difference

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.

Discussion stats
• 13 replies
• 1374 views
• 4 likes
• 3 in conversation