Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The Fibonacci numbers have many mathematical relationships and have been discovered repeatedly in nature. They are constructed as the sum of the previous two values, initialized with the values 1 and 1.

A pdf of this example is available here.

SAS
In SAS, we use the lag function (section 1.4.17, p. 22) to retrieve the last value.

data fibo;
do i = 1 to 10;
fib = sum(fib, lag(fib));
if i eq 1 then fib = 1;
output;
end;
run;
proc print data=fibo;
run;


This generates the following output:

Obs i fib
1   1   1
2   2   1
3   3   2
4   4   3
5   5   5
6   6   8
7   7  13
8   8  21
9   9  34
10 10  55

R
In R we can loop over an array to perform the same job.

len

This generates the following output:

> fibvals
[1] 1 1 2 3 5 8 13 21 34 55