Iterative calculation using lag variable

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

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
Respected Advisor
Posts: 3,063

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;

View solution in original post


All Replies
Grand Advisor
Posts: 17,360

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
Respected Advisor
Posts: 3,063

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

Thank you SASKiwi. it worked.

Regards,

RK

☑ This topic is SOLVED.

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

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