Create SAS Code from R ‘tree’ Objects

I recently was faced with the desire to port some tree models developed in R to SAS so I could score a large database. To me this makes sense as SAS is better with large files (or at least so as not to offend anyone, I am better with large files in SAS).

I started with a web search and identified the following post:

Model decision tree in R, score in Base SAS

which basically talked about the same situation, and created code to write the SAS code. Unfortunately for me they used package party::ctree for the trees, while I use package tree.

Inspired by the great idea, I developed a code that generates the if statements to insert into  the Data Step for ‘tree’ objects developed by package tree. It is likely not the most efficient way to move through the tree, but it works (I have not dealt with binary trees in code since programming 2 in college – in Pascal of all languages – over 20 years ago). I welcome any suggestions.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.


tree2sas <- function (treeobj,predictor_name,filename) {

#Developed by Mario Segal. 

frame <- treeobj$frame
frame$printed <- 0;
frame$close <- 0;

code <- data.frame(line=””,stringsAsFactors =F)
j <- 1
end <- row.names(frame[dim(frame)[1],])
mymax <- 0;
while (row.names(frame[i,]) != end) {
i <- mymax+1
#cycle the process until you get to the last leaf on the left from current node;
while (frame[i,]$var != ‘<leaf>’) {
code[j,] = paste(‘if’,frame[i,]$var,frame[i,]$splits[1,’cutleft’],’then do;’)
i<- i+1;

mymax <- max(i,mymax)
# we are at the farthest left possible, so write the assignment;
code[j,] = paste(predictor_name,’ = ‘,”‘”,frame[i,]$yval,”‘”,”;”,sep=””)
#since I printed the prediction mark as printed;
frame[i,7] <- 1;
j<- j+1

#move up until you find the first not printed. To account for the way back from right;
#check also if you are back at node #1 if not stop
while (frame[i,]$printed==1) {
if (i==1) {break}
if ( frame[i,]$close==0) {
#if not not closed ,close it, I am closing leafs as I prinmt them so otherwise I duplicate;
code[j,] = ‘end;’
j <- j+1;
frame[i,8] <- 1;

if (i == 1 & mymax==dim(frame)[1]) {break}
code[j,] = paste(‘if’,frame[i,]$var,frame[i,]$splits[1,’cutright’],’then do;’)
j<- j+1;
frame[i,7] <- 1;
#the next right one here will be max+1;


for (k in 1:dim(code)[1]) {



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s