Preparing R Model Files to Use with SAS/IML

Build an R Model

Use the following SAS code to create an R model and save it in the outmodel.rda model component file:
/* Define the libref to the SAS input data set.   */

libname libref "path-to-input-data-set";

/* Use PROC IML to export the SAS input data set to the R input data set. */

proc iml;
	run ExportDatasetToR("input-data-set" , "R-matrix-input");
	
/* Submit the model-fitting R code.   */

submit /R;
		attach(R-matrix-input)

		# -----------------------------------------------
		# FIT THE MODEL 
		# -----------------------------------------------
		model-name<- model-fitting-function

		# -----------------------------------------------
		# SAVE THE PARAMETER ESTIMATE TO LOCAL FILE OUTMODEL.RDA 
		# -----------------------------------------------
		save(model-name, file="path/outmodel.rda")
	endsubmit;
run;
quit;
Supply the following values:

path-to-input-data-set

is the path to the library where the input data set is stored.

input-data-set

is the name of the input data set.

R-matrix-input

is the R input data.

model-name

is the name of the model.

model-fitting-function

is the R formula that is used to fit the model.

path

is the path to where outmodel.rda is to be stored.

Here is an example of creating an R model using the HMEQ train data set as the SAS input data set:
libname mmsamp "!sasroot\mmcommon\sample";
proc iml;
	run ExportDatasetToR("mmsamp.hmeq_train" , "mm_inds");
	submit /R;
		attach(mm_inds)

		# -----------------------------------------------
		# FIT THE LOGISTIC MODEL 
		# -----------------------------------------------
		logiten<- glm(BAD ~ VALUE + factor(REASON) + factor(JOB) + DEROG + 
                   CLAGE + NINQ + CLNO , family=binomial)

		# -----------------------------------------------
		# SAVE THE PARAMETER ESTIMATE TO LOCAL FILE OUTMODEL.RDA 
		# -----------------------------------------------
		save(logiten, file="c:/RtoMMfiles/outmodel.rda")
	endsubmit;
run;
quit;

Prepare an R Model Template File

SAS Model Manager provides three R model templates that you can use as a model template for your R model:
  • RClassification
  • RPrediction
  • RAnalyticalmodel
To view these model templates:
  1. From the Projects category view, Click Template and select Manage Templates. The Manage Templates appears.
  2. Select an R model template and click Edit File.
  3. Review the model template to make sure that it contains all of the model component files and properties for your model. If it does, you can use this template to import your R model. To customize the model template, you can copy the XML content from one of the supplied template files and make modifications using a text editor. You can then create a new model template using the modified XML content and the model template to the SAS Content Server.
    To create a custom R model template, see Model Template Component Files and User-Defined Model Templates.

Prepare R Model Component Files

R Model Component Files for Executing R Models Using SAS/IML

To submit R models from SAS Model Manager using SAS/IML, you need several model component files:
  • modelinput.sas7bdat
  • modeloutput.sas7bdat
  • target.sas7bdat
  • inputvar.xml
  • outputvar.xml
  • targetvar.xml
  • outmodel.rda
  • score.r
  • score.sas
  • training.r (not required if you do not retrain your R model)
  • training.sas (not required if you do not retrain your R model)
You create the modelinput.sas7bdat, modeloutput.sas7bdat, target.sas7bdat, inputvar.xml, outputvar.xml, and targetvar.xml files as you would for importing a SAS code file. For more information, see Model Template Component Files.
The remaining files, outmodel.rda, score.r, score.sas training.r, and training.sas require additional file preparation.

Create outmodel.rda

The outmodel.rda file contains the output parameter estimate. This file is used by SAS Model Manager to register and score the model. You create outmodel.rda when you build an R model. See Build an R Model. The outmodel.rda file uses the R function save() to save the scoring results.
Here is the syntax of an outmodel.rda file:
save(model-name, file="path/outmodel.rda")
Supply the following values:

model-name

is the name of the R model.

path

is the system path to the location where outmodel.rda is stored.

