Do Loop Fibonacci sequenc

Reply
New Contributor
Posts: 2

Do Loop Fibonacci sequenc

How can I write a code that uses terms in the Fibonacci sequence whose values do not exceed four million, but find the sum of the even-valued terms? I think I understand how to write the script for the Fibonacci series but the even number sum is giving me an error 

Frequent Contributor
Posts: 144

Re: Do Loop Fibonacci sequenc

[ Edited ]

Copied from 

https://communities.sas.com/t5/SAS-Procedures/how-to-display-a-fibonacci-series-using-base-sas-conce...

here a data step to create a fibonacci serie

 

data temp(keep=a);
	a=0;
	b=1;
	n=100;
	do while (a<4e6);
	c=a+b;
	output;
	a=b;
	b=c;
	count+1;
	end;
run;

data fibonacci;
set temp;
if _N_>1;
run;

Doing this you have the fibonacci serie. Then you must sum the even terms, to do it use mod function to know if it is even or not

proc sql;
	select sum(a) as sum
	from fibonacci
where mod(a,2)=0;
quit;
Ask a Question
Discussion stats
  • 1 reply
  • 385 views
  • 0 likes
  • 2 in conversation