Use proc cas; table.columnInfo; quit; frequently during your development phase. It is the most reliable way to verify how CAS has interpreted your LENGTH statements, especially when joining tables from different data sources (like SQL Server or Oracle) where type mapping can var
CHAR(n): For fixed-length character strings, where `n` is the maximum number of characters. Shorter values are padded with spaces. This type does not support ANSI SQL NULL values.
VARCHAR(n): For variable-length character strings. Values are not padded with spaces. Ideal for data where length varies. `VARCHAR(*)` can be used when the maximum length is not known, but this can lead to conversion to `CHAR(32767)` when copying to libraries that do not support `VARCHAR`.
DOUBLE: Represents a double-precision (64-bit) floating-point number allowing a large magnitude and high precision, and supports missing values.
Examples use generated data (datalines) or SASHELP, ensuring their autonomy and direct execution.
1 Code Block
DATA STEP Data
Explanation : This simple example creates a CAS table named `fixed_char` with a `name` column of type `CHAR(10)`. Assigned values are less than or equal to 10 characters in length and will be right-padded with spaces if necessary. `proc print` is used to display the table.
Copied!
/* Création d'une table CAS avec une colonne CHAR de longueur fixe */
cas libref=casuser;
data casuser.fixed_char;
length name char(10);
name = 'Alice';
output;
name = 'Bob';
output;
run;
/* Affichage du contenu de la table pour vérifier */
proc print data=casuser.fixed_char;
run;
/* Nettoyage */
proc cas;
table.dropTable result=r / name='fixed_char' caslib='casuser';
print r;
run;
quit;
1
/* Création d'une table CAS avec une colonne CHAR de longueur fixe */
2
cas libref=casuser;
3
4
DATA casuser.fixed_char;
5
LENGTH name char(10);
6
name = 'Alice';
7
OUTPUT;
8
name = 'Bob';
9
OUTPUT;
10
RUN;
11
12
/* Affichage du contenu de la table pour vérifier */
Explanation : This example uses the `VARCHAR(50)` type for the `description` column. It is observed that values are stored with their actual length without padding, even if they are shorter than the defined maximum length. `proc columnInfo` is used to inspect column attributes, including their lengths.
Copied!
/* Création d'une table CAS avec une colonne VARCHAR de longueur explicite */
cas libref=casuser;
data casuser.varying_char_example;
length description varchar(50);
description = 'Ceci est une description courte.';
output;
description = 'Ceci est une description beaucoup plus longue qui utilise plus de caractères.';
output;
description = 'Court';
output;
run;
/* Affichage du contenu et des informations sur les colonnes pour vérifier les longueurs réelles */
proc print data=casuser.varying_char_example;
run;
proc cas;
table.columnInfo result=c / table='varying_char_example' caslib='casuser';
print c;
run;
quit;
/* Nettoyage */
proc cas;
table.dropTable result=r / name='varying_char_example' caslib='casuser';
print r;
run;
quit;
1
/* Création d'une table CAS avec une colonne VARCHAR de longueur explicite */
2
cas libref=casuser;
3
4
DATA casuser.varying_char_example;
5
LENGTH description varchar(50);
6
description = 'Ceci est une description courte.';
7
OUTPUT;
8
description = 'Ceci est une description beaucoup plus longue qui utilise plus de caractères.';
9
OUTPUT;
10
description = 'Court';
11
OUTPUT;
12
RUN;
13
14
/* Affichage du contenu et des informations sur les colonnes pour vérifier les longueurs réelles */
Explanation : This example illustrates the use of the `DOUBLE` type to store numbers with high precision, including very small and very large numbers, as well as missing values. `proc columnInfo` confirms the column type, and `table.fetch` is used to retrieve and display the data, demonstrating the handling of missing values and floating-point precision.
Copied!
/* Création d'une table CAS avec une colonne DOUBLE incluant des valeurs manquantes et des nombres à haute précision */
cas libref=casuser;
data casuser.double_advanced;
input num_val;
datalines;
100.123456789
.
1.23456789123456789E-10
-5.6789123456789E+12
;
run;
/* Affichage des informations sur les colonnes pour confirmer le type DOUBLE */
proc cas;
table.columnInfo result=c / table='double_advanced' caslib='casuser';
print c;
run;
/* Récupération et affichage des données pour montrer la précision et les manquants */
proc cas;
table.fetch result=f / table='double_advanced' caslib='casuser';
print f;
run;
quit;
/* Nettoyage */
proc cas;
table.dropTable result=r / name='double_advanced' caslib='casuser';
print r;
run;
quit;
1
/* Création d'une table CAS avec une colonne DOUBLE incluant des valeurs manquantes et des nombres à haute précision */
2
cas libref=casuser;
3
4
DATA casuser.double_advanced;
5
INPUT num_val;
6
DATALINES;
7
100.123456789
8
.
9
1.23456789123456789E-10
10
-5.6789123456789E+12
11
;
12
RUN;
13
14
/* Affichage des informations sur les colonnes pour confirmer le type DOUBLE */
Explanation : This advanced example shows how `VARCHAR(*)` and `CHAR(n)` types behave when copying a CAS table to a standard SAS library (non-CAS). It is observed that `VARCHAR(*)` can be converted to a `CHAR(32767)` column in a non-CAS context, while `CHAR(n)` will retain its fixed length, truncating data if it is too long. `PROC CONTENTS` is used to check column lengths after copying.
Copied!
/* Initialise une session CAS */
cas libref=casuser;
/* Crée une table CAS avec une colonne VARCHAR(*) */
data casuser.varchar_star_orig;
length comments varchar(*);
comments = 'Un commentaire.'; output;
comments = 'Un commentaire très long, dont la taille maximale n''est pas prédéfinie, pour observer l''impact lors de la copie.'; output;
run;
/* Crée une table CAS avec une colonne CHAR(10) */
data casuser.char_fixed_orig;
length short_text char(10);
short_text = 'Hello'; output;
short_text = 'WorldLonger'; /* Sera tronqué */
output;
run;
/* Copie les tables CAS vers une bibliothèque de travail SAS standard */
/* Remarque: La bibliothèque WORK est un chemin local non-CAS. */
/* Pour une exécution hors de SAS Studio/Viya, ajuster le LIBNAME si nécessaire */
libname work clear;
data work.varchar_star_copy;
set casuser.varchar_star_orig;
run;
data work.char_fixed_copy;
set casuser.char_fixed_orig;
run;
/* Vérifie les attributs des colonnes dans la table copiée */
proc contents data=work.varchar_star_copy varnum; run;
proc contents data=work.char_fixed_copy varnum; run;
/* Affiche le contenu de la table CHAR pour voir la troncature */
proc print data=work.char_fixed_copy;
run;
/* Nettoyage des tables CAS */
proc cas;
table.dropTable result=r / name='varchar_star_orig' caslib='casuser';
print r;
table.dropTable result=r / name='char_fixed_orig' caslib='casuser';
print r;
run;
quit;
1
/* Initialise une session CAS */
2
cas libref=casuser;
3
4
/* Crée une table CAS avec une colonne VARCHAR(*) */
5
DATA casuser.varchar_star_orig;
6
LENGTH comments varchar(*);
7
comments = 'Un commentaire.'; OUTPUT;
8
comments = 'Un commentaire très long, dont la taille maximale n''est pas prédéfinie, pour observer l''impact lors de la copie.'; OUTPUT;
9
RUN;
10
11
/* Crée une table CAS avec une colonne CHAR(10) */
12
DATA casuser.char_fixed_orig;
13
LENGTH short_text char(10);
14
short_text = 'Hello'; OUTPUT;
15
short_text = 'WorldLonger'; /* Sera tronqué */
16
OUTPUT;
17
RUN;
18
19
/* Copie les tables CAS vers une bibliothèque de travail SAS standard */
20
/* Remarque: La bibliothèque WORK est un chemin local non-CAS. */
21
/* Pour une exécution hors de SAS Studio/Viya, ajuster le LIBNAME si nécessaire */
22
LIBNAME work clear;
23
24
DATA work.varchar_star_copy;
25
SET casuser.varchar_star_orig;
26
RUN;
27
28
DATA work.char_fixed_copy;
29
SET casuser.char_fixed_orig;
30
RUN;
31
32
/* Vérifie les attributs des colonnes dans la table copiée */
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.