Published on :
Data Access CREATION_INTERNE

Examples: Accessing Data Without Using a Libref

This code is also available in: Deutsch Español Français
Awaiting validation
The functional analysis details the management of data libraries in SAS©. It explains how the SAS© system handles single-level file names, directing them by default to the Work library for temporary data. For permanent files without an explicit libref, the configuration of the User library is presented. In addition, methods for direct access via the full file path and the use of filerefs for external raw data are explored, offering flexibility in how data sources are referenced and used.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) for SAS tables to ensure autonomy, and an external URL file for raw data.

1 Code Block
DATA STEP / PROC CONTENTS Data
Explanation :
This code creates a SAS table named 'mytable' with a variable 'x' initialized to 1. Since no libref is specified (single-level name), the table is automatically stored in the temporary Work library. The PROC CONTENTS procedure displays the metadata of this table, confirming its location in the Work library. This method is ideal for intermediate or non-persistent data.
Copied!
1DATA mytable;
2 x=1;
3RUN;
4PROC CONTENTS DATA=mytable;
5RUN;
2 Code Block
LIBNAME / OPTIONS / PROC PRINT Data
Explanation :
This example demonstrates how to designate a 'User' library to store permanent data with single-level names. Initially, a 'quarter1' table is created for illustration purposes. Then, the 'sales' libref is assigned to a permanent path via the LIBNAME statement. The system option USER=sales configures 'sales' as the default library for single-level names. Thus, PROC PRINT data=quarter1 will read the table from 'sales.quarter1'. The additional code blocks show alternative methods for assigning the 'User' library, either directly with a 'user' libref, or via the LIBNAME function in a DATA _NULL_ step, while achieving the same behavior for data access. Note that '/sas/data/permanent' is an example path, which must be replaced by a real path on your system.
Copied!
1/* Création d'une table exemple pour les besoins de l'illustration */
2DATA quarter1;
3 INPUT Quarter $ Sales;
4 DATALINES;
5Q1 100
6Q2 150
7Q3 200
8Q4 250
9;
10RUN;
11 
12LIBNAME sales '/sas/data/permanent';
13options user=sales;
14PROC PRINT DATA=quarter1;
15RUN;
16 
17/* Autre méthode d'affectation de la bibliothèque User */
18LIBNAME user '/sas/data/permanent';
19PROC PRINT DATA=quarter1;
20RUN;
21 
22DATA _null_;
23 x=LIBNAME ('user', '/sas/data/permanent');
24RUN;
25PROC PRINT DATA=quarter1;
26RUN;
3 Code Block
PROC PRINT
Explanation :
This example shows how to access a SAS dataset by specifying its full physical path and file name (including the .sas7bdat extension), all within quotation marks, instead of using a libref. A 'mydata' table is first created in the Work library. It is assumed that the corresponding SAS file 'mydata.sas7bdat' is then saved to a specific location like '/sas/data/temp/'. The PROC PRINT procedure can then directly read this file using the full path. This method is useful when one does not wish to assign a temporary or permanent libref, although many SAS language elements prefer librefs.
Copied!
1/* Création d'une table exemple pour les besoins de l'illustration */
2DATA work.mydata;
3 INPUT ID Name $;
4 DATALINES;
51 John
62 Jane
7;
8RUN;
9 
10/* Supposons que 'mydata.sas7bdat' est stocké dans /sas/data/temp/ */
11/* Remplacez '/sas/data/temp/mydata.sas7bdat' par le chemin réel de votre fichier */
12PROC PRINT DATA='/sas/data/temp/mydata.sas7bdat';
13RUN;
4 Code Block
FILENAME / DATA STEP / PROC PRINT Data
Explanation :
This example illustrates the use of a fileref to reference an external (non-SAS) raw data file. The FILENAME statement assigns the fileref 'test' to a remote URL containing the data. The 'credit' DATA step uses the INFILE statement with the 'test' fileref to read a specific set of lines (945-954) from this external file, defining the variables 'Account', 'Name', 'Type', and 'Transaction'. Finally, PROC PRINT displays the newly created SAS table 'credit', thus verifying the successful import of external data. Filerefs are essential for interacting with files that are not members of SAS libraries, such as text or CSV files.
Copied!
1filename test url "http://support.sas.com/publishing/cert/sampdata.txt";
2DATA credit;
3 INFILE test firstobs=945 obs=954;
4 INPUT Account $ 1-4 Name $ 6-22 Type $ 24 Transaction $ 26-31;
5RUN;
6PROC PRINT DATA=credit;
7RUN;
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