BookmarkSubscribeRSS Feed
usuario_estudo
Calcite | Level 5

I'd like to write a code that would do a recursive multiplication in one column.
I have columns A and B and the idea is to calculate the column C as follows:
When B is different from empty, then B=C.
When B is empty, then the value for the line "i" of C is the value for the line "i" of A times the value for line (i-1) of C, as in the example below:

 

ABC
1166
22727
71212
8.96
3.288
4.1152
2 REPLIES 2
novinosrin
Tourmaline | Level 20
data have;
input A	B	;
cards;
11	6	6
2	27	27
7	12	12
8	.	96
3	.	288
4	.	1152
;

data want;
 set have;
 retain c;
 if b then c=b;
 else c=c*a;
run;
Tom
Super User Tom
Super User

That is not a recursive problem.

Assuming that you meant to set C to the value of B and not the other way around (since the other way around wouldn't produce your expected values).

data expect;
  input A B expect;
cards;
11 6 6
2 27 27
7 12 12
8 . 96
3 . 288
4 . 1152
;
data want;
  set expect ;
  retain c;
  if not missing(b) then c=b;
  else c=a*c;
run;
proc print;
run;
Obs     A     B    expect       c

 1     11     6        6        6
 2      2    27       27       27
 3      7    12       12       12
 4      8     .       96       96
 5      3     .      288      288
 6      4     .     1152     1152

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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