Contributor
Posts: 20

How to perform a running calculation?

I am trying to calculate a running total in SAS.

Here are what the columns look like.

Month       Return            Index

 1 100 2 1.06 105.57 3 1.00 106.3 4 0.91 106.72 5 0.94 107.29 6 0.95 108.02 7 1.08 108.64

The calculation is as follows. Index _month_2 = Return_2 * Index_month_1

Index_month_3 = Return_3 * Index _month_2

And so on,

Super User
Posts: 23,776

Re: How to perform a running calculation?

Here's how to calculate a lagged return.

But not sure if the data was present and the numbers rounded or if it wasn't in the original data.

data have;

input Month Return Index;

cards;

1    .        100.0

2    1.06    105.57

3    1.00    106.30

4    0.91    106.72

5    0.94    107.29

6    0.95    108.02

7    1.08    108.64

;

run;

data want;

set have;

index_return=return*lag(index);

run;

Contributor
Posts: 20

Re: How to perform a running calculation?

Sorry I should clearer. Here is what the data looks like in SAS.

month         Return           Index

 1 100.0 2 1.06 3 1.00 4 0.91 5 0.94 6 0.95 7 1.08

I would like to populate the variable Index. The starting number is 100.

Super User
Posts: 23,776

Re: How to perform a running calculation?

Here's one way, but assumes your 100 isn't in the dataset at first.

data have;

input Month Return ;

cards;

1    .

2    1.06

3    1.00

4    0.91

5    0.94

6    0.95

7    1.08

;

run;

data want;

set have;

retain index 100;

if _n_ = 1 then index=100;

else index=return*index;

run;

Contributor
Posts: 20

Re: How to perform a running calculation?

Perfect. Thank you. Also, thank you about the Microsoft add in. Never knew about it.

Todd

Discussion stats
• 4 replies
• 208 views
• 4 likes
• 2 in conversation