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
- /
- SAS Procedures
- /
- Running sum in Array statement

Topic Options

- Subscribe to 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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

11-28-2010 04:49 PM

Hello,

I have 365 variables each for a day in particular year (i.e. Jan01--Dec31). I need to find sum of 60 days only. I used array statement to find sum, but i need to mansion all 60 variables. Can I use "Total=Sum (of var {i+1}--var{i+60})" ? It is not working with my program.

Any suggestions highly appreciated.

Thank you..

I have 365 variables each for a day in particular year (i.e. Jan01--Dec31). I need to find sum of 60 days only. I used array statement to find sum, but i need to mansion all 60 variables. Can I use "Total=Sum (of var {i+1}--var{i+60})" ? It is not working with my program.

Any suggestions highly appreciated.

Thank you..

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

Posted in reply to apat86

11-28-2010 07:46 PM

While you can't use array values as list elements, you can do what you want by using a stack. The stack avoids a loop that passes through the array multiple times.

The following silly program creates the first sum and then for each successive sum removes the oldest value and adds the newest (FIFO stack).

[pre]

data days(keep=day;

array days {366} day1-day366;

do i = 1 to 365;

days{i} = 1;

end;

output;

run;

data sums(keep=period sum);

set days;

retain sum .;

array days {366} day1-day366;

period=1;

sum = sum(of day1-day60);

output;

do period = 2 to 306;

sum = sum(of sum, -days{period-1},days{period+60});

output sums;

end;

run;[/pre]

The following silly program creates the first sum and then for each successive sum removes the oldest value and adds the newest (FIFO stack).

[pre]

data days(keep=day;

array days {366} day1-day366;

do i = 1 to 365;

days{i} = 1;

end;

output;

run;

data sums(keep=period sum);

set days;

retain sum .;

array days {366} day1-day366;

period=1;

sum = sum(of day1-day60);

output;

do period = 2 to 306;

sum = sum(of sum, -days{period-1},days{period+60});

output sums;

end;

run;[/pre]

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

Posted in reply to apat86

11-28-2010 08:32 PM

Hi.

Not sure what variables you need to sum.

But SUM function supports not only list input but also colon input.

For example you need to sum January and February.

[pre]

sum(of jan: feb

[/pre]

Ksharp

Not sure what variables you need to sum.

But SUM function supports not only list input but also colon input.

For example you need to sum January and February.

[pre]

sum(of jan: feb

[/pre]

Ksharp