Lapis Lazuli | Level 10

## Calculate the Surgery_Date from Surgery_Duration value

Dear Experts,

I want to calculate the Surgery_date value from the Surgery_Duration.

Kindly go through the given sample dataset.

``````data have;
input ID Record_date mmddyy10. Duration DuratIn\$.;
format Record_date mmddyy10.;
cards;
101 03/05/2001 7.6 Years
102 04/02/2001 6 Months
103 03/08/2003 2.06 Years
;``````

Expected OP:

 ID Record_date Duration DuratIn DateonSurg 101 03/05/2001 7.6 Year 09/05/2008 102 04/02/2001 6 Month 10/02/2001 103 03/08/2003 2.06 Year 03/14/2005

Please Suggest some ideas to solve the case.

Much Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Calculate the Surgery_Date from Surgery_Duration value

Maybe something like this, but i am not sure that i fully understood the decimal place and how they should be used in the calculation.

``````data want;
set have;

attrib DateonSurg length=8 format=mmddyy10.;

select (DuratIn);
when ('Years') do;
y = int(Duration);
m = int((Duration - y) * 10);
d = int((Duration - y - m/10) * 100);
end;
when ('Months') do;
y = 0;
m = int(Duration);
d = 0; /* maybe wrong, no data to check */
end;
otherwise put 'ERROR: DuratIn' DuratIn 'is not yet implemented';
end;

DateOnSurg = intnx('Year', Record_date, y, 's');
DateOnSurg = intnx('month', DateOnSurg, m, 's');
DateOnSurg = intnx('day', DateOnSurg, d, 's');

drop y m d;
run;``````
9 REPLIES 9

## Re: Calculate the Surgery_Date from Surgery_Duration value

In the last line of your example, you don't add 2.06 years, but 2 years an 6 days, is this correct?

``````data have;
input ID Record_date mmddyy10. Duration DuratIn\$.;
format Record_date mmddyy10.;
cards;
101 03/05/2001 7.6 Years
102 04/02/2001 6 Months
103 03/08/2003 2.06 Years
;``````

Is this the version of your data we should use, or will there be further changes?

Lapis Lazuli | Level 10

## Re: Calculate the Surgery_Date from Surgery_Duration value

Sorry for an inconvenience @andreas_lds

You are absolutely right (2.6 years). There are no further changes required.

I have to calculate the surgery date using the Record_date and Surgery_Duration. That's it

Thanks!

## Re: Calculate the Surgery_Date from Surgery_Duration value

Maybe something like this, but i am not sure that i fully understood the decimal place and how they should be used in the calculation.

``````data want;
set have;

attrib DateonSurg length=8 format=mmddyy10.;

select (DuratIn);
when ('Years') do;
y = int(Duration);
m = int((Duration - y) * 10);
d = int((Duration - y - m/10) * 100);
end;
when ('Months') do;
y = 0;
m = int(Duration);
d = 0; /* maybe wrong, no data to check */
end;
otherwise put 'ERROR: DuratIn' DuratIn 'is not yet implemented';
end;

DateOnSurg = intnx('Year', Record_date, y, 's');
DateOnSurg = intnx('month', DateOnSurg, m, 's');
DateOnSurg = intnx('day', DateOnSurg, d, 's');

drop y m d;
run;``````
Lapis Lazuli | Level 10

## Re: Calculate the Surgery_Date from Surgery_Duration value

Dear @andreas_lds  . Your code works well.

what is the alternate line if Days exist for

``d = int((Duration - y - m/10) * 100);``

## Re: Calculate the Surgery_Date from Surgery_Duration value

@Sathish_jammy wrote:

Dear @andreas_lds  . Your code works well.

what is the alternate line if Days exist for

``d = int((Duration - y - m/10) * 100);``

Sorry, but i don't understand the question.

Lapis Lazuli | Level 10

## Re: Calculate the Surgery_Date from Surgery_Duration value

I would suggest to calculate the number of days and then just adding that to the record_date to get your DateonSurg.

Super User

## Re: Calculate the Surgery_Date from Surgery_Duration value

Here could give you a start.

``````data have;
input ID Record_date : mmddyy10. Duration DuratIn \$;
format Record_date mmddyy10.;
cards;
101 03/05/2001 7.6 Years
102 04/02/2001 6 Months
103 03/08/2003 2.6 Years
;

data want;
set have;
if DuratIn='Years' then do;
n=ceil(duration);
do i=Record_date to intnx('year',Record_date,n);
if round(yrdif(Record_date,i,'act/act'),0.1)=round(Duration,0.1) then do;
want=i;leave;end;
end;
end;
format want mmddyy10.;
run;

``````
Lapis Lazuli | Level 10

## Re: Calculate the Surgery_Date from Surgery_Duration value

Dear @Ksharp

Thanks for your time and consideration. Your code works well for Year rows.

The Month parameter looks empty.

Could you please go through the month parameter.

Super User

## Re: Calculate the Surgery_Date from Surgery_Duration value

If you want take .6 as semi-year , . 2 as two months ,

I think @andreas_lds  has already given you great solution.

Discussion stats
• 9 replies
• 800 views
• 3 likes
• 4 in conversation