Calcite | Level 5

## Calculating Days within a given year.

Hello, I am trying calculate enrollment days for a given patient in a given year that has a diagnosis of interest within that year.  The criteria is that a patient must have 300 days of enrollment within the same year as their diagnosis.  Does anyone have any code I can use to calculate that any given diagnosis in any year from 2004 to 2012 also has the corresponding 300 days of valid enrollment days for the same year.

E.g diagnosis index date 20th Jan 2011.   Patient must have 300 days combined before and after that date for 2011.  I only want to search in 2011 so i need some logic that searchs number of days prior to diagnosis Indexdate and post DIAGNOSIS  index date but only within the SAME year of diagnosis index date (2011) .

6 REPLIES 6
Calcite | Level 5

## Re: Calculating Days within a given year.

Following on from that  and trying to explain it a bit better if a patient  had a diagnosis of 12apr2012 and their enrollment start date was 12apr2010 and their enrollment end date of 18may 2012 i want to calculate the number of days of enrollment just in the year of diagnosis so only want to return the number of days of enrollment in 2012 and not include any days prior to 2012 or post 2012.

Diamond | Level 26

## Re: Calculating Days within a given year.

Hi,

Have a look at interval function INTX, INTCK.  With these and you two dates, you can get a result of how many days/months years etc.  Also you could, as dates/times are numeric, just do end_date - start_date (sometimes +1 depending on specs).

Super User

## Re: Calculating Days within a given year.

PPost a sample of what your data looks like and what you want results to look like.

Super User

## Re: Calculating Days within a given year.

What happens if the "enrollment" is considerably before the end date, such as enrollment of 10 Dec 2012 and end date of 20 July 2014?

If you have the dates as SAS date values then you might want:

Days = min (enddate-enrollmentdate, endate - mdy(1,1,year(enddate)));

Calcite | Level 5

## Re: Calculating Days within a given year.

Sounds like you need some if statements, if I am understanding correctly?

Would this work for you?

data temp;

format enroll_start_date enroll_end_date diag_date date9.;

enroll_start_date = '12apr2010'd;

enroll_end_date = '18may2012'd;

diag_date = '12apr2012'd;

if year(enroll_start_date) = year(diag_date) then

enroll_days = enroll_end_date - enroll_start_date;

else if year(enroll_end_date) = year(diag_date) then

enroll_days = enroll_end_date - mdy(1,1,year(enroll_end_date));

else

enroll_days = 0;

run;

Calcite | Level 5

## Re: Calculating Days within a given year.

I am not sure what your enrollment data looks like but if you only want the number of enrollment days in the diagnosis year then you need to get the beginning and ending enrollment period for that particular year.

data temp;

format enroll_start_date enroll_end_date diag_date mmddyy10.;

enroll_start_date = '01mar2010'd;

enroll_end_date = '31may2013'd;

diag_date = '12apr2010'd;

output;

diag_date = '05jan2011'd;

output;

run;

data temp;

set temp;

format yr_enroll_start_date yr_enroll_end_date mmddyy10.;

/*****************************************************************/

/*    create yr enrollment information for the yr of the diag */

/******************************************************************/

/* if the enrollment end date is after the end of the year of the diag,

then set the yr enrollment end date to the last day of the diag year */

if year(enroll_end_date) > year(diag_date) then

yr_enroll_end_date = intnx('year', diag_date, 0, 'ending');

else yr_enroll_end_date = enroll_end_date;

/* if the enrollment start date is prior to the year of the

diag, then set the yr enrollment start date to the beginning of the

diag year */

if year(enroll_start_date) < year(diag_date) then

yr_enroll_start_date = intnx('year',diag_date,0,'beginning');

else yr_enroll_start_date = enroll_start_date;

days = yr_enroll_end_date - yr_enroll_start_date + 1;

if days >= 300 then valid_enrollment = 'Y';

else valid_enrollment = 'N';

run;

Discussion stats
• 6 replies
• 2399 views
• 0 likes
• 6 in conversation