BookmarkSubscribeRSS Feed

Tip: Using the SAS Viya Code node in SAS Enterprise Miner 14.3

Started ‎10-13-2017 by
Modified ‎10-13-2017 by
Views 4,220

This tip will help users who have licensed both SAS Enterprise Miner 14.3 and SAS Viya get started with incorporating SAS Viya code -- in particular, the modern, in-memory algorithms available in the SAS Visual Data Mining and Machine Learning procedures -- into their SAS Enterprise Miner flows using the SAS Viya Code node.


You first might want to familiarize yourself with SAS Viya and Cloud Analytic Services (CAS) server concepts like caslibs and CAS LIBNAME engines, and you can find information about those in the SAS Viya Programming section of the documentation found here: SAS 9.4 and SAS Viya 3.2 Programming Documentation


Viya Code node icon.jpgThe SAS Viya Code node, found on the Utility tab in SAS Enterprise Miner 14.3, is similar to the SAS Code node that you are probably already familiar with. It includes a Code Editor where you can enter your custom SAS code, but this Code Editor is pre-populated with a template that has the utility macros for simplifying the integration of your SAS code, which can be SAS Viya code, into SAS Enterprise Miner. While this node can be used as a "stand-alone" node, it will typically be included in a flow, downstream from an Input Data node. The example code shown here assumes the SAS Viya Code node is included in a flow somewhere after an Input Data node that has a target variable assigned.


In the first part of the template that is included in the SAS Viya Code node, shown below, all you need to specify are:


  • the values for the casHost and casPort macro variables.  Note that these do not need to be included if the CAS host and port have already been specified via the em_casHost and em_casPort macro variables in the Project Start Code,
  • the values for the caslib and casuser macro variables to define the caslib where the data is loaded (for example, your personal caslib that is the default: CASUSER) and your user ID, respectively,
  • em_register macro calls for any tables or files for which you want to register a unique file key; that is, tables or files you want displayed or used for plots in the node results.  One such call is included in the example code below, before the em_viya_setmacrovars macro. 

Then the following three macros are included in the template:


  • em_viya_signon to create your CAS session and assign the required libraries
  • em_viya_dataprep to prepare the data (all partitions if data has been partitioned) and upload it to the CAS in-memory execution engine in SAS Viya.  
  • em_viya_setmacrovars to create the macros and macro variables that you can use to represent various attributes about your data in your SAS code, including creating the macro variables for tables or files registered with the em_register macro.  A macro variable called em_user_key is created and can then be used to reference the table or file in your code, and this table or file is saved to the workspace directory (see the SAS Code node chapter of the SAS Enterprise Miner Reference Help for more details on this macro, or refer to the tip Tip: Create Graphs in a SAS Code Node Using %em_report).  A list of the other macros and macro variables is shown in the comment preceding the macro call in the template.


* Setup the CAS information
* Note that the casHost and casPort can be specified in the EM project
* startup code using the em_casHost and em_casPort macro variables.
* %let casHost =;
* %let casPort =;
 %let caslib = CASUSER;
 %let casuser = /* your user ID */;
* Create a CAS session and assign the CAS library.
* Prepare the training data and loads it in CAS
* em_casdata= macro variable identifying the training data
* em_casLib = macro variable identifying the CAS library
* Setting up macro variables and macros in SAS Viya
* Variable Macros:
* em_vars = all variables
* em_class = class variables
* em_interval = interval variables
* em_interval_input = interval inputs
* em_binary_input = binary inputs
* em_nominal_input = nominal inputs
* em_ordinal_input = ordinal inputs
* em_interval_rejected = interval rejected
* em_binary_rejected = binary rejected
* em_nominal_rejected = nominal rejected
* em_ordinal_rejected = ordinal rejected
* em_id = id variables
* em_key = key variable
* Variable:
* em_num_vars = number of variables
* em_num_class = number of class variables
* em_num_interval = number of interval variables
* em_num_interval_input = number of interval inputs
* em_num_binary_input = number of binary inputs
* em_num_nominal_input = number of nominal inputs
* em_num_ordinal_input = number of ordinal inputs
* em_num_interval_rejected = number of interval rejected
* em_num_binary_rejected = number of binary rejected
* em_num_nominal_rejected = number of nominal rejected
* em_num_ordinal_rejected = number of ordinal rejected
* em_num_id = number of id variables
* em_num_key = number of key variable
* em_partitionvar = partition variable
* Target information:
* em_dec_target = target variable
* em_dec_level = measurement level of target variable
* Utility:
* em_partition_statement = partition statement
* Files:
* em_viya_nodedir = temporary node folder
* em_file_scorecode = score code file
* em_file_cdelta_train = metadata file to modify the metadata exported by the node
* em_file_odsfile = ods output file
* em_file_astore = local analytic store file
* em_data_rstore = analytic store CAS table
* em_data_viyaroc = ROC CAS table;
* em_data_viyalift = Lift CAS table;
* em_data_viyafitstat = Fit Statistic CAS table

 %em_register(key=varImportance, type=DATA);




