Published on :
ETL CREATION_INTERNE

Managing VARCHAR data in CAS

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script initializes a CAS session and configures the CASLIBs. It then creates a temporary SAS© table (`saswork_table_with_char300`) with character variables of 300, 15, and 16 bytes respectively. After examining its structure via PROC CONTENTS, it manipulates a `sas©7bdat` CASLIB to point to the SAS© `WORK` directory. Finally, it loads the created table into CAS using `PROC CASUTIL` with the `varcharconversion=16` option to manage character variables, then displays the column information of the loaded table in CAS. CASLIB manipulations (`_all_ assign`, `dropCaslib`, `addcaslib`) are administration-level actions.
Data Analysis

Type : CREATION_INTERNE


The `saswork_table_with_char300` table is created directly in the script via a DATA step with implicit `datalines`.

1 Code Block
CAS Session Management
Explanation :
This block initializes a CAS session, assigns all available CASLIBs to the session, and defines a macro variable `workpath` containing the path to the SAS WORK directory. This is essential for managing data between SAS and CAS. The assignment of `caslib _all_` is an administrative action.
Copied!
1/*
2Copyright © 2021, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
3SPDX-License-Identifier: Apache-2.0
4*/
5/* cas casauto terminate; */
6cas;
7caslib _all_ assign;
8 
9/* WORKPATH contains the path to SASWORK */
10%let workpath = %sysfunc(quote(%sysfunc(pathname(work)))) ;
11%put &workpath;
2 Code Block
DATA STEP Data
Explanation :
This DATA step creates the temporary `saswork_table_with_char300` table in the WORK library. It defines three character variables (`a`, `b`, `c`) with initial lengths of 300, 15, and 16 bytes respectively, then inserts three observations. This table will be used to demonstrate VARCHAR conversion in CAS.
Copied!
1DATA saswork_table_with_char300;
2 LENGTH a $ 300 b $ 15 c $ 16;
3 a='a300'; b='b15' ; c='c16' ;
4 OUTPUT;
5 a='a300300'; b='b151515'; c='c161616';
6 OUTPUT;
7 c='c161616161616161';
8 b='b15151515151515';
9 a="a300qzwsxedcrfvtgbyhnujmiklopqazwsxedcrfvtgbyhnujmikolp1234567890123456789012345678901234567890";
10 OUTPUT;
11RUN;
3 Code Block
PROC CONTENTS
Explanation :
This procedure displays the metadata of the `saswork_table_with_char300` table, including variable names, types, and lengths. This allows verifying the structure of the SAS table before its transfer to CAS and confirming the lengths of the character variables.
Copied!
1 
2PROC CONTENTS
3DATA=saswork_table_with_char300;
4title "Contents of WORK.SASWORK_TABLE_WITH_CHAR300";
5RUN;
6 
4 Code Block
PROC CAS (CASLIB Management)
Explanation :
This block uses `PROC CAS` to manage a CASLIB named `sas7bdat`. It first attempts to delete this CASLIB if it already exists (`dropCaslib`), then recreates it (`addcaslib`) by pointing it to the SAS WORK directory path (`&workpath`). This step is necessary for CAS to access SAS files stored in the WORK directory. Direct manipulation of CASLIBs is an administrative action.
Copied!
1PROC CAS;
2 file log;
3 TABLE.dropCaslib /
4 caslib='sas7bdat' quiet = true;
5 RUN;
6 addcaslib /
7 datasource={srctype="path"}
8 name="sas7bdat"
9 path=&workpath ;
10 RUN;
5 Code Block
PROC CASUTIL Data
Explanation :
This procedure loads the `saswork_table_with_char300` table (which is saved on disk in the WORK directory as `saswork_table_with_char300.sas7bdat`) into the CAS server under the name `cas_table_with_varchar` in the `casuser` CASLIB. The `varcharconversion=16` option is crucial here: it forces SAS character variables with a length greater than 16 bytes to be converted to VARCHAR type in CAS, allowing for more efficient storage of variable-length character strings and optimizing memory usage in CAS.
Copied!
1PROC CASUTIL;
2 load casdata="saswork_table_with_char300.sas7bdat"
3 casout="cas_table_with_varchar"
4 outcaslib="casuser"
5 importoptions=(filetype="basesas", dtm="auto", debug="dmsglvli", varcharconversion=16) ;
6RUN;
7QUIT;
8 
9title;
6 Code Block
PROC CAS (Table Inspection)
Explanation :
This last block uses `PROC CAS` to set session options on the `casuser` CASLIB, then calls the CAS action `table.columninfo` to display detailed column metadata for the `cas_table_with_varchar` CAS table. This allows verifying how character variables were handled after VARCHAR conversion, particularly confirming that variables 'a' and 'c' were converted to VARCHAR.
Copied!
1PROC CAS;
2 sessionProp.setSessOpt /
3 caslib="casuser";
4RUN;
5 TABLE.columninfo / TABLE="cas_table_with_varchar";
6QUIT;
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 : Copyright © 2021, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. SPDX-License-Identifier: Apache-2.0