Network Optimization Action Set
Reading a Directed Graph
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 1, Introduction (SAS Optimization: The OPTNETWORK Procedure). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
This example describes how to input a graph by using the readGraph action. Let define a graph that contains a set N of nodes and a set A of links. Consider the directed graph shown in Figure 16.
Figure 16: A Directed Graph

The directed graph G can be represented by the following links data set, LinkSetIn:
data LinkSetIn;
input from $ to $ weight @@;
datalines;
A B 1 A C 2 A D 4 B C 1 B E 2
B F 5 C E 1 D E 1 E D 1 E F 2
F G 6 G H 1 G I 1 H G 2 H I 3
;
The following DATA step loads the LinkSetIn data set into a CAS data table named mylib.LinkSetIn. These statements assume that the CAS engine libref is named mylib, but you can substitute any appropriately defined CAS engine libref.
data mylib.LinkSetIn;
set LinkSetIn;
run;
The following statements read in this graph, declare it as a directed graph, and output the resulting links and nodes data tables. These statements do not run any algorithms, so the resulting output contains only the input graph.
proc cas;
loadactionset "optNetwork";
action optNetwork.readGraph result=r status=s /
direction = "directed"
links = {name = "LinkSetIn"}
outNodes = {name = "NodeSetOut", replace=true}
outLinks = {name = "LinkSetOut", replace=true};
run;
print r.ProblemSummary; run;
action table.fetch / table = "NodeSetOut"; run;
action table.fetch / table = "LinkSetOut"; run;
quit;
The problem summary output from this action is shown in Output 2.13.1.
Output 2.13.1: Problem Summary
| Problem Summary | |
|---|---|
| Number of Nodes | 9 |
| Number of Links | 15 |
| Graph Direction | Directed |
The output data table NodeSetOut, shown in Output 2.13.2, now contains the nodes that are read from the input links data table. The variable node shows the label that is associated with each node.
Output 2.13.2: Nodes Data Table of a Directed Graph
| Selected Rows from Table NODESETOUT | |
|---|---|
| _Index_ | node |
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
| 6 | F |
| 7 | G |
| 8 | H |
| 9 | I |
The output data table LinkSetOut, shown in Output 2.13.3, contains the links that were read from the input links data table. The variables from and to show the associated node labels.
Output 2.13.3: Links Data Table of a Directed Graph
| Selected Rows from Table LINKSETOUT | |||
|---|---|---|---|
| _Index_ | from | to | weight |
| 1 | A | B | 1 |
| 2 | A | C | 2 |
| 3 | A | D | 4 |
| 4 | B | C | 1 |
| 5 | B | E | 2 |
| 6 | B | F | 5 |
| 7 | C | E | 1 |
| 8 | D | E | 1 |
| 9 | E | D | 1 |
| 10 | E | F | 2 |
| 11 | F | G | 6 |
| 12 | G | H | 1 |
| 13 | G | I | 1 |
| 14 | H | G | 2 |
| 15 | H | I | 3 |
Reading a Directed Graph
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the LinkSetIn data to the comma-separated-value (CSV) file LinkSetIn.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="LinkSetIn.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
The following statements read in this graph, declare it as a directed graph, and output the resulting links and nodes data tables. These statements do not run any algorithms, so the resulting output contains only the input graph.
s:optNetwork_readGraph{
direction = "directed",
links = {name = "LinkSetIn"},
outNodes = {name = "NodeSetOut", replace=true},
outLinks = {name = "LinkSetOut", replace=true}}
Reading a Directed Graph
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the LinkSetIn data to the comma-separated-value (CSV) file LinkSetIn.csv and then use the following code to load the CSV file into CAS:
s.upload_file('LinkSetIn.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
The following statements read in this graph, declare it as a directed graph, and output the resulting links and nodes data tables. These statements do not run any algorithms, so the resulting output contains only the input graph.
s.optNetwork.readGraph(
direction = "directed",
links = {"name":"LinkSetIn"},
outNodes = {"name":"NodeSetOut", "replace":True},
outLinks = {"name":"LinkSetOut", "replace":True})
Reading a Directed Graph
This example is not available for the R programming language.