Published on :

Sans titre

This code is also available in: Deutsch Español Français
Awaiting validation
Data Analysis

Type : MIXTE


The data used for the demonstration ('COLUMNSAMPLE' table) is created directly in the script via DATALINES statements, making it an internal source. However, the script is configured to connect to an external data source (ODBC) via the `addCaslib` action, but no data is actually read from this external source for the `columnInfo` examples.

1 Code Block
Macro Variables / CAS Setup
Explanation :
This block initializes macro variables (`srctype`, `cas_session`, `caslib_alias`, `username`, `password`, `database`, `schema`, `CONNOPTS`) used to configure the connection to the data source and the CAS session. The `cas &cas_session;` line establishes a new CAS session named 'mysess', essential for all subsequent CAS operations.
Copied!
1/* Specify the DBMS engine, for example: ODBC */
2%let srctype=odbc;
3 
4%let cas_session = mysess;
5%let caslib_alias = datalib;
6 
7 
8/*Specify connection parameters within a string
9 
10Edit the below variables with the required DBMS-specific conection options
11Also, specify such variables within the CONNOPS string, as this will be used for the other sample programs
12 
13Most datasources will require username, password, database, and schema/server
14Please refer to the connopts.xlsx file for specific connection requirements for each DBMS engine
15 
16BigQuery: requires protject and cred_path parameters
17Hadoop: requires hadoopJarPath, hadoopConfigDir, and dtm parameters
18Impala: requires port and conopts parameters
19JDBC: requires url, class, and classpath parameters
20Salesforce: requires authendpoint and catalog parameters
21SAP Hana: requires instance and tabletype parameters
22Teradata: requires dataTransferMode parameter
23Oracle, Netezza, and MSSWL require catalog parameter
24 
25*/
26 
27%let username = ???????;
28%let password = ???????;
29%let database = ???????;
30%let schema = ???????;
31 
32%let CONNOPTS=%str(user=???????
33 pwd=???????
34 dsn=???????);
35 
36/* Connect to CAS using cashost and casport, optional in SAS Studio */
37/* options cashost="????????" casport=????; */
38 
39 
40/* Create a CAS Session */
41cas &cas_session;
2 Code Block
PROC CAS (addCaslib action)
Explanation :
This block uses the PROC CAS procedure and its `addCaslib` action to define a new CASLIB. This CASLIB, named 'datalib', is configured to connect to an ODBC data source using the parameters specified by the macro variables (username, password, database, etc.). This allows CAS to access tables from the external database via this CASLIB.
Copied!
1PROC CAS;
2SESSION &cas_session;
3ACTION addCaslib caslib="&caslib_alias"
4 datasource={ srctype="&srctype",
5 user="&username",
6 pass="&password",
7 database="&database",
8 schema="&schema",
9 catalog="*"};
10RUN;
11QUIT;
3 Code Block
DATA STEP Data
Explanation :
This block defines a LIBNAME 'caslib' to refer to the previously created CASLIB (`datalib`) and associates it with the active CAS session. Then, a DATA STEP is used to create a new in-memory table called 'COLUMNSAMPLE' within this CASLIB. The data for this table is provided directly via `datalines` statements, simulating a table to be analyzed.
Copied!
1/* Create in-memory tables in CAS using CAS Libname engine */
2LIBNAME caslib cas sessref=&cas_session tag="";
3 
4DATA caslib.COLUMNSAMPLE;
5 INPUT FLIGHT $3. +5 DATES $7. +3 DEPART $5. +2 ORIG $3.
6 +3 DEST $3. +7 MILES +6 BOARDED +6 CAPACITY;
7 DATALINES;
8114 01MAR98 7:10 LGA LAX 2475 172 210
9202 01MAR98 10:43 LGA ORD 740 151 210
10219 01MAR98 9:31 LGA LON 3442 198 250
11622 01MAR98 12:19 LGA FRA 3857 207 250
12132 01MAR98 15:35 LGA YYZ 366 115 178
13271 01MAR98 13:17 LGA PAR 3635 138 250
14302 01MAR98 20:22 LGA WAS 229 105 180
15114 02MAR98 7:10 LGA LAX 2475 119 210
16202 02MAR98 10:43 LGA ORD 740 120 210
17219 02MAR98 9:31 LGA LON 3442 147 250
18622 02MAR98 12:19 LGA FRA 3857 176 250
19132 02MAR98 15:35 LGA YYZ 366 106 178
20;
21RUN;
4 Code Block
PROC CAS (table.columnInfo action)
Explanation :
This example uses the CAS `table.columnInfo` action to retrieve detailed information about all columns of the 'COLUMNSAMPLE' table residing in the 'datalib' CASLIB. This action provides metadata such as column names, type, length, etc.
Copied!
1/* Proc cas columninfo action */
2PROC CAS;
3 TABLE.columnInfo /
4 TABLE={name = "COLUMNSAMPLE",
5 caslib = "&caslib_alias"};
6RUN;
7QUIT;
5 Code Block
PROC CAS (table.columnInfo action)
Explanation :
Similar to the previous block, but this example demonstrates how to limit the returned information to a subset of specific columns ('FLIGHT', 'DATES', 'BOARDED', 'CAPACITY') using the `vars` parameter of the `table.columnInfo` action. This is useful for targeted analyses.
Copied!
1/*Proc cas columninfo action with specified vars variable */
2PROC CAS;
3 TABLE.columnInfo /
4 TABLE={name="COLUMNSAMPLE",
5 caslib = "&caslib_alias"
6 vars={"FLIGHT", "DATES", "BOARDED", "CAPACITY"}};
7RUN;
8QUIT;
6 Code Block
PROC CAS (table.columnInfo action)
Explanation :
This example presents an alternative method for specifying columns of interest. Instead of using the `vars` parameter within the `table` option, it uses the `inputs` parameter directly under the `table.columnInfo` action, passing it a list of dictionaries where each dictionary specifies the name of a variable ('FLIGHT', 'BOARDED').
Copied!
1/*Proc cas columninfo action alternative method of specifying variables to display*/
2PROC CAS;
3 TABLE.columnInfo /
4 inputs = {{name="FLIGHT"},{name="BOARDED"}}
5 TABLE={name = "COLUMNSAMPLE",
6 caslib = "&caslib_alias"};
7RUN;
8QUIT;
7 Code Block
CAS Session Termination
Explanation :
This command terminates the previously established CAS session, thereby releasing associated resources. It is good practice to always terminate CAS sessions after use.
Copied!
1/* Remove CAS
2session */
3cas &cas_session. terminate;
4 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : SAS SAMPLE LIBRARY