Hi,
I want to recursively estimate some indicator according to the formula:
indicator_t=0.93*indicator_t-1 + 0.07(series1-0,5)*(series2-0.5)
I run a following code. It produces the results. However, when I export the data to Excel and run calculations manually for period=2, I get different results (see the attached pdf with a print screen). What is wrong with the code?
data have;
input period x1 x2 x3;
datalines;
1 0.509181970 0.449638286 0.023773669827945
2 0.600445186 0.623260991 .
3 0.674457429 0.526989427 .
4 0.433500278 0.563717307 .
5 0.503060657 0.514746800 .
;
run;
data have;
set have;
lag_x3=lag(x3);
run;
%macro loop;
%do i=2 %to 5 /*&nobs*/;
data have;
set have;
if period=&i then do;
x3=0.93*lag_x3 + 0.07*(x1-0.5)*(x2-0.05);
end;
lag_x3=lag(x3);
run;
%end;
%mend;
%loop;
Please copy/paste your "have" data step from here and try to run it. You will find that it does not work, as somewhere there were tabs introduced.
Why do you do it so complicated in SAS? If all you want is to calculate x3, this is the code to do it:
data have;
input period x1 x2 x3;
datalines;
1 0.509181970 0.449638286 0.023773669827945
2 0.600445186 0.623260991 .
3 0.674457429 0.526989427 .
4 0.433500278 0.563717307 .
5 0.503060657 0.514746800 .
;
data want;
set have;
retain lag_x3;
if _n_ = 1 then lag_x3 = x3;
else do;
x3=0.93*lag_x3 + 0.07*(x1-0.5)*(x2-0.05);
lag_x3 = x3;
end;
run;
I haven't thought of doing this differently. The first thing that came to my mind was using macro. Apparently, there's easier way. Thanks!
Or (similar to Kurt Bremser's code):
data want;
set have;
retain lag_x3;
if period>1 then x3=0.93*lag_x3+0.07*(x1-0.5)*(x2-0.5);
output;
lag_x3=x3;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.