East Carolina University
Department of Psychology

Data Files in SAS Libraries

    While I generally prefer to keep my data in plain text files in directories unknown to SAS before I point to them with an INFILE statement, one can use SAS to organize data files and other files into libraries known to SAS. Try the following little exercise:


Creating a SAS Library
  1. Create a folder on your hard drive which will serve as a SAS library. I created the folder D:\SAS\SASdata.

  2. Double-click the LIBRARIES icon in the Explorer window in SAS (which appears on the left hand side when you boot up SAS, but I usually close it to give me more screen space for the editor, log, and output windows).

  3. Right click in the resulting window and select NEW.

  4. In the New Library window enter "Sol" in the name field. Leave the Engine at "Default," but you may want to drop down the menu there to see the various engines available. Use the BROWSE key to point at the directory you just created to house SASdata files.  Check "Enable at startup."  OK.


Entering Data Into a Member of the Library
  1. An icon for the library you created should appear in the Explorer Libraries window.

  2. Double-click the icon for the library in which you wish enter a data file.  Right click in the resulting window and select NEW, TABLE, OK.

  3. You get a VIEWTABLE window, like a spreadsheet.


  4. Right click on the header of the leftmost, "A," column and click Data, Column Attributes.  In the Name field, enter "Shots." In the Label field enter "Shots of Scotch consumed." Select Type = Numeric . Close.


  5. Do the same for Column B, using the Name "Errors," Label "Typographic Errors."

  6. Enter into the data cells the data as shown below.


  7. Close the window (X in upper right corner). Tell SAS Yes, you want to save the changes. In the SAVE AS window, double click the Sol icon to point at that library and give a MEMBER NAME of "Scotch." Click SAVE.

The new data file will be added to the library.


Accessing a Data Member From the DATA Step: The LIBNAME and SET Commands

    After creating the Scotch data member, run the following program as an example of how to read in a Library Data File:

LIBNAME sol 'D:\SAS\SASdata' ;
tipsy; Set sol.Scotch;
Corr; var shots errors; run;

    The LIBNAME points SAS to the location of SASdata on my hard drive.  Be sure that your LIBNAME points to the correct name and location of your library.

    The SET command has an argument of library_name.member_name. Note that the names of variables are included in the library data file, you do not have to include them in an INPUT statement. This can be quite handy if you will be using a data file many times.

    It is rather inefficient to use DATA and SET.  More efficient would be to point to the SAS data file in a PROC statement, like this:

LIBNAME sol 'D:\SAS\SASdata' ;
corr data=sol.Scotch; var shots errors; run;


Using the Import Wizard to Convert a Standard Data File to a SAS Member.

    You can also create SAS Library Data Files by using the SAS Import Wizard to convert another type of data file. For example, I had an Excel spreadsheet with salary faculty for faulty.  To convert this Excel file into an SAS Library Data File, I simply did the following:

    Within SAS, I clicked File, Import Data. I told SAS that the data to be imported was in an Excel file, then I pointed at the file on my hard drive, I told it to put it in the library named sol, member (name) EPA_Salaries05, FINISH.  The SAS data file then appeared in my library.


Creating a SAS Data Member From Within the DATA Step.

    Another way to create an SAS data file is to use a library dot member name in the DATA statement. Consider the following program:

LIBNAME sol 'D:\SAS\SASdata' ;
Data Sol.XlogY; input x y @@;
log_x = log10(x);
2 10 2 9 2 8 3 9 3 8 5 8 8 7 12 8 17 6
Proc Means skewness; var x y log_x;
Proc Corr nosimple; var x log_x; with y; run;

    Note that the data statement identifies a system file with library name Sol and member name XlogY.  After you run this program you will find a new member in the Sol library. That member will be named XlogY and will include the data from this program, both the X, Y data that followed the Cardsstatement and the log-transformed X data created in the DATA step.


Using a SAS Data File Obtained From Another Person

    First, copy it into the folder to which LIBNAME will point.  The file should a name something like this:  NAME.sas7bdat.  Submit code like this to find out what is in the SAS data file.

LIBNAME sol 'D:\SAS\SASdata' ;
CONTENTS data=sol.NAME;run;

You will get a list of the variable names and some other useful information.


birds flying

Back to the SAS Help PageBack to the SAS Help Page

spider in web
Contact Information for the Webmaster,
Dr. Karl L. Wuensch

This page most recently revised on 3. December 2007.