Help using Base SAS procedures

Running sum in Array statement

Reply
N/A
Posts: 1

Running sum in Array statement

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..
Valued Guide
Posts: 632

Re: Running sum in Array statement

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=daySmiley Happy;
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]
Super User
Posts: 9,687

Re: Running sum in Array statement

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: febSmiley Happy
[/pre]



Ksharp
Ask a Question
Discussion stats
  • 2 replies
  • 117 views
  • 0 likes
  • 3 in conversation