Solved
Contributor
Posts: 58

# How to retain a value from the previous row to do calculations on current row?

Hello SAS world,

I need your help with the following:

Have:

PatientID            Date

X                      01/22/2012

X                      01/30/2012

X                      01/31/2012

X                      02/02/2012

Want:

PatientID            Date                      Diff

X                      01/22/2012                0

X                      01/30/2012                8

X                      01/31/2012                1

X                      02/02/2012                2

Accepted Solutions
Solution
‎06-27-2017 10:28 AM
Super User
Posts: 8,215

## Re: How to retain a value from the previous row to do calculations on current row?

Here is one way:

```data want;
set have;
by PatientID;
diff=ifn(first.PatientID eq 0,dif(date),0);
run;
```

Art, CEO, AnalystFinder.com

All Replies
Solution
‎06-27-2017 10:28 AM
Super User
Posts: 8,215

## Re: How to retain a value from the previous row to do calculations on current row?

Here is one way:

```data want;
set have;
by PatientID;
diff=ifn(first.PatientID eq 0,dif(date),0);
run;
```

Art, CEO, AnalystFinder.com

Contributor
Posts: 58

Thank you!
Contributor
Posts: 58

## Re: How to retain a value from the previous row to do calculations on current row?

Hello,

Thanks for the earlier help, I need your help to solve the next problem.

From the previous output I got the difference between dates. In the study period, I want to flag patients where there is difference of >120 days and mark all the dates after that >120 days. If there is a patients who have 2 rows of >120 days then I want to mark first >120 days to the 2nd >120 days as "R1" and "R2" for dates greater than 2nd >120 days.

Thanks!

Super User
Posts: 8,215

## Re: How to retain a value from the previous row to do calculations on current row?

You will get more responses if you post your new question as a new question .. not a continuation of the previous question.

Art, CEO, AnalystFinder.com

Posts: 1,163

## Re: How to retain a value from the previous row to do calculations on current row?

another way

``````option missing=0;
data have;
input PatientID \$          Date :mmddyy10.;
lagdate=intck('day',lag(date),date);
format date date9.;
cards;
X                      01/22/2012
X                      01/30/2012
X                      01/31/2012
X                      02/02/2012
;``````
Thanks,
Jag
Contributor
Posts: 47

## Re: How to retain a value from the previous row to do calculations on current row?

You will need a line to reset the calculated difference to zero for the first obs in an ID group. Something like this:

option missing=0;
data have;
input PatientID \$ Date :mmddyy10.;
lagdate=intck('day',lag(date),date);
if PatientID ne lag(PatientID) then lagdate = 0;
format date date9.;
cards;
X 01/22/2012
X 01/30/2012
X 01/31/2012
X 02/02/2012
y 01/22/2017
y 01/30/2017
y 01/31/2017
y 02/02/2017
;
run;

Posts: 1,163

## Re: How to retain a value from the previous row to do calculations on current row?

I don't need the step of lagdate = 0; because in my code i used options missing=0; what this does is it assigns the value 0 to the missing values.
Thanks,
Jag
Valued Guide
Posts: 765

## Re: How to retain a value from the previous row to do calculations on current row?

[ Edited ]

Hi ... OPTIONS MISSING=0 does NOT change values of missing data in a data set to ZERO. It only changes the way they appear when printed.

1169
1170 options missing=0;
1171 data x;
1172 x = 1;
1173 y = .;
1174 z = x + y;
1175 run;

NOTE: Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line) : (Column).
1 at 1174:7

If you print data set X you see ZEROES (due to the OPTIONS statement), but if you look at the LOG, you'll notice that SAS still sees MISSING data.

Syntax
MISSING=<'>character<'>

Syntax Description
<'>character<'>
specifies the value to be printed. The value can be any character. Single or double quotation marks are optional. The period is the default.

Contributor
Posts: 58