Network 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 2, Shared Concepts (SAS Viya: Machine Learning Procedures). 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 upper G equals left-parenthesis upper N comma upper A right-parenthesis define a graph that contains a set N of nodes and a set A of links. Consider the directed graph shown in Figure 19.

Figure 19: A Directed Graph

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 "network";
   action network.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 28.16.1.

Output 28.16.1: Problem Summary

Problem Summary
Number of Nodes9
Number of Links15
Graph DirectionDirected


The output data table NodeSetOut, shown in Output 28.16.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 28.16.2: Nodes Data Table of a Directed Graph

Selected Rows from Table
NODESETOUT
_Index_node
1A
2B
3C
4D
5E
6F
7G
8H
9I


The output data table LinkSetOut, shown in Output 28.16.3, contains the links that were read from the input links data table. The variables from and to show the associated node labels.

Output 28.16.3: Links Data Table of a Directed Graph

Selected Rows from Table LINKSETOUT
_Index_fromtoweight
1AB1
2AC2
3AD4
4BC1
5BE2
6BF5
7CE1
8DE1
9ED1
10EF2
11FG6
12GH1
13GI1
14HG2
15HI3


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:network_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.network.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.

Last updated: March 12, 2026