Publication date: 07/08/2024

Convert a CAS Table to a Data Table

CAS Table to Data Table() converts JSON data to a data table. A JSON element in the results of an action contains “_ctb”. This is a marker indicating that it is formatted to represent a CAS table. You can send the JSON results to CAS Table To Data Table(). JMP will create a JMP data table from the JSON.

If there’s more than one result, you might need to perform a little extra work as shown with Get Results at the end of the following example. This example shows how to export data, run an action, and save the data in several data tables.

cas = Current CAS Connection();
If( IsEmpty(cas),
	cas = CAS Connect( URL( url ), Username( username ), Prompt( "IfNeeded" ));
);
dt = Open( "$SAMPLE_DATA/Animals.jmp" );
cas << Export Data( dt, "CASUSER.Animals" );
json = "\[
{
  "table": {
	"name": "Animals",
  	"caslib": "CASUSER"
  },
  "class": [
    "species",
    "subject",
    "season"
  ],
  "model": {
    "depVar": "miles",
    "effects": [
      {
        "vars": [
          "species",
          "season"
        ],
        "interaction": "BAR"
      }
    ],
    "printsol": true,
    "cl": false,
    "dfmethod": "RESIDUAL"
  },
  "random": [
    {
      "depVars": "miles",
      "effects": [
        {
          "vars": [
            "subject"
          ],
          "nest": [
            "species"
          ]
        }
      ]
    }
  ],
  "method": "REML"
}
]\";
action = New CAS Action(
	Action( "mixed.mixed" ),
	JSON( json )
);
rc = cas << Submit( action );
if(rc,
	results = action << Get Results;
	keys = results << Get Keys();
	For(i = 1, i <= N Items( keys ), i++,
		dt = CAS Table To Data Table( results[keys[i]] );
	);
);
cas << Disconnect();

Get Results enables you to examine the results of a CAS action; it’s a JSL associative array. Get JSON gives you the raw JSON; it’s a JSL string.

The JSON data is in the format discussed at https://developer.sas.com/apis/cas/rest/current/apidoc.html. Search for “ResultsTable”.

In CAS Table to Data Table(), specify the JSON string, open the data table as invisible or private, and control the table name and column names.

json = "jsonstring";
CAS Table to Data Table( json, <Invisible(Boolean)|Private(Boolean)>, <"Use Labels For Var Names">);

Invisible hides the data table from view but shows it in the JMP Home Window. private hides the data table completely. Specify Private if the user doesn’t need to interact with the data table.

"Use Labels for Var Names" indicates that the table name and the column names will be set to the SAS dataset label and the column labels. The appropriate column property, SAS Name SAS Label, is used.

Note: CAS ResultsTable attribute values (peers to “_ctb”) found in the “_ctb” JSON are also set as table variables. The “CREATETIME” attribute creates a character table variable with a locale-formatted timestamp (for example, 04/03/2019 4:38:05 PM).

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).