Solved
Contributor
Posts: 29

# Date Differences

Hi All,

I want to find out the future date from the current date with 'x' number of days. the data is as follows.

Now I want to add the number of days mentioned in the 'Count' column in the 'Visit Date' column and it should be stored in Next Visit date Column if the visit date for the current visit is not mentioned.

E.g. - The Patient 101 is having visit date till visit no 4 (21-Jan-16) then to find out his next visit date (for visit no 5), I want the '28' (from count variable) to be added in his visit number 4 date i.e. 21-jan-16 and stored it in 'Next Visit Date' column (21-jan-16 + 28 = 18-Feb-16). if his next visit date (i.e. visit 6) is also missing then the next corresponding count value to be added in 'Next Visit Date' column (18-Feb-16 + 28 = 17-Mar-16) and it is stored in 'Next Visit Date' column and so on.

Now the patient 102 only has one visit date then I want to find out His Next Visit Dates based on the count mentioned in the corresponding observation till visit no 6.

The final result should look like as below.

Thank you very much for your help.

Accepted Solutions
Solution
‎03-30-2016 09:37 AM
Super User
Posts: 10,778

## Re: Date Differences

[ Edited ]

Yeah. Nobody like picture. Nobody would like to type it for you . Post it in text and better is SAS code .

Next time ,don't forget it.

``````data have;
input patient visitno visitdate : date9. count;
format visitdate  date9. ;
cards;
101 1 1jan16 1
101 2 2jan16 6
101 3 8jan16 7
101 4 21jan16 13
101 5 . 28
101 6 . 28
101 7 . 28
102 1 1feb16 1
102 2 . 6
;
run;

data want;
set have;
by patient ;
retain temp;
if first.patient then call missing(temp);
if not missing(visitdate) then newvisitdate=visitdate;
else newvisitdate=temp+count;
temp=newvisitdate;
drop temp;
format newvisitdate date9.;
run;``````

All Replies
Super User
Posts: 5,878

## Re: Date Differences

Have the visit date from the previous observation stored in a retained variable, and use that as part of the calculation for the next visit date.

Data never sleeps
Posts: 5,526

## Re: Date Differences

Just a note. Posting your data as a picture will not get you a tested solution. My SAS doesn't read pictures.

PG
Solution
‎03-30-2016 09:37 AM
Super User
Posts: 10,778

## Re: Date Differences

[ Edited ]

Yeah. Nobody like picture. Nobody would like to type it for you . Post it in text and better is SAS code .

Next time ,don't forget it.

``````data have;
input patient visitno visitdate : date9. count;
format visitdate  date9. ;
cards;
101 1 1jan16 1
101 2 2jan16 6
101 3 8jan16 7
101 4 21jan16 13
101 5 . 28
101 6 . 28
101 7 . 28
102 1 1feb16 1
102 2 . 6
;
run;

data want;
set have;
by patient ;
retain temp;
if first.patient then call missing(temp);
if not missing(visitdate) then newvisitdate=visitdate;
else newvisitdate=temp+count;
temp=newvisitdate;
drop temp;
format newvisitdate date9.;
run;``````
Contributor
Posts: 29

## Re: Date Differences

Please accept my apologies. I will not post images hereafter.

Thank you very much for your help.

attachments. -##
Posts: 1,248

## Re: Date Differences

Once you have verified that Ksharp's suggestion solves your problem, it would be good if you marked his reply as the accepted solution (by clicking the respective button "Accept as Solution"). Please see this article why this is important.

Contributor
Posts: 29

## Re: Date Differences

Thank you for the help. It is working perfectly.

Posts: 1,248

## Re: Date Differences

[ Edited ]

There are "Accept as Solution" buttons in every post and, of course, you should click the one pertaining to the solution. That's why I underlined the word "respective" in 'by clicking the respective button "Accept as Solution"'. Unfortunately, you still managed to click the wrong button. Not sure if you can change it. But you could at least give likes to the actual solution (by pressing the respective orange "applause" button).

Edit: Maybe Super Users or community administrators (@ChrisHemedinger?) can correct this.

Posts: 5,526

## Re: Date Differences

The super users don't seem to have that power. I think the op can un-accept a solution by clicking the "Not the solution" choice on the wrong answer's menu. (untested)

PG
Contributor
Posts: 29

## Re: Date Differences

I am sorry. I'm a new user here. And I was not aware of it.  Next time onwards,  I will keep it in my mind.

Also,  the solution provided by Ksharp is correct. I'm able to get the desired results with it.

Thank you very much for your help.

Community Manager
Posts: 3,428