Published on :
DATA Step CREATION_INTERNE

Create a VARCHAR variable with the LENGTH statement

This code is also available in: Deutsch Español Français
Awaiting validation
The functional analysis details the creation and management of VARCHAR variables in the SAS© Viya environment. Unlike the CHAR type, which uses a fixed length in bytes, the VARCHAR type adapts to the actual length of the character string, thus avoiding truncation, especially with multi-byte characters. The use of the CAS engine is imperative for creating VARCHAR variables, as the SAS© 9 engine does not support them. It is advisable to favor VARCHAR for variable-length strings and to consider the memory usage implications for very short strings.
Data Analysis

Type : CREATION_INTERNE


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!
1DATA mycas.ex_base_string;
2 LENGTH x varchar(30);
3 LENGTH y $30;
4 x = 'Bonjour le monde';
5 y = 'Bonjour le monde';
6RUN;
7 
8PROC CONTENTS DATA=mycas.ex_base_string;
9RUN;
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!
1options encoding='utf-8';
2 
3DATA 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;
11RUN;
12 
13PROC PRINT DATA=mycas.ex_troncature;
14 title 'Comparaison VARCHAR et CHAR avec caractères multi-octets';
15RUN;
16 
17PROC CONTENTS DATA=mycas.ex_troncature;
18RUN;
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.
Copied!
1DATA mycas.ex_manip_varchar;
2 LENGTH nom_produit varchar(50);
3 LENGTH description_courte varchar(20);
4
5 nom_produit = 'Ordinateur Portable Ultra-Fin XPS 15';
6 description_courte = substr(nom_produit, 1, 20);
7
8 nouvelle_chaine = catx(' - ', 'Nouveau', description_courte, 'Édition');
9 OUTPUT;
10RUN;
11 
12PROC PRINT DATA=mycas.ex_manip_varchar;
13 title 'Manipulation de variables VARCHAR';
14RUN;
15 
16PROC CONTENTS DATA=mycas.ex_manip_varchar;
17RUN;
4 Code Block
DATA STEP / PROC CASUTIL Data
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!
1/* Assurez-vous d'avoir une session CAS active */
2cas mycas;
3 
4/* Crée une table CAS avec une variable VARCHAR */
5DATA mycas.clients;
6 LENGTH client_id varchar(15) client_nom varchar(100);
7 client_id = 'C001';
8 client_nom = 'Dupont Martin';
9 OUTPUT;
10 
11 client_id = 'C002';
12 client_nom = 'Marie Dubois avec un nom très très long qui dépasse 100 caractères si on ne fait pas attention';
13 OUTPUT;
14RUN;
15 
16/* Affiche le contenu de la table CAS */
17PROC PRINT DATA=mycas.clients;
18 title 'Clients avec VARCHAR en CAS';
19RUN;
20 
21/* Vérifie la structure de la table CAS avec PROC CONTENTS */
22PROC CONTENTS DATA=mycas.clients;
23 title 'Contenu de la table CAS avec VARCHAR';
24RUN;
25 
26/* Utilise PROC CASUTIL pour télécharger la table CAS vers une libname SAS */
27/* Cela n'est pas nécessaire pour l'utilisation directe en CAS, mais montre l'interopérabilité */
28/* proc casutil;
29 load casdata='clients' incaslib='mycas' outcaslib='casuser' replace;
30 run; */
31 
32/* 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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved.