BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mahler_ji
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
ballardw
Super User

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;

mahler_ji
Obsidian | Level 7

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

ballardw
Super User

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.

suga badge.PNGThe SAS Users Group for Administrators (SUGA) is open to all SAS administrators and architects who install, update, manage or maintain a SAS deployment. 

Join SUGA 

Get Started with SAS Information Catalog in SAS Viya

SAS technical trainer Erin Winters shows you how to explore assets, create new data discovery agents, schedule data discovery agents, and much more.

Find more tutorials on the SAS Users YouTube channel.

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