ArcGIS Pro

SAS®-ArcGIS Bridge: Create a Geoprocessing Tool that Runs SAS

SAS has a variety of analytical capabilities that complement the spatial analysis tools available in ArcGIS. To help integrate ArcGIS and SAS, the SAS®-ArcGIS Bridge includes Python modules that make it possible to run SAS analytics directly within ArcGIS Pro… which is helpful if you already know how to write SAS procedures.

But what if a colleague needs to run a SAS-based analysis inside ArcGIS Pro and they don't know how to use SAS?

Well, you can help them out by creating a script tool that calls SAS statements and procedures for them.  This tool runs like any other geoprocessing tool in ArcGIS Pro. This blog provides a quick overview of how to accomplish this, and once you know how to integrate SAS statements and procedures into your script tools, you’ll be ready to share geoprocessing workflows with colleagues who want to make delicious analytical meals using both ArcGIS and SAS ingredients. So, let’s take a look!

First, let’s go over a few prerequisites

You’ll need to know how to create a Python toolbox in ArcGIS Pro. Thankfully it’s straightforward, and there are guides to help you get up to speed. Here’s a few to get started:

You’ll also need to know how to use SAS… or at least how to write SAS code using saspy. If you’re not already up to speed with SAS, there are several easy guides to help. Here’s just two to get started:

Now, let’s proceed to the main show.

Creating a geoprocessing tool that calls SAS

For this example, we’ll create a tool that calls the SAS CORR procedure, which assesses the correlation between variables in your data using various statistics. We’ll write this using Python and test it on a dataset of New York City taxi pick-up and drop-off locations.

Let’s begin with a basic python script tool template, which you get by creating a new Python toolbox in the Catalog Pane:

Procedure to add python toolbox

Once you create the Python tool, right-click the tool and edit it in your preferred script editor. You’ll start with the basic Python toolbox template and take three steps to create your SAS correlation tool:

Preview of python toolbox template

Not too bad, right? Let’s do this.

1. import modules

We’ll use a few modules for this tool:

Python import statements

The statement `from saspy import autocfg` deals with SAS configuration files. If you’re already a SAS pro, you probably know about creating SAS configuration files and how this works. If not, worry not because we’ll use this utility to create a configuration file that SAS uses when creating “sessions” that allow us to interact with SAS behind the scenes.

Feel free to add any other modules you often use or might consider using in your upcoming tools.

2. Define parameters

The tool we’ll create is straightforward and has simple parameters. You’ll specify an input feature layer, input fields from the input feature layer, and an output table.

Python toolbox parameters section

The field dependency and filter control the list of fields displayed in the Input Fields parameter. For example, when the user picks an input feature layer, the Input Fields drop-down will contain all the Short, Long, Float, and Double fields found in the Input Features. You probably already knew this, and if not, don’t worry!

3. Write tool logic

This is the fun part: you’ll create the engine under the hood by writing the logic in the execute method. Start by adding some basic python statements that accept your user provided parameter values as text:

execute class in the tool

The ‘varNames’ variable uses the input fields value, which are then converted to upper case, and any semicolons are replaced with spaces. This will help in a later step where you write the SAS CORR procedure and pass these fields as parameters; it makes SAS happier and will make your job easier.

Next, your code will convert the input feature layer to a SAS table, so we’ll need to create a unique file name so that the tool works every time for every user without overwriting someone else’s data.

unique file name code

Now, you’ll convert the input feature layer to a SAS table that we’ll later pass to the CORR procedure. We’ll do this using one of the SAS-ArcGIS Bridge data conversion tools: Table to SAS.

Table To SAS tool in python

Ok! Your tool will now create a table that SAS can use. Now here comes the exciting part: writing the SAS CORR procedure.

If you’re a SAS user and have written your fair share of SAS procedures, you know the drill.  However, if you’re new to this part of things, don’t let it intimidate you; it’s just another scripting language with access to an extensive set of statistical analysis tools. Of course, like any scripting language, there are some syntax rules to learn.

Here’s the syntax for the SAS CORR procedure:

SAS CORR procedure

You’re wrapping up the procedure in a string variable and passing your input geoprocessing parameters as SAS parameters. The SAS CORR procedure is documented, and other guides are available, but let’s break it down:

explanation of SAS CORR variables

The sasSyntax variable becomes a string with the SAS code that calls the CORR procedure, using your tool’s parameter values in the right places to allow SAS to run the procedure. Here’s an example of what this variable contains when running the tool:

options linesize=80;

proc corr data=sasuser.temp_20211116_130151 OUTS=sasuser.out_temp_20211116_130151 pearson spearman kendall hoeffding;

var PICKUP_HOUR DROPOFF_HOUR PASSENGER_COUNT TRIP_DURATION VENDOR_ID;

run;

 

Ok, you’ve made it this far; great job. Your tool will now actually run the procedure, but it needs to configure and create a SAS session. Let’s take a look at how that works.

configuring a SAS session

This might feel a bit odd if you’re not used to SAS, but it boils down to the following:

SAS procedures run in a SAS session.  To establish a SAS session, the geoprocessing tool needs to know where SAS is installed.  The autocfg.main() function determines where SAS is installed and creates the configuration file.

The code above handled this for your user, so they don’t have to worry about creating a configuration file or editing the one that comes with the SAS installation. Great, let’s move on to establishing the SAS session and submitting the SAS procedure!

sas session variable

Your tool is now creating a SAS procedure, configuring a SAS session, submitting the SAS statements, and even cleaning up after itself by ending the SAS session. You’re on a roll.

You have a few steps left: you need to convert the result back to a table that ArcGIS Pro can use, and you need to provide some nicely formatted geoprocessing messages to help your user understand what on Earth just happened. Let’s finish this.

Converting the result back to a file geodatabase table is easy-peasy:

SAS To Table tool

Providing the nicely formatted geoprocessing messages is accomplished with some straightforward Python. Let’s break it down.

Handling python messaging

Your last line of code is simple and nicely earned…

You’re done with the execute class, and your tool is now complete. The full code can be seen in this .pyt file.

You can now test and use your tool before distributing it to your colleagues.

4. Using the tool

Save the file and go back to ArcGIS Pro to open the tool.

You’ll now see the tool parameters. You’ve done a great job keeping this tool usable and straightforward, even if the user does not know SAS, so all you have to do is pass an input feature layer, specify some fields, and tell the tool where you want the outputs.

GP tool interface

Click “Run” and see the magic happen: after a few moments, your tool will provide an output table containing the results of the Spearman Rank-Order Correlation, ready for your user to use.

Successful GP tool run
Output correlation table

And the SAS procedure’s log output is provided as geoprocessing messages that your user can dive into:

Output geoprocessing messages

You’ve created a tool that brings ingredients from both SAS and ArcGIS together to make some friendly analytical workflows, and you can now share and collaborate with others even if they’re not experienced SAS users.

Good luck creating your next tool!

Visit our page for more resources on Spatial Statistics capabilities.

About the author

Alberto Nieto is a Product Engineer on Esri’s Spatial Statistics team. In his role, he helps research, build, and maintain spatial data science capabilities in ArcGIS and works closely with government agencies to learn about the problems our software can help solve. Alberto’s background includes fourteen years of experience, including previous roles as a GIS Developer at Capital One and NOAA's Climate Prediction Center, and as a GIS Analyst at the Census Bureau and the Alachua County Environmental Protection Department.

Connect:
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Next Article

Using Arcade to Translate Pop-Ups for Use in the ArcGIS Instant Apps

Read this article