Solved
Contributor
Posts: 48

# How to lookup previous value of a raw to calculate new value

Hi everyone,

I have a sasdataset that looks like this. I need to recalculate the total column using values from the previous month and ratio from the current month. I tried using lag and retain function but I couldn’t get the correct answer. Any suggestion?

SAS dataset;

data t1;

input group \$ amt ratio month total;

datalines;

A 1200 0.2 1 240

B 3290 0.1 2 0

C 2000 0.1 3 0

A 1000 0.4 1 240

B 5000 0.5 2 0

C 1430 0.6 3 0

;

run;

data t2;

set t1;

if month gt 2 then do;

total = ratio * lag(total);

end;

retain total;

run;

desire results:

A 1200 0.2 1 240

B 3290 0.1 2 24

C 2000 0.1 3 2.4

A 1000 0.4 1 240

B 5000 0.5 2 120

C 1430 0.6 3 72

Accepted Solutions
Solution
‎04-25-2017 02:14 PM
Super User
Posts: 6,898

## Re: How to lookup previous value of a raw to calculate new value

Here's a way that should work:

data t2;

prior_total = total;

set t1;

if month > 1 then total = ratio * prior_total;

run;

A couple of notes ... you used gt as the comparison.  But the results are consistent with ge, not gt (hence month > 1 in my code).  Also, there is no need to retain TOTAL.  Since it is part of an incoming SAS data set, it is automatically retained.  It's up to you if you want to drop PRIOR_TOTAL.  You might want to keep it the first time, to better understand how the program works.

All Replies
PROC Star
Posts: 1,400

## Re: How to lookup previous value of a raw to calculate new value

Looking at your desired data output, I'm guessing that this has to happen only for Group B and C and not when the group is equal to A?

Super User
Posts: 23,928

## Re: How to lookup previous value of a raw to calculate new value

You can't calculate LAG conditionally.

Calculate lag outside of the IF condition and then you can use it in the IF condition.

Solution
‎04-25-2017 02:14 PM
Super User
Posts: 6,898

## Re: How to lookup previous value of a raw to calculate new value

Here's a way that should work:

data t2;

prior_total = total;

set t1;

if month > 1 then total = ratio * prior_total;

run;

A couple of notes ... you used gt as the comparison.  But the results are consistent with ge, not gt (hence month > 1 in my code).  Also, there is no need to retain TOTAL.  Since it is part of an incoming SAS data set, it is automatically retained.  It's up to you if you want to drop PRIOR_TOTAL.  You might want to keep it the first time, to better understand how the program works.

Frequent Contributor
Posts: 93

## Re: How to lookup previous value of a raw to calculate new value

``````data t1;
input group \$ amt ratio month total;
datalines;
A 1200 0.2 1 240
B 3290 0.1 2 0
C 2000 0.1 3 0
A 1000 0.4 1 240
B 5000 0.5 2 0
C 1430 0.6 3 0
;
run;

data t2;
set t1_NEW;
FORMAT NEWTOTAL 8.2;
retain NEWTOTAL;
IF MONTH = 1 THEN DO;
NEWTOTAL = TOTAL;
END;
if month GE 2 then do;
NEWTOTAL = ratio * NEWTOTAL;
end;
run;``````

Edit: Sorry, forgot the final run command!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 4 replies
• 167 views
• 0 likes
• 5 in conversation