Published on :
Data Access CREATION_INTERNE

Library Assignment Elements

This code is also available in: Deutsch Español Français
Assigning a SAS© library via the LIBNAME statement allows referencing a physical data location. It is composed of a libref (short name), a library engine (often V9 by default), the folder location, and specific options. Assignments are valid for the current session, unless made persistent. The document details syntax rules, situations where a libref is not necessary, and best practices for efficient SAS© data management.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP.

1 Code Block
LIBNAME statement
Explanation :
This example shows the assignment of a SAS library to a local folder. The `LIBNAME` statement creates a reference (libref 'mylib') pointing to a physical location. We use `V9` as the library engine, which is the default engine for traditional SAS files. Then, a small table `test_data` is created and saved in this library. `PROC PRINT` is used to display the contents of the table. Finally, the library is de-assigned from the SAS session with `LIBNAME mylib clear;`.
Copied!
1/* Crée un dossier temporaire pour la bibliothèque si nécessaire */
2/* Remplacez 'chemin/vers/votre/bibliotheque' par un chemin réel */
3/* Pour SAS Viya, cela pourrait être un chemin CAS ou un chemin de système de fichiers accessible */
4/* Ici, nous utilisons un dossier local simulé pour l'exemple */
5 
6/* Affecte la bibliothèque 'mylib' au chemin spécifié */
7LIBNAME mylib V9 '%sysfunc(pathname(WORK))/my_sas_library';
8 
9/* Crée une table simple dans la bibliothèque 'mylib' */
10DATA mylib.test_data;
11 INPUT id name $;
12 DATALINES;
131 John
142 Jane
153 Mike
16;
17RUN;
18 
19/* Affiche le contenu de la table */
20PROC PRINT DATA=mylib.test_data;
21 title 'Contenu de mylib.test_data';
22RUN;
23 
24/* Désaffecte la bibliothèque */
25LIBNAME mylib clear;
26 
2 Code Block
LIBNAME statement with options
Explanation :
This example illustrates the use of a library option. The 'readonly' libref is assigned to the V9 engine with the `ACCESS=READONLY` option. This means users will only be able to read data from this library, and any attempt to write or modify (like creating 'new_table' in the example) will result in a SAS error. This is useful for protecting data integrity. The code includes a simulation of an existing file so that read-only access makes sense.
Copied!
1/* Affecte une bibliothèque en mode lecture seule */
2LIBNAME readonly V9 '%sysfunc(pathname(WORK))/my_read_only_library' access=readonly;
3 
4/* Crée une table temporaire pour la démonstration */
5DATA _null_;
6 file '%sysfunc(pathname(WORK))/my_read_only_library/sample.sas7bdat';
7 put 'This is a sample SAS dataset file.';
8RUN;
9 
10/* Tente de créer une nouvelle table dans la bibliothèque en lecture seule (va échouer) */
11DATA readonly.new_table;
12 x=1;
13RUN;
14 
15/* Désaffecte la bibliothèque */
16LIBNAME readonly clear;
17 
3 Code Block
Direct file access
Explanation :
Although using a libref is recommended, this example shows how to access a SAS table by directly specifying its full path and filename in quotes. This works for many SAS language elements but does not offer the flexibility and advantages of libref management, particularly for engine options and simple path modification.
Copied!
1/* Crée un dossier temporaire pour l'exemple */
2/* Créons une table SAS temporaire directement dans un chemin */
3DATA '%sysfunc(pathname(WORK))/my_direct_access_data.sas7bdat';
4 INPUT val;
5 DATALINES;
610
720
830
9;
10RUN;
11 
12/* Accède à la table SAS directement par son chemin complet */
13PROC PRINT DATA='%sysfunc(pathname(WORK))/my_direct_access_data.sas7bdat';
14 title 'Accès direct à la table';
15RUN;
16 
17/* Note: Le fichier reste sur le disque après la session à moins d'être supprimé explicitement */
18 
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


Related Documentation : Data Access

Sujet / Mot-cléLien vers la ressource
DOC FedSQL en/sampleCode/FEDSQL9D66