BookmarkSubscribeRSS Feed
chris2377
Quartz | Level 8

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;
4 REPLIES 4
chris2377
Quartz | Level 8
I've made a mistake in the code (x2-0.05) instead (x2-0.5). It's all OK now. Please disregard the post. Sorry for the mess
Kurt_Bremser
Super User

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;
chris2377
Quartz | Level 8

@Kurt_Bremser 

 

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!

FreelanceReinh
Jade | Level 19

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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