BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15
Hello
I am trying to perform cumulative multilplication calculation between rows.
Data tbl.
Input p:
Cards:
0.99
0.97
0.95
;
Ron;

Data tbl2;
Set tbl1;
Retain cum_p,
If first.p then cum_p=p;
Else cum_p=cum_p*p;
Run;
3 REPLIES 3
RichardDeVen
Barite | Level 11

Change the logic

if first.p

to

if _N_ = 1

You have a couple typo errors  (dot, colon, comma) where semi-colons should be, so I guess the code is untested. 

ed_sas_member
Meteorite | Level 14

Hi @Ronein 

You cannot use first.p as you have not defined p as a group variable in a BY statement.

See the log:

 NOTE: Variable first.p is uninitialized.

Please try "if _n_=1 then ..." instead:

Data tbl2;
Set tbl1;
Retain cum_p;
If _n_=1 then cum_p=p;
Else cum_p=cum_p*p;
Run;

Best,

Kurt_Bremser
Super User

Testing data step code for example data is not a crime, it is a necessity and a sign of good manners.

 

You can have it much easier by initializing the retained variable to 1:

data have;
input p;
cards;
0.99
0.97
0.95
;

data want;
set have;
retain cum_p 1;
cum_p = cum_p * p;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1065 views
  • 0 likes
  • 4 in conversation