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