Tag: SAS

Fibonacci sequence in R and SAS

Because the Fibonacci sequence is simply defined by recursion, it makes for an elegant programming exercise. Here is one way to do it in SAS, and another way to do it in R. I’ve also included unit testing code to check that it works.

Fibonacci sequence in SAS using a recursive macro:


%macro fib(n);
%if &n = 1 %then 1; * first seed value;
%else %if &n = 2 %then 1; * second seed value;
%else %eval(%fib(%eval(&n-1))+%fib(%eval(&n-2))); * use recursion;
%mend;

* show values 1-5;
%put %fib(1);
%put %fib(2);
%put %fib(3);
%put %fib(4);
%put %fib(5);

* check values 1-10;
%macro check_fib;
%if %fib(1) ne 1 %then %abort;
%if %fib(2) ne 1 %then %abort;
%if %fib(3) ne 2 %then %abort;
%if %fib(4) ne 3 %then %abort;
%if %fib(5) ne 5 %then %abort;
%if %fib(6) ne 8 %then %abort;
%if %fib(7) ne 13 %then %abort;
%if %fib(8) ne 21 %then %abort;
%if %fib(9) ne 34 %then %abort;
%if %fib(10) ne 55 %then %abort;
%put NOTE: OK!;
%mend;
%check_fib;

Fibonacci sequence in R using a recursive function that supports either single integers or a vector of integers:


fib {
if (length(n) > 1) return(sapply(n, fib)) # accept a numeric vector
if (n == 1) return(1) # first seed value
if (n == 2) return(1) # second seed value
return(fib(n-1)+fib(n-2)) # use recursion
}

# print first five Fibonacci numbers
fib(1)
fib(2)
fib(3)
fib(4)
fib(5)

# verify the Fibonacci sequence 1 through 10
(actual (expected all.equal(actual,expected)

For alternative implements, see SAS and R: Example 7.1: Create a Fibonacci sequence. In SAS, Nick Horton calculates the Fibonacci sequence using a DATA STEP, and in R he uses a FOR loop.

Adam Rich responded with his post Fibonacci Sequence in R with Memoization which gives a performance boost by caching the results.

In the comments below, Rick Wicklin referred to his SAS/IML solution that generates the Fibonacci sequence iteratively and Matrices, eigenvalues, Fibonacci, and the golden ratio.

This post first appeared on Heuristic Andrew.

For more posts like this, see Heuristic Andrew.

New Service Matches SAS Analysts and Employers

Analyst Finder is a new service created by well-known SAS programmer Art Tabachneck to help connect analytical professionals with potential employers.  I asked Art a few questions: Why did you create Analyst Finder? The analytical community has helped me throughout my career; and, over the years, I have done my best to return the favor. […]

Takeaway Materials From PharmaSUG SDE, Cary, NC

It’s the first PharmaSUG event I ever attended and it’s great and I plan to submit a paper for PharmaSUG 2015. Honors belonged to Mike Molter and Lex Jansen for their hardcore talks on XML and CDISC Dataset-XML respectively. Dataset-XML is basically ODM based replacement of currently wildly used SAS Version 5 Transport File. It’s […]

Read sas7bdat files in R with GGASoftware Parso library

… using the new R package sas7bdat.parso. The software company GGASoftware has extended the work of myself and others on the sas7bdat R package by developing a Java library called Parso, which also reads sas7bdat files. They have worked out most of the remaining kinks. For example, the Parso library reads sas7bdat files with compressed […]

To Exit or Not Exit, the SAS Ways

The tragedy of life is not that it ends so soon, but that we wait so long to begin it. –by W. M. Lewis So, which is the exit style you prefer, in the following 3 macros (which are all valid SAS codes): /*#1 if branch*/ %macro tragedy_of_life1(ds);     %if %sysfunc(exist(&ds)) %then %do;         proc […]

The Imitation Game @TIGmovie @BletchleyPark

For wholly different reasons, my daughter and I are thrilled to see a date for the premiere of The Imitation Game movie in London and the UK. She’s thrilled because it features Benedict “Sherlock” Cumberbatch; I’m thrilled because it’s a big scree…

Order Matters: A Weird Behavior of SAS ODS Style Options

The codes to push a dataset to Excel (technically XML): ods tagsets.excelxp file=”test.xls”;   *#1; ods tagsets.excelxp options( sheet_name=’test 1′); proc print data=sashelp.class noobs;     var height / style={tagattr=’format:text’} style(column)={cellwidth=.5 in}; run; *#2;  ods tagsets.excelxp options( sheet_name=’test 2′); proc print data=sashelp.class noobs;     var height / style(column)={cellwidth=.5 in} style={tagattr=’format:text’} ; run;   ods tagsets.excelxp close; […]

SAS Certification As a Tool for Professional Development

If you had told me a year ago that I would write a paper about SAS certification for the Western Users of SAS Software 2014 Educational Forum and Conference, I would have been very surprised!  I became a SAS Certified Professional long ago, and that certification expired–long ago!  However, in the past six months, both […]