Thank you all for valuable suggestions. I modified the first code by Søren, so it actually does the trick. It puts the data step with a retain statement on hold, starts another data step, gets the value created through this auxiliary data step and sends it to the main data step without interruption of the retain statement. Using DOSUBL may be faster. Here is the code: data values;
infile datalines;
input message $ value;
datalines;
new_good 0.5
retain .
new_good 0.7
retain .
retain .
new_bad 0.6
retain .
new_good 0.3
retain .
retain .
retain .
;
run;
data ultimately_good_value;
x=1;
run;
%macro data_step();
data temp;
set ultimately_good_value;
call symput('macro_x',x);
run;
%mend data_step;
proc fcmp outlib=work.funcs.test;
function getval();
rc = run_macro('data_step', macro_x );
return (macro_x);
endsub;
quit;
options cmplib=work.funcs;
data values_want;
set values;
retain true_value .;
if message = "new_good" then true_value=value;
if message = "new_bad" then
true_value = getval() ;
run;
The dataset ultimately_good_value could also be created within the data_step macro. Sorry for not giving out all the details. I can't create a macro variable before the data step because in my original code I do not know what x is going to be equal to. Every time x is determined based on several retained values, message flag and some other variables from a different dataset. This created a viscious circle: I do not know where to look for x in each case before I run a data step with a retain statement, but if I start this data step, I do not know how to fetch x once I learn where it is (and future values of x depend on the previous ones).
... View more