Next in the template of the SAS Viya Code node is a section for you to enter your custom SAS code, which can be a combination of SAS 9.4 and SAS Viya code.  In this example, the SAS Visual Data Mining and Machine Learning procedures TREESPLIT and GRADBOOST are used to train decision tree and gradient boosting models, respectively.  The AUTOTUNE statement is used in each procedure to perform automatic hyperparameter tuning so you don’t have to guess at the best values to set for procedure options like the maximum depth for a decision tree and the number of trees to grow in the gradient boosting model.  If your code creates score code that produces the appropriate posterior probabilities (for a class target) or predicted variables (for an interval target), you can then use the em_viya_assess macro to get the assessment results just like you do for SAS Enterprise Miner modeling nodes. The score code can either be DATA step code or an analytic store.  The example code below includes a CODE statement in PROC TREESPLIT to save the DATA step score code from the decision tree model and a SAVESTATE statement in PROC GRADBOOST to save the analytic store from the gradient boosting model.  The macro variables em_file_scorecode and em_data_rstore are used for naming these files as needed for the em_viya_assess macro. Additionally, the em_viya_modelselection macro is included in the template if you want to perform model comparison when multiple models are run as in this example to choose a champion model to export from the node.




* User Code
/* Run an autotuned decision tree */
      proc treesplit data=&em_casdata;
         target %em_target /level=&em_dec_level;
         input %em_interval_input / level=interval;
         input %em_nominal_input  / level=nominal;
         prune reducederror;
         code file=”&em_file_scorecode”;
         ods output variableImportance=&em_user_varimportance;
/* Run an autotuned gradient boosting model */
      proc gradboost data=&em_casdata;
         target %em_target /level=&em_dec_level;
         input %em_interval_input / level=interval;
         input %em_nominal_input  / level=nominal;
         savestate rstore=&em_data_rstore;
* If the node is a modeling node, that runs multiple modeling procedures,
* then you can call the em_viya_modelselection macro.  This macro selects
* the best model from the models that have been assessed using the
* %em_viya_assess macro.  It uses the fit statistic specified by either
* the Class Selection Statistic or Interval Selection Statistic property
* depending on the measurement level of the target variable.



You are almost done – now you just need to specify the em_report macro for any tables or plots you want to include in the results, using the key that you created with the em_register macro:



  description=Decision Tree Variable Importance,  
  Block=SAS Viya Reports, 


And then the em_viya_report macro is included in the template to process all the assessment results and other reports.


Finally, your CAS session is terminated by the em_viya_terminate macro included in the template:



* The em_viya_terminate macro terminates the CAS session.



We have several example flows using SAS Viya Code nodes that you can download from the EM-14.3 folder of the GitHub repository em-bridge2viya.  You can import the XML files into your SAS Enterprise Miner project, and all you need to do is fill in the four “CAS” macro variables at the top of the template mentioned above, then run.  Hope this helps you get started with incorporating your SAS Viya code for implementing the highly-scalable data mining and machine learning algorithms it provides into your SAS Enterprise Miner flows.


Great tip, Wendy!

Version history
Last update:
‎10-13-2017 09:08 AM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags