Examples use generated data (datalines) or SASHELP to create temporary or in-memory tables.
1 Code Block
DATA STEP Data
Explanation : This example illustrates the creation of a VARCHAR variable (X) and a CHAR variable (Y) with a maximum length of 30 characters/bytes. Variable X stores the string without wasting space for unused characters, while Y always allocates 30 bytes.
Copied!
data mycas.ex_base_string;
length x varchar(30);
length y $30;
x = 'Bonjour le monde';
y = 'Bonjour le monde';
run;
proc contents data=mycas.ex_base_string;
run;
1
DATA mycas.ex_base_string;
2
LENGTH x varchar(30);
3
LENGTH y $30;
4
x = 'Bonjour le monde';
5
y = 'Bonjour le monde';
6
RUN;
7
8
PROC CONTENTSDATA=mycas.ex_base_string;
9
RUN;
2 Code Block
DATA STEP Data
Explanation : This example demonstrates the difference in handling multi-byte characters (like 'ã' in UTF-8) between VARCHAR and CHAR. The 'ville_char' variable with a length of 10 bytes could be truncated if the multi-byte characters exceed the byte limit, while 'ville_varchar' with a length of 10 characters will adapt correctly, storing the complete string if its character length is respected, regardless of the byte size.
Copied!
options encoding='utf-8';
data mycas.ex_troncature;
length ville_varchar varchar(10);
length ville_char $10;
ville_varchar = 'São Paulo'; /* 9 caractères, mais 'ã' est multi-octets en UTF-8 */
ville_char = 'São Paulo';
output;
run;
proc print data=mycas.ex_troncature;
title 'Comparaison VARCHAR et CHAR avec caractères multi-octets';
run;
proc contents data=mycas.ex_troncature;
run;
1
options encoding='utf-8';
2
3
DATA mycas.ex_troncature;
4
LENGTH ville_varchar varchar(10);
5
LENGTH ville_char $10;
6
7
ville_varchar = 'São Paulo'; /* 9 caractères, mais 'ã' est multi-octets en UTF-8 */
8
ville_char = 'São Paulo';
9
10
OUTPUT;
11
RUN;
12
13
PROC PRINTDATA=mycas.ex_troncature;
14
title 'Comparaison VARCHAR et CHAR avec caractères multi-octets';
15
RUN;
16
17
PROC CONTENTSDATA=mycas.ex_troncature;
18
RUN;
3 Code Block
DATA STEP Data
Explanation : This example shows how to manipulate VARCHAR variables. It uses the `SUBSTR` function to extract a substring from a VARCHAR variable and the `CATX` function to concatenate multiple elements (including another VARCHAR variable) into a new VARCHAR variable. This illustrates the flexibility of the VARCHAR type in string manipulation operations.
Explanation : This advanced example illustrates the creation of a table directly in the CAS environment ('mycas' is a CAS libname) with VARCHAR variables. It shows how VARCHAR lengths are respected even with potentially very long strings, without causing visible truncation in the display, as long as the declared character limit is sufficient. The use of `proc contents` confirms the effective types and lengths in CAS. It highlights the natural integration of VARCHARs into Viya's distributed in-memory operations.
Copied!
/* Assurez-vous d'avoir une session CAS active */
cas mycas;
/* Crée une table CAS avec une variable VARCHAR */
data mycas.clients;
length client_id varchar(15) client_nom varchar(100);
client_id = 'C001';
client_nom = 'Dupont Martin';
output;
client_id = 'C002';
client_nom = 'Marie Dubois avec un nom très très long qui dépasse 100 caractères si on ne fait pas attention';
output;
run;
/* Affiche le contenu de la table CAS */
proc print data=mycas.clients;
title 'Clients avec VARCHAR en CAS';
run;
/* Vérifie la structure de la table CAS avec PROC CONTENTS */
proc contents data=mycas.clients;
title 'Contenu de la table CAS avec VARCHAR';
run;
/* Utilise PROC CASUTIL pour télécharger la table CAS vers une libname SAS */
/* Cela n'est pas nécessaire pour l'utilisation directe en CAS, mais montre l'interopérabilité */
/* proc casutil;
load casdata='clients' incaslib='mycas' outcaslib='casuser' replace;
run; */
/* Fin de la session CAS (facultatif, selon la gestion de la session) */
/* cas mycas terminate; */
1
/* Assurez-vous d'avoir une session CAS active */
2
cas mycas;
3
4
/* Crée une table CAS avec une variable VARCHAR */
/* Fin de la session CAS (facultatif, selon la gestion de la session) */
33
/* cas mycas terminate; */
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.