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!
data mytable;
x=1;
run;
proc contents data=mytable;
run;
1
DATA mytable;
2
x=1;
3
RUN;
4
PROC CONTENTSDATA=mytable;
5
RUN;
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!
/* Création d'une table exemple pour les besoins de l'illustration */
data quarter1;
input Quarter $ Sales;
datalines;
Q1 100
Q2 150
Q3 200
Q4 250
;
run;
libname sales '/sas/data/permanent';
options user=sales;
proc print data=quarter1;
run;
/* Autre méthode d'affectation de la bibliothèque User */
libname user '/sas/data/permanent';
proc print data=quarter1;
run;
data _null_;
x=libname ('user', '/sas/data/permanent');
run;
proc print data=quarter1;
run;
1
/* Création d'une table exemple pour les besoins de l'illustration */
2
DATA quarter1;
3
INPUT Quarter $ Sales;
4
DATALINES;
5
Q1 100
6
Q2 150
7
Q3 200
8
Q4 250
9
;
10
RUN;
11
12
LIBNAME sales '/sas/data/permanent';
13
options user=sales;
14
PROC PRINTDATA=quarter1;
15
RUN;
16
17
/* Autre méthode d'affectation de la bibliothèque User */
18
LIBNAME user '/sas/data/permanent';
19
PROC PRINTDATA=quarter1;
20
RUN;
21
22
DATA _null_;
23
x=LIBNAME ('user', '/sas/data/permanent');
24
RUN;
25
PROC PRINTDATA=quarter1;
26
RUN;
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!
/* Création d'une table exemple pour les besoins de l'illustration */
data work.mydata;
input ID Name $;
datalines;
1 John
2 Jane
;
run;
/* Supposons que 'mydata.sas7bdat' est stocké dans /sas/data/temp/ */
/* Remplacez '/sas/data/temp/mydata.sas7bdat' par le chemin réel de votre fichier */
proc print data='/sas/data/temp/mydata.sas7bdat';
run;
1
/* Création d'une table exemple pour les besoins de l'illustration */
2
DATA work.mydata;
3
INPUT ID Name $;
4
DATALINES;
5
1 John
6
2 Jane
7
;
8
RUN;
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 */
12
PROC PRINTDATA='/sas/data/temp/mydata.sas7bdat';
13
RUN;
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!
filename test url "http://support.sas.com/publishing/cert/sampdata.txt";
data credit;
infile test firstobs=945 obs=954;
input Account $ 1-4 Name $ 6-22 Type $ 24 Transaction $ 26-31;
run;
proc print data=credit;
run;
1
filename test url "http://support.sas.com/publishing/cert/sampdata.txt";
2
DATA credit;
3
INFILE test firstobs=945 obs=954;
4
INPUT Account $ 1-4 Name $ 6-22 Type $ 24 Transaction $ 26-31;
5
RUN;
6
PROC PRINTDATA=credit;
7
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.