This week's task will be an altered version of the built-in Viya Clustering 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:
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.
Right-click on the Clustering task and select "Add to My Tasks."
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.
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"/>
</Group>
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>
<Dependency condition="(($measurementLevel == 'intervalData' || $measurementLevel == 'bothData') && ($intervalVariables.size() > 1))">
<Target action="show" conditionResult="true" option="chkCORR"/>
<Target action="hide" conditionResult="false" option="chkCORR"/>
</Dependency>
<Dependency condition="($measurementLevel == 'nominalData' || $measurementLevel == 'bothData')">
<Target action="show" conditionResult="true" option="chkBAR"/>
<Target action="hide" conditionResult="false" option="chkBAR"/>
</Dependency>
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;
run;
#end
#end
#if ($chkBAR == 1)
#if ($nominalVariables.size() > 0 )
proc sgplot data=$dataset;
vbar #foreach( $item in $nominalVariables ) $item#end /;
yaxis grid;
run;
#end
#end
#if ($chkCORR == 1)
#if ($intervalVariables.size() > 1 )
proc corr data=$dataset;
var #foreach( $item in $intervalVariables ) $item#end;
run;
#end
#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!
Use the hashtag #CustomTaskTuesday and tweet @OliviaJWright with your Custom Task comments and questions!
Visit our SAS Studio GitHub to download the code for this task and follow along.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.