BookmarkSubscribeRSS Feed

Taking the Next Step in SAS Studio Customization - Part 2

Started ‎12-15-2023 by
Modified ‎12-15-2023 by
Views 292

 

Within SAS Studio, many users have already taken advantage of the ability to customize SAS Studio tasks to provide a starter for themselves and their coworkers. The time taken to build such custom tasks has allowed others to get started using analytical procedures within SAS without having to know all the details about coding. SAS Studio now has another means of customization available, Custom Steps.
 
This post continues from part 1 where we will continue the process of creating our own SAS Studio Custom Step that then can be shared with others. We will add the OPTIONS and OUTPUT tabs to our GUI and then work with the associated SAS code with our Custom Step.
 

STEP 4: Creating the Step in Designer (Continued)

 

  • Starting from SAS Drive, use the applications menu to proceed to Develop Code and Flows. From the left pane, access the Steps area and click on Shared. We should see our KCLUSExample from before listed. Double-click to open our Custom Step.
  • In the Control Library, click Add Page. With this new page selected, set ID to options and Label to OPTIONS in the properties panel.
  • Add two Check Box items to the page. Set the following properties:
    • Check Box 1 - Set ID to imputeint. Make "Replace interval missing values with mean" the label.
    • Check Box 2 - Set ID to imputecat. Make "Replace nominal missing values with mode" the label.
  • Add a radio button group to the page. Set ID to radiocluster and Label to Number of clusters: . Click Add Many to add several items to the radio list. In the box that appears, enter Specify number of clusters and Calculate number of clusters, where each option appears on its own line. Click OK.
  • If the original default radio button is still present in the list, click the box in front of that option (in properties) and click the trash button.
  • Click the Map Values button to expand the options within the radio list. A new column named Value appears. On each line, change Value to specify and calculate respectively.
  • Set the default radio button to Specify number of clusters.
  • Add a text or numeric field to the page. Set ID to numcluster and Label to Maximum number of clusters. Change Type to Numeric. Set the default value to 4, the minimum value to 2, and the maximum value to 6.

 

border_01_damodl_blog4_radio.png

 Select any image to see a larger version.

Mobile users: To view the images, select the "Full" version at the bottom of the page.

 
Sometimes we do not want something to appear on the GUI unless another option has been selected. For example, should the text/numeric object appear if we pick Calculate number of clusters? No. The Dependencies area of the properties allows us to clarify moments when the object will appear or disappear. In our case, we only want the text/numeric object to appear when we pick the Specify radio button.
 
  • Expand the Dependencies area within the text/numeric object. Under Visibility, type ["$radiocluster","=","specify"].

 

border_02_damodl_blog4_depend.png

 

Your OPTIONS pane should now look like this.

 

border_03_damodl_blog4_options.png

 

Now let's move on to the OUTPUT tab of the Custom Step.
 
  • In the Control Library, click Add Page. With this new page selected, set ID to output and Label to OUTPUT.
  • Add an output table to the page. Set ID to outputdata and Label to Select location of output table. Select the box for Required.
 
Your OUTPUT pane should now look like this.
 

border_04_damodl_blog4_output.png

 

Step 5: Adding the SAS Code

 
With the GUI created, we turn our attention to the backing code for the Custom Step. For ease, we will address each section of the code as it will be added to the program code area. Recall that we needed to be careful when we were naming the IDs of each of our objects on the GUI. You will now see these ID names as macro variables referenced within the upcoming code.
 
Click on the Program tab just above the Control Library of the Step Designer. You will see a line that says /* SAS templated code goes here */. This is a comment and can be left or replaced with our code.
 
The first code block we will add creates two macro variables that contain the list of categorical inputs and continuous inputs selected by the user. The macro do loop shows how you can step through the list of variables selected one by one. This can be beneficial if you are needing to address aspects of these variables individually. The macro &catvars_1_name would reference the first categorical variable chosen by the user in the column selector. The &i and &j macro variables will count through the list until reaching the maximum number of variables selected. This value is stored in the macro &catvars_count. There are other ways to more directly reference the entire list of variables selected but this approach works well if there are other things you need to do to the variables before analysis.

 

*Creates the lists of categorical and interval inputs;
%let catvarslist=;
%let intvarslist=;

%macro predictors;
%do i=1 %to &catvars_count;
   %let catvarslist = &catvarslist &&catvars_&i._name;
%end;
%do j=1 %to &intvars_count;
   %let intvarslist = &intvarslist &&intvars_&j._name;
%end;
%mend;
 
%predictors;
 
Next let's start the creation of the PROC KCLUS statement. The opening %let starts the PROC KCLUS statement and references the selected data set and distance options. These are consistent in the code regardless of the other options that are selected by the user on the GUI. The macro then adds options to this original PROC KCLUS statement based upon user selections. The macro variables &imputeint and &imputecat add options for imputation of the mean or mode as selected. The macro &radiocluster, when set to specify, adds the option of the maximum number of clusters input by the user, &numcluster. When &radiocluster is set to calculate, the option for the abc calculation will be added to the PROC line.
 
*Creates the PROC KCLUS statement;
%let proc=proc kclus data=&inputdata distance=Euclidean distancenom=binary ;

%macro procline;
%if (&imputeint) %then %do; %let proc = &proc impute=mean; %end;
%if (&imputecat) %then %do; %let proc = &proc imputenom=mode; %end;
%if (&radiocluster = specify) %then %do; %let proc = &proc maxclusters=&numcluster; %end;
%if (&radiocluster = calculate) %then %do; %let proc = &proc noc=abc(minclusters=2) maxclusters=10; %end;
%mend;

%procline;
 
Add this code block after the previously added code for the input variables.
 
Now let's use these macros and establish the PROC KCLUS code we will want to run. The macro &proc will contain the completed PROC statement to begin KCLUS. The two input lines will call the correct macro variable to list the interval list and the categorical list. The final line, SCORE, will create the output data set that is referenced by the macro &outputdata.

 

&proc;
   input &intvarslist / level=interval;
   input &catvarslist / level=nominal;
   score out=&outputdata copyvars=(_all_);
run;
 
Add this code block after the previously added code for the PROC line generation.

 

The code for our Custom Step for PROC KCLUS is complete. Be sure to save if you have not already done so. Recall that your created Custom Step should appear in the Shared list when SAS Steps is selected on the left pane of SAS Studio.
 

Step 6: Running your Custom Step

 
To run your Custom Step, you have two options. You can right click on the step and select "Open in a tab". This will open your step in a very similar view as a SAS Studio task.

 

border_05_damodl_blog4_WindowRun.png

 

The alternative method would be to click on New and then select Flow. You would then click and drag your Custom Step from the left pane into the flow. After adding and connecting data sets to and from the created step, you select the step in the flow and complete the GUI that appears at the bottom of your screen. When using a flow, the objects responsible for selecting the input and output data sets do not appear in the GUI. They are completed when you connect data sets to the squares on each side of the step within the flow. Input data sets will connect on the left side and output data sets will connect to the right.

 

border_06_damodl_blog4_FlowRun.png

 

Congratulations! You have created your first Custom Step. From here, you can now add more things to the GUI and code to expand the use of this step as needed.

 

If you would like to investigate Custom Steps further, please visit here to access more information.  Also check out the SAS Custom Step repository.

 

 

Find more articles from SAS Global Enablement and Learning here.

Version history
Last update:
‎12-15-2023 10:36 AM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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