The LIBNAME statement is more than just a shortcut; it is a critical layer of abstraction in SAS architecture. While your code demonstrates that direct path access is possible (and useful for one-off tasks or debugging), it bypasses the management benefits of a libref.
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!
/* Crée un dossier temporaire pour la bibliothèque si nécessaire */
/* Remplacez 'chemin/vers/votre/bibliotheque' par un chemin réel */
/* Pour SAS Viya, cela pourrait être un chemin CAS ou un chemin de système de fichiers accessible */
/* Ici, nous utilisons un dossier local simulé pour l'exemple */
/* Affecte la bibliothèque 'mylib' au chemin spécifié */
libname mylib V9 '%sysfunc(pathname(WORK))/my_sas_library';
/* Crée une table simple dans la bibliothèque 'mylib' */
data mylib.test_data;
input id name $;
datalines;
1 John
2 Jane
3 Mike
;
run;
/* Affiche le contenu de la table */
proc print data=mylib.test_data;
title 'Contenu de mylib.test_data';
run;
/* Désaffecte la bibliothèque */
libname mylib clear;
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é */
/* Crée une table simple dans la bibliothèque 'mylib' */
10
DATA mylib.test_data;
11
INPUT id name $;
12
DATALINES;
13
1 John
14
2 Jane
15
3 Mike
16
;
17
RUN;
18
19
/* Affiche le contenu de la table */
20
PROC PRINTDATA=mylib.test_data;
21
title 'Contenu de mylib.test_data';
22
RUN;
23
24
/* Désaffecte la bibliothèque */
25
LIBNAME 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!
/* Affecte une bibliothèque en mode lecture seule */
libname readonly V9 '%sysfunc(pathname(WORK))/my_read_only_library' access=readonly;
/* Crée une table temporaire pour la démonstration */
data _null_;
file '%sysfunc(pathname(WORK))/my_read_only_library/sample.sas7bdat';
put 'This is a sample SAS dataset file.';
run;
/* Tente de créer une nouvelle table dans la bibliothèque en lecture seule (va échouer) */
data readonly.new_table;
x=1;
run;
/* Désaffecte la bibliothèque */
libname readonly clear;
1
/* Affecte une bibliothèque en mode lecture seule */
/* Tente de créer une nouvelle table dans la bibliothèque en lecture seule (va échouer) */
11
DATA readonly.new_table;
12
x=1;
13
RUN;
14
15
/* Désaffecte la bibliothèque */
16
LIBNAME 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!
/* Crée un dossier temporaire pour l'exemple */
/* Créons une table SAS temporaire directement dans un chemin */
data '%sysfunc(pathname(WORK))/my_direct_access_data.sas7bdat';
input val;
datalines;
10
20
30
;
run;
/* Accède à la table SAS directement par son chemin complet */
proc print data='%sysfunc(pathname(WORK))/my_direct_access_data.sas7bdat';
title 'Accès direct à la table';
run;
/* Note: Le fichier reste sur le disque après la session à moins d'être supprimé explicitement */
1
/* Crée un dossier temporaire pour l'exemple */
2
/* Créons une table SAS temporaire directement dans un chemin */
/* 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.
Notice: Undefined variable: stmtFrFallback in /var/www/app/detail_sascode.php on line 1428
Fatal error: Uncaught Error: Call to a member function execute() on null in /var/www/app/detail_sascode.php:1428
Stack trace:
#0 {main}
thrown in /var/www/app/detail_sascode.php on line 1428