Addressing SAS Data Sets

Doug Hemken

December 2017

SAS Notes

Examples

Data set names may be up to 32 characters long, and should begin with a letter or an underscore, _.
They should be composed of letters, numbers, and underscores, with no blanks or special characters.

Data sets (and some other files) may be specified in a SAS program in one of two ways, either in the language of the computer operating system, or in the SAS language using a logical reference called a ‘library.’ The operating system’s name for a file’s location and the SAS library reference are connected through a LIBNAME statement.

Note: a library name must be 8 characters or less.

Example: under Windows, a SAS file is referenced in quotes, giving the full path and file name.

proc print data="y:\sas\data\employees" obs=15; run;

Example: using a SAS library;

libname y "y:\sas\data";
proc print data=y.employees obs=10; run;

A DATA step example;

data newemp1;
    set "y:\sas\data\employees";
    run;

data newemp2;
    set y.employees;
    run;

SAS libraries, and the files within them, may be either permanent (they remain stored on disk) or temporary (erased when your SAS session ends). The temporary library is called WORK, and is automatically created each time you start SAS.

Example: data saved in a permanent data set.

libname u "u:\";
data u.sim1;
    do i=1 to 10;
        x=ranuni(-1);
        output;
    end;
    run;

Anything referenced directly through Windows is permanent.

data "u:\sim2";
    do i=1 to 10;
        x=ceil(6*ranuni(-1));
        output;
    end;
    run;

Example: data temporarily saved, in WORK;

data work.sim3;
    do i=1 to 20;
        x=rannor(-1)+5;
        output;
    end;
    run;

If no library reference is given, the data is stored in WORK.

data sim4;  
    do i=1 to 20;
        x=rannor(-1) - 5;
        output;
    end;
    run;