Solved
Contributor
Posts: 22

# Calculating date difference

[ Edited ]

I have the following data

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

Accepted Solutions
Solution
‎04-23-2017 10:57 AM
PROC Star
Posts: 8,164

## Re: Calculating date difference

I think that is easier in a datastep:

```data have;
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;
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;
if diff gt 30 then flag=1;
end;
run;
```

Art, CEO, AnalystFinder.com

All Replies
PROC Star
Posts: 8,164

## Re: Calculating date difference

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

Art, CEO, AnalystFinder.com

Contributor
Posts: 22

## Re: Calculating date difference

[ Edited ]

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.

Solution
‎04-23-2017 10:57 AM
PROC Star
Posts: 8,164

## Re: Calculating date difference

I think that is easier in a datastep:

```data have;
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;
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;
if diff gt 30 then flag=1;
end;
run;
```

Art, CEO, AnalystFinder.com

Contributor
Posts: 22

## 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
Posts: 23,751

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

Contributor
Posts: 22

## 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
Posts: 23,751

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

Contributor
Posts: 22

## 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.
PROC Star
Posts: 8,164

## 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

Contributor
Posts: 22

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

PROC Star
Posts: 8,164

## 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;
if diff gt 30 then flag=1;
end;
run;
```

Art, CEO, AnalystFinder.com

Contributor
Posts: 22

Thank you!!
Super User
Posts: 23,751

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

☑ This topic is solved.