Section 3.5: LIBNAME -- Specify the location of permanent SAS datasets LIBNAME is a stand-alone statement that specifies the subdirectory to store or retrieve SAS datasets external to the command file. With it a SAS dataset can be stored "permanently" on disk like other files. To make a SAS dataset permanent, first enter the LIBNAME statement to identify the directory where you will store the data. The LIBNAME statement appropriate to the system you are working on must appear before the DATA step or PROC where the data set is created or read. LIBNAME statements are global and when invoked take effect immediately. One does not need to place a RUN; statement following them. To demonstrate this, open the Explorer window and click on "Libraries". Then in the editor window highlight a LIBNAME statement and click on Run. The libname icon should appear immediately in the explorer window. A libref (logical name) is used to identify the physical location of your SAS files. The "libref" follows the keyword LIBNAME (choose "libref" to have a valid name). It acts as a link to the directory where SAS data sets are created or are to be read by the program. Subdirectory specifications depend on the system you're using. Do not use the names sasuser and sashelp for the "libref". On Windows system, enter the directory path as: LIBNAME libref 'c:\project\data'; * be sure to enter the backslash; If files are accessed in the same folder where you run SAS, enter: LIBNAME libref '.'; * a period specifies the current working directory; If you are accessing data from a directory other than your working folder, specify the directory location within the quotation marks: LIBNAME libref 'c:\project\data'; NOTE: enter a ~/ and the forward slash on UNIX systems. And if you are accessing data from some other user's directory on a UNIX system, you need to specify the complete root directory/subdirectory name: LIBNAME libf 'c:\data\'; Your colleague must make the file(s) readable to others. You can check the file 'readability' status with the unix command: ls -l /home1/collegues_username/project/data/*.dat The following program reads data from a text file called mydata.dat and outputs the variables id, a, b, and c from each observation to the SAS data set libref.newdat defined in the DATA statement. LIBNAME libref 'c:\project\data'; DATA libref.newdat; INFILE 'project.dat' missover; INPUT id $ a b c; OUTPUT libref.newdat; RUN; The left-hand portion of the data set name, libref, refers only to the location of the directory identified in the LIBNAME statement that appears prior to the DATA statement. The actual file name - newdat - is provided by you to define the name of the newly created SAS data set. When saved to the directory specified by libref, the permanent SAS data set for version 8 will be listed as follows: unix: newdat.sas7bdat PC: newdat.sas7bdat Once the data are read into a permanent SAS dataset they can be accessed by separate SAS programs that contain any SAS procedure using a LIBNAME statement and the DATA=libref.newdata option in the PROC statement. OPTIONS nocenter ls=78 ps=55; LIBNAME libref '.'; PROC DATA=libref.newdat ; < SAS statements > RUN; Since you need to use SAS to look at the contents of SAS data sets, here are two quick ways to determine what is in them if you need to know the names of variables, or types of data, and so forth. PROC CONTENTS DATA=libref.newdat; PROC PRINT DATA=libref.newdat(OBS=5); * print the first 5 observations; When a SAS library is written, the version of the library is defined with the engine. SAS has a Multiple Engine Architecture that enables reading and writing from files in different formats (e.g., versions). Since datasets are written or read from the DATA step, the default engine (BASE) points the SAS data library to an empty directory. To be specific about the engine you want to use (esp. if you are trying to read datasets written with previous versions), specify the engine on the LIBNAME statement. A partial list of valid engine values currently includes: V9, V8, V7, V6, and BASE. When you run the following statement, notice the output printed to the log file: LIBNAME dat 'c:\data'; NOTE: Libref DAT was successfully assigned as follows: Engine: V8 Physical Name: c:\data If you have a SAS dataset that is currently saved in Version 8 format and want to save it as Version 9, enter the V8 engine in the LIBNAME statement to specify its current location and then convert it to a version 9 dataset with another LIBNAME statement: LIBNAME in V8 "c:\sas\v8_data"; * to specify version 8.2; LIBNAME dat V9 "c:\sas\v9_data"; * to specify version 9.1, V9 is not needed; DATA dat.bank; SET in.bank; RUN; How to convert version 6 datasets to ver 8 or 9? libname sasv6 v6 'c:\temp1'; libname sasv8 v8 'c:\temp2'; proc copy in=sasv6 out=sasv8 memtype=data; run; Changing the destination library engine in the above statements will convert datasets from a lower version to the higher version. Reading and Writing Data from and to Excel Files The LIBNAME statement is covered at length when reading Excel files in the pages found at: http://www.uoregon.edu/~robinh/data_transfer.html Converting SPSS Datasets Assume you have first saved an SPSS data file in *.por format with "File-Save-As", you can read it into a SAS dataset by issuing the spss engine with a LIBNAME statement: LIBNAME in spss '~/spss/bank.por'; DATA bank; SET in.bank; RUN; PROC Contents DATA=bank; PROC Print DATA=bank(obs=25) NOobs; RUN; Beginning with Version 8 of SAS on the PC you can directly reference datasets on the DATA or SET statements or within procedures without defining a LIBNAME. This is done by enclosing the physical SAS dataset file name and directory in quotes. * Write a version 8 dataset ; DATA 'c:\sas\test.sas7bdat' ; a=5; b=2; run ; The log file reads: NOTE: The data set c:\sas\test.sd7 has 1 observations and 2 variables. NOTE: DATA statement used: real time 0.06 seconds cpu time 0.03 seconds The file name of the SAS dataset will actually be test.sas7bdat, though you are able to read or write it with the 3-letter suffix. If you rename the file as test1.sas7bdat you can print its contents (or use any other procedure) with: PROC PRINT DATA='c:\sas\test1.sd7' ; run ; Obs a b 1 5 2 MAKE SINGLE LEVEL DATASETS BECOME PERMANENT * All single level datasets automatically go to the WORK library, which is automatically cleared when exiting the SAS system; DATA test; x=1; run; You can define a USER libref which will send all datasets to a permanent location: LIBNAME user 'c:\sas'; DATA test; x= 1; run; When finished free the USER fileref to redirect datasets to WORK; LIBNAME user clear; This technique relies on a feature that automatically sends single level datasets to the WORK library. However there is another feature that says that if you allocate a library called USER then all single level datasets will be stored there, rather than in WORK. This means that you can use single level dataset names, and while testing they will be put in WORK and deleted at the end of the SAS session. You can then keep those datasets by simply allocating the USER library and pointing it to a directory to keep those datasets.