In Intelligent Decisioning variables can have different data types. One data type is a DataGrid. A DataGrid is a table and allows us to build one to many relationships on decision records. For example, you could have a person with several addresses.
Decision Record:
Name | Date of Birth | Address | ||
---|---|---|---|---|
Dave | 1998-05-02 | Street | Town | Postal Code |
10 High St | Brighton | BG1 3ER | ||
11 Downing Rd | London | LO1 5YY |
In the above example you would put the address information in a DataGrid and perform all necessary address operations on the DataGrid. For this Intelligent Decisioning supplies 70+ DS2 functions to work with DataGrids. There are functions for different categories like create, update, or delete, to get meta information, to retrieve values from a DataGrid, to perform statistical operations, and more.
However, sometimes we have decision flows where we must add some logic in a code node. If the code node is a Python code node and we want to work with a DataGrid in the Python code, we can’t use the DS2 DataGrid function, but we can still work with the DataGrid in Python.
Let’s have look how to work with DataGrids in a Python code node in Intelligent Decisioning.
There are basically three steps involved:
Technically DataGrids are json objects and stored in a character variable on the decision record.
A DataGrid json string has the following basic format:
[{"metadata”: [column-definitions]},{"data":[column-data]}]
The column definitions are name-value pairs separated by commas:
{"column1-name":"data-type"},{"column2-name":"data-type"},...
The data for each row of the DataGrid is specified in square brackets with commas between each value:
[column1-data, column2-data...]
As we cannot pass a DataGrid object directly into a Python code node in Intelligent Decisioning, we need to serialize the DataGrid first and save its JSON structure in a character variable.
In this example I use a Rule Set. The Rule Set has one input parameter and one output parameter:
We add an assignment statement to the Rule Set to retrieve the json structure for the DataGrid by calling the DataGrid function dataGrid_toString() and pass the output of the function to the Rule Set output parameter.
dgJSON= dataGrid_toString(dg)
Next, we use the Rule Set in the decision flow before we call the Python node to prepare the DataGrid in order to input it into the Python code node.
We pass the serialized DataGrid string (addressJSON) into the Pyhton code node as input parameter.
In the Python code we use json.loads() from the Pyhton json library to load the DataGrid into a Python list object.
Once we have loaded the DataGrid into a list object we have full access to the DataGrid and can perform all necessary operations.
In our case we validate each address row by calling an external address validation service. We add a new column to the DataGrid and store the validation result in the DataGrid.
To send the changed DataGrid back to the decision flow we need to serialize its Python list object by calling json.dumps() and return the serialized object via Python return.
In the decision flow we receive the updated DataGrid as serialized JSON string. We pass this JSON string into a Rule Set to convert it back into a DataGrid.
The Rule Set has one input parameter and one output parameter:
We add an assignment statement to the Rule Set to convert the json structure into a DataGrid by calling the DataGrid function dataGrid_create() and output the DataGrid from the Rule Set.
dataGrid_Create(dg, dgJSON)
Now we can continue working with the updated DataGrid in the decision flow.
We discussed the steps involved working with DataGrids in a Python code node. This way we can conveniently use DataGrids in Python if needed which enhances the capabilities of Intelligent Decisioning.
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.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.