## Calculating Cumulative Returns

Solved
Frequent Contributor
Posts: 101

# Calculating Cumulative Returns

Hello,

I have data that takes the following form:

data have;

input month y;

1 1

2 3

3 2

4 3

5 2

6 1

7 2

8 2

9 2

10 3

;

run;

And I want to create a column that, each month, calculates the cumulative product of all of the variable Y.  So for month 1, it would have a value of 1, then for month 2, it would be 3, then for month 3, it would be 6, and for month 4, it would be 18.

I could do this by transposing the columns, but I have hundreds of observations I need to do this with, so I am trying to find a more elegant way.

Thanks,

John

Accepted Solutions
Solution
‎06-26-2014 01:57 PM
Super User
Posts: 12,316

## Re: Calculating Cumulative Returns

Does this do what you expect?

data have;

input month y;
retain product 1;

product = product*y;

datalines;

1 1

2 3

3 2

4 3

5 2

6 1

7 2

8 2

9 2

10 3

;

run;

All Replies
Solution
‎06-26-2014 01:57 PM
Super User
Posts: 12,316

## Re: Calculating Cumulative Returns

Does this do what you expect?

data have;

input month y;
retain product 1;

product = product*y;

datalines;

1 1

2 3

3 2

4 3

5 2

6 1

7 2

8 2

9 2

10 3

;

run;

Frequent Contributor
Posts: 101

## Re: Calculating Cumulative Returns

Yes it does!  Thank you very much.

Also, if you don't mind, can you explain what the retain function is doing here so that I can understand how this is working?

Thank you so much!

John

Super User
Posts: 12,316