Saving SAS Data Sets

Doug Hemken

December 2017

SAS Notes

Introduction

In order to process any data, that data must be in the format of a SAS data set, a special SAS binary file which usually has the file extension ".sas7bdat". These are usually saved by writing some SAS code, as opposed to clicking on a "Save" icon on the Toolbar or in the menus.

Ordinary SAS data sets are kept as files in your computer's file system, rather than being loaded in your computer's memory.

In your SAS code, these data files can be addressed in either of two ways, either using an operating system name or using a SAS name. SAS names a usually preferred, because they make your code more portable and easier to read, write, and maintain.

The operating system file name might include the location (the directory or folder) as well as the file name. For example a Windows data file name might look like

"U:\SAS64\class.sas7bdat"

while a Linux data file name could look like

"~/SAS64/class.sas7bdat"

On either operating system the SAS name for these data files might look like

u.class

Operating system file names are ALWAYS enclosed in quotes, while SAS file names are NEVER in quotes. In operating systems names, the file path (location) is included as part of the file name specification. In SAS file names, the location is given by a prefix called a library name. In the SAS example above, u is a library name while class is the data file name.

SAS Names

The basic code to save a SAS data set using the SAS name is

libname U "U:\";
data u.class;
  set sashelp.class;
run;

This produces a log which, in it's own way, says that the file was successfully saved.

2          libname U "U:\";
NOTE: Libref U was successfully assigned as follows: 
      Engine:        V9 
      Physical Name: U:\
3          data u.class;
4            set sashelp.class;
5          run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set U.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.14 seconds
      cpu time            0.03 seconds
      

For now, for purposes of saving SAS data sets, there are two key elements to this code. One is the line that says data u.class;, a "data statement", which instructs SAS where to write the file and what to name it. In this case, the file will be written wherever the library U points to, and will be named class.

The other key element, then, is the line that says libname U "U:\";, a "libname statement", which tells SAS that the library U points to the place the operating system calls "U:\" (I like simple, easy to remember names).

Obviously, the order in which these instructions are given to SAS is important!

The data set name and the library name are chosen by you, but there are certain restrictions (see SAS names). SAS names typically begin with a letter or underscore, and are composed of letters and numerals and underscores - special characters like blanks or parentheses are not (usually) allowed. Library names may be up to eight characters, while data set names may be up to 32 characters.

(The set statement tells SAS where to get data from, and the run; statement tells SAS to execute the instructions accumulated so far.)

Operating System Names

Any place you can use a SAS name for a data set, you could instead use the operating system name. So the code that saves the data set, above, could have been written

data "U:\class.sas7bdat";
  set sashelp.class;
run;

And again we'll check the log.

2          data "U:\class.sas7bdat";
3            set sashelp.class;
4          run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set U:\class.sas7bdat has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.09 seconds
      cpu time            0.01 seconds
      

Pros and Cons

The advantages of using operating system names are

The disadvantage are

In our experience, most SAS programmers use SAS names.