turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Iterative calculation using lag variable

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-20-2015 05:24 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to RajK

04-20-2015 06:22 PM

You can do this easily without lag:

**data **have;

input b c;

cards;

2 3

3 4

4 5

;

**run**;

da**ta **want;

set have;

retain last_a;

a = sum(last_a, b, c);

output;

last_a = a;

**run**;

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to RajK

04-20-2015 05:32 PM

Can you post what you've tried.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

04-20-2015 05:44 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to RajK

04-20-2015 06:22 PM

You can do this easily without lag:

**data **have;

input b c;

cards;

2 3

3 4

4 5

;

**run**;

da**ta **want;

set have;

retain last_a;

a = sum(last_a, b, c);

output;

last_a = a;

**run**;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SASKiwi

04-20-2015 07:57 PM

Thank you SASKiwi. it worked.

Regards,

RK