Architecting, installing and maintaining your SAS environment

Calculating Cumulative Returns

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 101
Accepted Solution

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.

Your help is truly appreciated!

Thanks,

John


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

Re: Calculating Cumulative Returns

Posted in reply to mahler_ji

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;

View solution in original post


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

Re: Calculating Cumulative Returns

Posted in reply to mahler_ji

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

Posted in reply to mahler_ji

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: 11,343

Re: Calculating Cumulative Returns

Posted in reply to mahler_ji

RETAIN tells SAS to keep the value of that variable from data row execution to the next data row. The 1 is to set an initial value, which since you wanted a product 1*value would give the correct first value.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 1253 views
  • 0 likes
  • 2 in conversation