Introduction
A custom step in SAS Studio enables you to create a user interface for users at your site so those user can complete a specific task without having to know or write SAS code.
Meanwhile the SAS Studio Custom Steps repository on GitHub has well over 100 custom steps, contributed by more than 30 step authors, taking advantage of this powerful capability to build point-and-click extensions for SAS Studio. Many thanks to all contributors!
As primary maintainer of this GitHub repository, I have come across several routines in the code generation part of custom steps that are performed in multiple entries. Code snippets for those routines have been standardized and are now available in the GitHub repository as well, hopefully making it even easier to build your custom steps. This article will walk you through some of them.
Programmatically stop SAS Studio flow execution
Sometimes there is a need to stop the execution of SAS code at runtime, as you might not be able to validate all pre-requisites in the custom step UI itself. Therefore, the generated code needs to perform validation at runtime and then programmatically stop flow execution when not all pre-requisites have been met.
One example would be where the custom step requires both the input and output table to reside in a libref that points to a caslib. The custom step UI doesn't provide an option to the step author to check this as usage time, so this can only be done at runtime.
How to exactly perform that check for an input or output table will be discussed in the next section, we will first focus on how to inform the user that their input is not meeting the pre-requisites.
To programmatically stop code execution you can use the abort statement in a data step, and precede it with a put or putlog statement that explains the reason why. More details about the abort statement can be found here: SAS Documentation for the abort statement.
Here is a simple code sample and a screenshot to show how this presents itself in SAS Studio:
data _null_;
putlog "ERROR: aborting this step because ...";
abort cancel;
run;
Finding the CAS library name for a table
In certain scenarios your custom step code generator might want to directly invoke a CAS action using proc cas when the selected input table is a CAS table.
As a custom step always uses SAS Compute, the inputtable (and outputtable) UI control in a custom step return the selected table as libref.tablename.
The code generator then first needs to check whether the libref points to a caslib, and then check which caslib that is. This because the CAS action only understands about caslib names and not about SAS Compute libraries. And you cannot guarantee that the libref used by the libname points to a caslib with the exact same name.
Here is an example showing a setup where libref and caslib name are different, in this case libname casdata points to caslib mydat42:
Now assume the user has selected table casdata.class_castable in your custom step for an input table control named inputtable1.
This would result in the following SAS macro variables being generated:
/* Macro variable(s) for UI control with ID of inputtable1 */
%let inputtable1=CASDATA.CLASS_CASTABLE;
%let inputtable1_engine=CAS;
%let inputtable1_label=;
%let inputtable1_lib=CASDATA;
%let inputtable1_name=CLASS_CASTABLE;
%let inputtable1_name_base=CLASS_CASTABLE;
%let inputtable1_tblType=table;
%let inputtable1_type=dataTable;
You would use the inputtable1_engine macro variable and check its value. If its value is "CAS" then use data step function getlcaslib (SAS doc link here) to retrieve the caslib name associated with the libref provided in macro variable inputtable1_lib.
Here is a code sample:
%if "%upcase(&inputtable1_engine)"="CAS" %then %do;
%let _usr_inputtable1_caslib=%sysfunc(getlcaslib(&inputtable1_lib));
proc cas;
action table.tableInfo /
caslib="&_usr_inputtable1_caslib" name="&inputtable1_name";
run;
%end;
%else %do;
data _null_;
putlog "ERROR: The selected input library does not point to a CASlib";
abort;
run;
%end;
This SAS Studio documentation page explains which SAS macro variables are available for each control type.
HINT: Look at the generated code of your custom step in a SAS Studio flow and make sure you have selected “Unfold all regions” from the popup menu in the panel that shows the generated code. Then find the section for your custom step. This will show which SAS macro variables are being generated for each UI control and what their values are.
Wrap up
Hopefully some of these code snippets will be useful when creating your custom steps.
This page in the SAS Studio Custom Steps GitHub repository lists which code snippets are currently available.
... View more