Here is an example outmodel.rda file:
save(logiten, file=”c:/temp/outmodel.rda")

Create score.r

The score.r script is an R script that is used to score data. You can use the following R script to create score.r:
attach(R-matrix-input)

# -----------------------------------------------
# LOAD THE OUTPUT PARAMETER ESTIMATE FROM FILE OUTMODEL.RDA 
# -----------------------------------------------
load('&_mm_scorefilesfolder/outmodel.rda')

# -----------------------------------------------
# SCORE THE MODEL
# -----------------------------------------------
score<- predict(model-name, type="response", newdata=R-matrix-input)

# -----------------------------------------------
# MERGING PREDICTED VALUE WITH MODEL INPUT VARIABLES
# -----------------------------------------------
mm_outds <- cbind(R-matrix-input, score)
Supply the following values:

R-matrix-input

is the name of the input R matrix file that you specified in the ExportDatasetToR function in the IML procedure. See Build an R Model.

score

is the output variable. The value for score must match the output variable that is defined in modeloutput.sas7bdat and outputvar.xml.

model-name

is the name of the R model. The value of model-name must match the R save function model-name argument that is specified in the outmodel.rda file.

Here is an example score.r file:
attach(mm_inds)

# -----------------------------------------------
# LOAD THE OUTPUT PARAMETER ESTIMATE FROM FILE OUTMODEL.RDA 
# -----------------------------------------------
load('&_mm_scorefilesfolder/outmodel.rda')

# -----------------------------------------------
# PREDICT
# -----------------------------------------------
score<- predict(logiten, type="response", newdata=mm_inds)

# -----------------------------------------------
# MERGE THE PREDICTED VALUE WITH MODEL INPUT VARIABLES
# -----------------------------------------------
mm_outds <- cbind(mm_inds, score)

Create score.sas

The score.sas program defines the score test information in a data set and calls the %mmbatch macro. When you submit the %mmbatch macro, the task mm_r_model_train_main completes the following tasks:
  • transforms a scoring data set to an R data frame
  • generates and submits R code for scoring
  • transforms the scored output to a SAS data set for reporting in SAS Model Manager
Here is the score.sas program:
filename tmp catalog "sashelp.modelmgr.mm_include.source";
%include tmp;
filename tmp;

data work.mm_score_task_information;
   length role $ 8;
   length name $ 80;
   length value $ 200;

   role = "input";
   name = "importedData";
   value = "&_mm_inputds";
   output;

   role = "input";
   name = "modelID";
   value = "&_mm_modelID";
   output;

   role = "output";
   name = "exportedData";
   value = "&_mm_outputds";
   output;

   role = "input";
   name = "dataRole";
   value = "output-variable-name";
   output;

   role = "input";
   name = "p_Target";
   value = "output-variable-name";
   output;
run;

/* mm_r_model_score_main is a SAS Model Manager process flow that is used to run */
/* R model scripts using PROC IML.                                               */

%mmbatch(task=mm_r_model_score_main, taskprops= mm_score_task_information);
Supply the following value:

output-variable-name

is the output variable that is defined in modeloutput.sas7bdat or modeloutput.xml.

To print verbose SAS logs, add the following lines before the RUN statement in the previous DATA step:
role = "input";
   name = "_mm_trace";
   value = "ON";
   output;

Create training.r

The training.r script is an R script that is used to build a train model. Use the following script for the training.r file. In the R save function, the path in the file= argument must be &_MM_TrainResultFolder.
You can use the following script to create training.r:
attach(R-matrix-input)

# -----------------------------------------------
# FIT THE LOGISTIC MODEL 
# -----------------------------------------------
model-name<- model-fitting-function

# -----------------------------------------------
# SAVE THE OUTPUT PARAMETER ESTIMATE TO LOCAL FILE OUTMODEL.RDA 
# -----------------------------------------------
save(model-name, file="&_MM_TrainResultFolder/outmodel.rda")
Supply the following values:

R-matrix-input

is the name of the R matrix that is specified in the ExportMatrixToR function that is used to build a model using the IML procedure.

model-name

is the name of the R model.

model-fitting-function

is an R model fitting function, such as lm() or glm().

Here is an example training.r R script to build the HMEQ R train model:
attach(mm_inds)

# -----------------------------------------------
# FIT THE LOGISTIC MODEL 
# -----------------------------------------------
logiten<- glm(BAD ~ VALUE + factor(REASON) + factor(JOB) + DEROG + CLAGE + 
              NINQ + CLNO , family=binomial)

# -----------------------------------------------
# SAVE THE OUTPUT PARAMETER ESTIMATE TO LOCAL FILE OUTMODEL.RDA 
# -----------------------------------------------
save(logiten, file="&_MM_TrainResultFolder/outmodel.rda")

Create training.sas

If you do not need to retrain your R model in SAS Model Manager, you do not need this file.
The training.sas program defines the train task information in a data set and calls the %mmbatch macro. When you submit the %mmbatch macro, the task mm_r_model_train_main completes the following tasks:
  • transforms a training data set to an R data frame
  • generates and submits R code for training
  • registers the training output parameter estimate file in SAS Model Manager
Here is the training.sas file:
filename tmp catalog "sashelp.modelmgr.mm_include.source";
%include tmp;
filename tmp;

data work.mm_train_task_information;
   length role $ 8;
   length name $ 80;
   length value $ 200;

   role = "input";
   name = "trainData";
   value = "&_mm_inputds";
   output;

   role = "input";
   name = "modelID";
   value = "&_mm_modelID";
   output;
run;

/* mm_r_model_train_main is a SAS Model Manager process flow that is used to run */
/* R model scripts using PROC IML.                                               */

%mmbatch(task=mm_r_model_train_main, taskprops= mm_train_task_information);
To print verbose SAS logs, add the following lines before the RUN statement in the previous DATA step:
role = "input";
   name = "_mm_trace";
   value = "ON";
   output;
Last updated: June 12, 2017