Published on :
Data Access CREATION_INTERNE

Access Data via a Libref

This code is also available in: Deutsch Español Français
Awaiting validation
This documentation explores the fundamental concepts of SAS© library management and data access. Each section presents a specific scenario with corresponding SAS© code, detailed explanations, and key takeaways. The focus is on compatibility with SAS© Viya 4 and SAS© Studio, ensuring that the code is self-contained and directly executable.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP.

1 Code Block
LIBNAME, DATA STEP, PROC PRINT Data
Explanation :
The LIBNAME statement assigns the sales libref to a library location. Substitute your library's location for library-path. The location must already exist and be accessible by the SAS Compute Server. The DATA step creates the sales.quarter1 dataset and stores it in the physical library location. The PROC PRINT step refers to the dataset by its two-level name, sales.quarter1.
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, %SYSFUNC, fonction LIBNAME, fonction LIBREF
Explanation :
The macro variable mylibref specifies the libref name, new. The macro variable mydirectory specifies the library location. Substitute your library's location for library-location. The location must already exist and be accessible by the Compute Server. An IF-THEN-ELSE statement calls the %SYSFUNC macro function, which in turn calls the LIBNAME function to attempt library assignment. If an error or warning occurs, the message is written to the SAS log. If no error or warning occurs, then success is written to the log. Note that in a macro statement, you do not enclose character strings in quotation marks. Another IF-THEN-ELSE statement calls the %SYSFUNC macro function, which calls the LIBREF function to validate the library assignment. Again, if an error or warning occurs, the message is written to the SAS log. If no error or warning occurs, an informational message is written to the log. The test macro executes.
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
Explanation :
The LIBNAME statement concatenates two SAS libraries. The following figure illustrates concatenation. Note that the index for apples does not appear in the concatenation. The lib2.apples dataset has an index. However, the lib1.apples dataset does not have an index, and lib1 is listed first in the concatenation. SAS removes the index when its associated dataset is not part of the concatenation. If multiple catalogs have the same name, their entries are concatenated. The lib3.formats catalog combines the entries from the lib1.formats and lib2.formats catalogs. For more details, see Catalog Concatenation in SAS V9 LIBNAME Engine: Reference. The key idea is that library concatenation allows you to reference multiple libraries stored in different physical locations. When a dataset is opened for input or update, the concatenated libraries are searched, and the first occurrence of the dataset is used. When a dataset is created, it is created in the first library listed in the concatenation, even if a file with the same name exists in another library in the concatenation. Undesirable behavior could occur if datasets with the same name exist in different locations.
Copied!
1LIBNAME lib3 (lib1 lib2);
4 Code Block
OPTIONS, SIGNON, LIBNAME, PROC DATASETS
Explanation :
The COMAMID= system option specifies TCP/IP as the communications access method. The myserver macro variable is assigned to the host name of the remote SAS/CONNECT server. The SIGNON statement refers to the myserver macro variable followed by the port number on which the SAS/CONNECT spawner listens. If your port number or service name is defined in the macro variable, omit it from the SIGNON statement. In the LIBNAME statement, do not specify an engine. Specify the location of your data, and specify the myserver macro variable in the SERVER= option. Include the port number if specified in the SIGNON statement. PROC DATASETS runs on the client. Although the client accesses data that resides on the server, the data is not written to the client's local disk.
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
Explanation :
The LIBNAME statement assigns the davdata libref to the URL location of a WebDAV server. The WEBDAV option is required to access a WebDAV server.
Copied!
1LIBNAME davdata v9 "https://www.webserver.com/datadir"
2webdav user="userid" pw="12345";
3 
6 Code Block
LIBNAME, DATA STEP, PROC DATASETS Data
Explanation :
The LIBNAME statement specifies the mytddata libref and TERADATA, which is the engine nickname for SAS/ACCESS Interface to Teradata. The statement also specifies connection options for Teradata. Modify these options to specify your SAS/ACCESS connection values and any other options you need. The DATA step creates a table named grades. The table resides in the Teradata DBMS and is not a SAS dataset. The PROC DATASETS output for the mytddata library shows that the engine is Teradata. For the grades table, the SAS member type is DATA and the DBMS member type is TABLE.
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
LIBNAME, DATA STEP, PROC PRINT, PROC DATASETS
Explanation :
In the LIBNAME statement, the target libref is assigned to a SAS library location. Substitute your library's location for library-path. No engine is specified, so SAS assigns the default V9 engine. This is the same SAS/ACCESS LIBNAME statement as in the 'Access DBMS Data as a SAS Library' example. The DATA step creates a SAS view named highgrades that refers to the Teradata table named grades. The view includes rows where the final variable is greater than 80. PROC PRINT executes the view. Be aware that DATA step views do not retain the LIBNAME statement. Therefore, when you refer to this view, you must first submit the LIBNAME statements for the mytddata library as well as the target library.
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;
8 Code Block
OPTIONS, LIBNAME
Explanation :
This example sets the DLCREATEDIR system option to create a subfolder for a library. In the example, the /home/userid/mydata/ folder exists, but the project subfolder does not. Since the DLCREATEDIR system option is set, SAS creates project.
Copied!
1options dlcreatedir;
2LIBNAME mynewlib '/home/userid/mydata/project';
3 
9 Code Block
LIBNAME
Explanation :
This statement deassigns the mylib libref from its physical location.
Copied!
1LIBNAME mylib clear;
10 Code Block
LIBNAME
Explanation :
Use the _ALL_ keyword in the LIBNAME statement to deassign all library assignments (other than system libraries).
Copied!
1LIBNAME _all_ clear;
11 Code Block
Macro, %SYSFUNC, fonction LIBNAME
Explanation :
You can also use the LIBNAME function. The following code deassigns the new libref, which was assigned in the 'Assign a Libref with a function' example.
Copied!
1%macro test;
2 %IF (%sysfunc(LIBNAME(new))) %THEN
3 %put %sysfunc(sysmsg());
4%mend test;
5%test
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