In SAS Studio on SAS Viya there are many ready-made steps at your disposal. There are times however, when the existing steps don’t cover your needs. Starting from Viya 2021 there is a possibility to create custom steps. If you’re a seasoned DI Studio user, think of this as a new way to create user-written transforms. Steps that are user built to cover a specific need. They are a great way to bring more power and capabilities to SAS Studio Flows. There a great example of creating a new custom step here:
I was thinking of a suitable example and figured that many times there is need for describing a table structure. In SAS there is the good old PROC CONTENTS that tells many things about a table. In Python Pandas library there is df.info() function that does similar things to a dataframe, look here:
So I got the idea of creating a custom step that allows user to connect a table and run either PROC CONTENTS or Pandas df.info to get information about table structure. First I opened up the dialogue to create a new custom step:
Selecting “Custom step quick start” opens the custom step designer. There are three different tabs:
Designer allows to create a UI layout for the new step and define names and labels to UI components
Prompt UItab creates a .json in the background while you add components to UI
Program tab is where you put the actual code that your custom step will run
I added to UI components to my designer page. Input table obviously allows you to connect a source table to your custom step. Note that it will not show in the actual step, when you use it. It only provides an input port to your custom step. The second component I added is the drop down list. That is also self-explanatory, you can add as many selections as needed and they will appear in custom steps options page when you use it.
I am adding two options to my drop-down list. First selection is “PROC_CONTENTS” that will call the code that runs the SAS PROC. Note that I have used an underscore, best to use proper variable names since these are the actual ones that are used in the code. My second selection is “Pandas_info”. This will run the Python code that run the df.info() function:
I’m happy with these two UI components, they’re all that I need to get my describe custom step done. Now I can take a look at the .json code that was created behind the scenes while I added my components. I omitted the start and ending but you can see the references to the two UI components that we created in the .json code:
Next step is to create the actual program code that will match with the variables provided by the UI. In this example we only use 2 variables:
InputTable: this references the source table that is mapped to the input port
InputType: this references one of the two possible selections from the drop-down menu
The code below is simple but does all that we need for this example. It’s a macro called “describeData” that will choose either of the if-then code blocks and run the reference part of code.
First IF statement runs when &inputType equals PROC_Contents and uses &inputTable as data.
Second IF statement run when &inputType equals Pandas_info and uses the same &inputTable variable.
This uses the PROC PYTHON procedure where you can place Python code between submit; and endsubmit; statements. I tried of course to inset my code here but got the error message that the submit block cannot be placed inside a SAS macro. So I resorted to the infile statement and placed the actual submit block inside myscript.py file that is accessible from a path in the filesystem. This worked exactly as I wanted. You can read more about this in the PROC PYTHON documentation:
I noticed that the macro variable is not directly visible to PROC PYTHON so I needed to define variable inputTable1 and use SAS.symget function to transfer it over to Python. The next line uses SAS.sd2df function to copy the SAS table into a Python dataframe. Then I call the Python df.info function to provide a description of the table.
Now that we have finished creating our new custom step it’s time to save it as “Describe Data” and give it a spin! I insert the custom step on the workflow canvas and use the good old class dataset as source:
It’s very simple, just connect source table, select either PROC_Contents or Pandas_info and run!
The result form SAS PROC CONTENTS is exactly what you would expect. Here’s the output from the Pandas df.info() function:
While this simple example may be limited in its applicability, I hope it shows how easy it is to create a custom step in SAS Studio! And the best thing is, if you place your custom step in the public folder, other users in your Viya platform can use and run it and even develop it further! So simple that Santa can do it while preparing for the big day! 🎅
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.