Ruby proves there is life after SAS

The first programming language I learned, 36 years ago, was BASIC, followed by Fortran the next semester. A couple of years later, my employer had me learn COBOL, FORESIGHT and M (the successor to MUMPS). Haven’t touched either in decades and of the three, COBOL is the only one that I’m sure still exists.

In 1982, I had my first SAS class, and I was around when PROC CATMOD (for categorical models) was a new idea, and lo-o-ng before things like structural equation models with SAS (that’s crazy talk! Learn LISREL for that.) I learned SAS macro language and forgot the % on %TO about 735,333 times. All the %eval and quoting functions drove me crazy …

… but in the end, sooner or later, I managed to bash whatever I needed into shape and my programs would run.

I decided I needed to do something new. In part, because I like new challenges. There have been points in my career when I would get so bored with what I was doing that I would write statements starting with the semi-colon and working backwards. You know then that your work has gotten too redundant.

Also, SAS has been around a LONG time now and that means that much of the way we do things is because whatever new statements, functions and procedures are written have to be integrated with the million lines or so of existing code. Much of that code was written before web pages, twitter and other essential fibers of our data digital lives were a consideration. The result is that you CAN parse text with SAS but it’s not really optimal.

So… after much soul-searching, or about as much soul-searching as could be done in the time to drink one glass of Chardonnay, I settled on Ruby. Why Ruby? I wanted something relatively new that would not have been encumbered in writing it with a mountain of legacy code. I was interested in analyzing data on the web primarily. The projects I had in mind were not going to be using petabytes of data but rather focused on a well-defined set, so the ability to handle ‘big data’ didn’t even figure in there.

And, I saw a couple of Ruby books, like Peter Cooper’s Beginning Ruby: From novice to professional, and The Little Book of Ruby that were very simple to follow. There were also a lot of videos available on-line, many so amateurishly produced they were unintentionally funny, but I wasn’t looking for Macbeth, I was looking for information, so that was fine.

Could I just as easily decided to pick up Python or Perl? Sure. But I started with Ruby for no particular reason and it was just SO easy to learn. Your mileage may vary. Some people who have never programmed before found Ruby, and the same resources I used to learn it, to be completely incomprehensible.

Now we come to the whole point of this post… If you’ve used SAS and you are thinking of picking up another language, you’ll find Ruby to be a piece of cake. Take this simple example from Cooper’s book"text.txt").each do |line|
line_count += 1
text << line

Of course, you recognize the File.Open as opening a text file (similar to the good old FILENAME statement. Then there is your DO loop with the DO and the END. The line_count += 1 is not identical, but similar enough to the syntax of line_count + 1 used in SAS that you could probably figure that it serves the same purpose of incrementing the variable by 1.

Or this example, from The Little Book of Ruby. If you aren’t familiar with SAS macro language it probably looks like something an Ewok would say.

puts( "\n\t#{(1 + 2) * 3}" )

On the other hand, if you are familiar with all the need to learn about masking special characters and specify when a value to an argument is to be interpreted as anything but text, you probably said,

“I’ll bet that goes to a new line, tabs over and then interprets (1+2) *3 and prints it out.”

You would be exactly right.

If you’ve worked with ODS much, you are familiar with the idea of inheritance, with different types of tables having certain characteristics and subclasses of that type inheriting all of those characteristics, even if you haven’t used those precise terms.  PROC TEMPLATE, which I avoid like the plague, even though Cynthia Zender tells me it will catch me eventually, is another example, where you can create a new template based on an existing one and it inherits all of the characteristics of the parent template.

Ironically, it was working with macros and ODS that drove me to look for a second language. I just thought there must be an easier way to work with text and I sure found it.

With books out there like R for SAS and SPSS users, Stata for SAS and SPSS users, etc. etc. I can’t believe no one has written a book Ruby for SAS users. It seems like it would be a natural fit. You could do the text processing with Ruby, which is free, easy to learn, and more than that, does not take a staff of Jedi Knights to install, unlike SAS Business Intelligence solutions. Then, you could pass the file, now structured data, back to SAS for statistical analysis.

After that, as Lewis Carroll said, it’s “Oh frabjous day!” and your life is beamish.

kid with lollipopRemember how you felt when you were a little kid at Disneyland and you got those lollipops as big as your face? Yeah, when it all works, you feel like that.

Similar Posts


  1. Wow! I am really excited to have stumbled on your blog. I am in the same boat(although I have not been programming in SAS for anywhere near the length of time you have/had). I also have found that Ruby functionality combined with SAS for analysis is a hidden gem. I second your statements about the similarities between the two languages. The only information (other than this blog) that I have found is :

    Please keep us updated on your progress.

    Also, I have found that Beginning Ruby by Peter Cooper is one of the best instructional programming books out there. I’m happy to see that you’re reading it too!

    …I agree with Bob, you should write a book!

Leave a Reply

Your email address will not be published. Required fields are marked *