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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 1079 views
  • 0 likes
  • 3 in conversation