Solved
Contributor
Posts: 25

# Iterative calculation using lag variable

Hi,

i need help in calculating iterative processing that has a lag variable. I do not have ETS package so it is getting complicating for me to get an idea.

below is the sample dataset.

a  b  c  laga

5  2  3  .

12  3  4  5

21  4  5  12

formula is a=lag(a)+b+c; laga is lag of variable a.  a itself is a calculated field.

i am having trouble in comingup with an idea to calculate this, i tried retain statement and output but it did not work. can you guys help me with it?

Thanks,

RK

Accepted Solutions
Solution
‎04-20-2015 06:22 PM
Super User
Posts: 3,919

## Re: Iterative calculation using lag variable

You can do this easily without lag:

data have;

input b c;

cards;

2  3

3  4

4  5

;

run;

data want;

set have;

retain last_a;

a = sum(last_a, b, c);

output;

last_a = a;

run;

All Replies
Super User
Posts: 23,752

## Re: Iterative calculation using lag variable

Can you post what you've tried.

Contributor
Posts: 25

## Re: Iterative calculation using lag variable

Hi Reeza,

assuming that above is a dataset with variables b & c.

data test2;

set test;

retain laga lagtemp;

if _n_=1 then laga=0;

else laga=lagtemp;

a=laga+b+c;

lagtemp=lag(a);

output;

run;

Thanks,

RK

Solution
‎04-20-2015 06:22 PM
Super User
Posts: 3,919

## Re: Iterative calculation using lag variable

You can do this easily without lag:

data have;

input b c;

cards;

2  3

3  4

4  5

;

run;

data want;

set have;

retain last_a;

a = sum(last_a, b, c);

output;

last_a = a;

run;

Contributor
Posts: 25

## Re: Iterative calculation using lag variable

Regards,

RK

🔒 This topic is solved and locked.

Discussion stats
• 4 replies
• 756 views
• 0 likes
• 3 in conversation