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.
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,
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.