Published on :
Data Access CREATION_INTERNE

Examples: Accessing Data Using a Libref

This code is also available in: Deutsch Español Français
Awaiting validation
This essential guide explores various data access scenarios within the SAS© Viya environment. It details the use of the LIBNAME statement to associate a logical name (libref) with a physical library location, and shows how SAS© functions can be used for the same task, allowing programmatic management. Advanced cases include concatenating multiple SAS© libraries, secure access to libraries on remote servers via SAS©/CONNECT, and integration with WebDAV servers for data sharing. Particular attention is paid to integrating Database Management Systems (DBMS) with SAS©/ACCESS, including creating SAS© views from DBMS tables, highlighting SAS©'s flexibility in working with diverse data sources. Finally, it explains how to dynamically manage library folders and clear librefs to optimize resources.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP, with the exception of examples accessing external sources (SAS/CONNECT, WebDAV, DBMS) for which simulated data or placeholders are used.

1 Code Block
DATA STEP / PROC PRINT Data
Explanation :
This example assigns the libref 'sales' to a specified library path. It then creates a 'quarter1' dataset in this library using a DATA step, and then uses PROC PRINT to display the contents of the dataset. The library path must exist and be accessible by the SAS Compute Server.
Copied!
1LIBNAME sales 'library-path';
2DATA sales.quarter1;
3 LENGTH mileage 4;
4 INPUT account mileage;
5 DATALINES;
61 932
72 563
8;
9PROC PRINT DATA=sales.quarter1;
10RUN;
2 Code Block
Macro / LIBNAME Function / LIBREF Function
Explanation :
This 'test' macro program dynamically assigns a libref ('new') to a directory location ('library-location') using the LIBNAME function. It then checks for successful assignment with the LIBREF function and displays an appropriate message. Paths must be valid and accessible. Functions are preferred for programmatic assignments.
Copied!
1%macro test;
2 %let mylibref=new;
3 %let mydirectory=library-location;
4 %IF %sysfunc(LIBNAME(&mylibref,&mydirectory)) %THEN
5 %put %sysfunc(sysmsg());
6 %ELSE %put success;
7 %IF %sysfunc(libref(&mylibref)) %THEN
8 %put %sysfunc(sysmsg());
9 %ELSE %put library &mylibref is assigned to &mydirectory;
10%mend test;
11 
12%test
3 Code Block
LIBNAME Statement
Explanation :
This LIBNAME statement concatenates two existing SAS libraries, 'lib1' and 'lib2', under the new libref 'lib3'. This allows access to data from both libraries using a single libref. When searching for a dataset, libraries are explored in the order of their list. If a dataset is created, it is placed in the first library of the concatenation.
Copied!
1LIBNAME lib3 (lib1 lib2);
4 Code Block
LIBNAME Statement / PROC DATASETS
Explanation :
This example illustrates how to access a SAS library located on a remote server via SAS/CONNECT. It establishes a TCP/IP connection, connects to the remote server ('myserver') with credentials, and then assigns the libref 'reports' to a remote directory. PROC DATASETS is then used to inspect the remote library. The SAS/CONNECT session is then closed.
Copied!
1options comamid=tcp;
2%let myserver=host.name.com;
3signon myserver.__1234 user=userid password='mypw';
4LIBNAME reports '/myremotedata' server=myserver.__1234;
5PROC DATASETS library=reports;
6RUN;
7QUIT;
8signoff myserver.__1234;
5 Code Block
LIBNAME Statement
Explanation :
This LIBNAME statement assigns the libref 'davdata' to a directory on a WebDAV server. The 'WEBDAV' option is crucial for specifying the access method. Credentials (user and password) are provided for authentication. SAS will temporarily retrieve files to the local disk for processing, then return them to the server after modifications.
Copied!
1LIBNAME davdata v9 "https://www.webserver.com/datadir"
2webdav user="userid" pw="12345";
3 
6 Code Block
DATA STEP / PROC DATASETS Data
Explanation :
This example assigns the libref 'mytddata' to a Teradata database, allowing access to tables as SAS datasets. A DATA step is used to create a 'grades' table directly in the Teradata database. PROC DATASETS is then used to display information about this table. It should be noted that SAS/ACCESS interfaces do not always support the REPLACE= option for DBMS tables.
Copied!
1LIBNAME mytddata teradata server=mytera user=myid password=mypw;
2DATA mytddata.grades;
3 INPUT student $ test1 test2 final;
4 DATALINES;
5Fred 66 80 70
6Wilma 97 91 98
7;
8PROC DATASETS library=mytddata;
9RUN;
10QUIT;
7 Code Block
DATA STEP / PROC PRINT / PROC DATASETS
Explanation :
This example creates a SAS view named 'highgrades' from an existing Teradata table ('mytddata.grades'). The view selects only records where the 'final' variable is greater than 80. PROC PRINT executes this view, and PROC DATASETS shows that 'highgrades' is indeed a SAS view. The librefs for 'target' and 'mytddata' must be assigned before this view can be used.
Copied!
1LIBNAME target 'library-path';
2LIBNAME mytddata teradata server=mytera user=myid password=mypw;
3DATA target.highgrades / view=target.highgrades;
4 SET mytddata.grades;
5 where final gt 80;
6RUN;
7PROC PRINT DATA=target.highgrades;
8RUN;
9PROC DATASETS library=target;
10RUN;
11QUIT;
8 Code Block
OPTIONS statement / LIBNAME Statement
Explanation :
This example shows how to automatically create a subfolder for a SAS library if it does not exist. The 'DLCREATEDIR' system option is enabled, allowing SAS to create the 'project' folder if it is missing from the specified path '/home/userid/mydata/project' when assigning the 'mynewlib' libref.
Copied!
1options dlcreatedir;
2LIBNAME mynewlib '/home/userid/mydata/project';
3 
9 Code Block
LIBNAME Statement
Explanation :
This LIBNAME statement de-assigns the libref 'mylib' from its physical location. This is useful for freeing up resources or changing a libref's assignment. To de-assign all librefs (except system libraries), 'libname _all_ clear;' can be used.
Copied!
1LIBNAME mylib clear;
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