BookmarkSubscribeRSS Feed

Custom Task Tuesday: Combine Viya Clustering and V9 Graphing

Started ‎09-11-2018 by
Modified ‎11-20-2018 by
Views 1,333

This week's task will be an altered version of the built-in Viya medium.jpgClustering task. We will add options for V9 histograms and correlations of interval variable and bar charts of nominal variables. Adding these options creates an end-to-end analytic task that will allow users to investigate their input variables before examining the clustering output.


This task will combine the power of Viya Clustering with the V9 PROCs that you know and love.


Here is a gif of this Custom Task in action:clustering.gif




Editing the Built-In Clustering Task

For this task, we are not creating a new Custom Task from scratch. Instead, we are going to be modifying the built-in Viya Clustering task. This task is found in the "SAS Viya Unsupervised Learning" sub-folder. 1.PNG

Right-click on the Clustering task and select "Add to My Tasks." 

add to my tasks.PNG

You will have the option to change the name of the task to something like "Viya Clustering with V9 Graphing." After the task is added to My Tasks, you will be able to edit it just like any other Custom Task.  


Adding the V9 Code for Graphing

For our V9 add in, we will need to add code to the Metadata, UI, Dependencies, and CodeTemplate sections.



Adding this code to the Metadata section will add the group (called "ADDITIONAL ANALYSES") and 3 check-boxes as options for our task. 

 <Option inputType="string" name="GROUPCHECK">ADDITIONAL ANALYSES</Option>
<Option inputType="string" name="labelCHECK">Select one or more of the following options to run before your clustering.</Option>
<Option defaultValue="0" inputType="checkbox" name="chkBAR">Bar Charts of nominal variables</Option>
<Option defaultValue="0" inputType="checkbox" name="chkHIST">Histograms of interval variables</Option>
<Option defaultValue="0" inputType="checkbox" name="chkCORR">Correlation matrix interval variables</Option>



Adding this code to the UI section will indicate the placement of the ADDITIONAL ANALYSES group at the end of the task with the 3 check-boxes in that order.

 <Group open="true" option="GROUPCHECK">
<OptionItem option="labelCHECK"/>
<OptionItem option="chkBAR"/>
<OptionItem option="chkHIST"/>
<OptionItem option="chkCORR"/>



Adding this code to the Dependencies section will control when our check-boxes appear. This is using the option from the built-in task, $measurementLevel. For example, the first dependency below says that "when the measurement level is either interval variables or both interval and nominal, show the check-box options for histograms."

 <Dependency condition="($measurementLevel == 'intervalData' || $measurementLevel == 'bothData')">
<Target action="show" conditionResult="true" option="chkHIST"/>
<Target action="hide" conditionResult="false" option="chkHIST"/>
<Dependency condition="(($measurementLevel == 'intervalData' || $measurementLevel == 'bothData') &amp;&amp; ($intervalVariables.size() &gt; 1))">
<Target action="show" conditionResult="true" option="chkCORR"/>
<Target action="hide" conditionResult="false" option="chkCORR"/>
<Dependency condition="($measurementLevel == 'nominalData' || $measurementLevel == 'bothData')">
<Target action="show" conditionResult="true" option="chkBAR"/>
<Target action="hide" conditionResult="false" option="chkBAR"/>



Adding this code to the CodeTemplate section will implement all of our changes. As you can see: our PROC UNIVARIATE will run if the histogram checkbox is checked, our PROC SGPLOT will run if the bar chart checkbox is checked, and our PROC CORR will run if the CORR checkbox is checked and there is more than one interval variable.

#if ($chkHIST == 1)
#if ($intervalVariables.size() > 0 )
ods graphics / imagemap=on;
proc univariate data=$dataset;
ods select Histogram;
var #foreach( $item in $intervalVariables ) $item#end;
histogram #foreach( $item in $intervalVariables ) $item#end;

#if ($chkBAR == 1)
#if ($nominalVariables.size() > 0 )
proc sgplot data=$dataset;
vbar #foreach( $item in $nominalVariables ) $item#end /;
yaxis grid;

#if ($chkCORR == 1)
#if ($intervalVariables.size() > 1 )
proc corr data=$dataset;
var #foreach( $item in $intervalVariables ) $item#end;


Do you have any ideas to improve this task? Do you have ideas about adding V9 code to any other Viya Tasks? Let me know in the comments below!


Join the Conversation on Twitter

Use the hashtag #CustomTaskTuesday and tweet Twitter_bird_logo_2012.svg.png@OliviaJWright with your Custom Task comments and questions!


Want to try it yourself?

Visit our SAS Studio GitHub to download the code for this task and follow along. 

 Take Me to GitHub!


Version history
Last update:
‎11-20-2018 01:50 PM
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