You are on page 1of 46

SAS

Library
And
Datasets

Last Updated : 29June, 2004

Center of
Excellence
 Review the concept of SAS data libraries.
 Review the LIBNAME statement.
 Review creating a new SAS data set from
an existing data set.
 Review conditional processing.
SAS Files
SAS data sets and
other files are stored
in SAS data libraries. SASUSER

WORK

PROG2
SAS Data Libraries
A SAS data library is a collection of SAS files that are
recognized as a unit by SAS on your operating environment.

WORK

 WORK - temporary library


SASUSER

 SASUSER - permanent library


You can create and access your own PROG2
permanent libraries.
 PROG2 - permanent library
SAS Data Libraries
The physical structure of a SAS data
library depends on your operating system.

Directory-based operating systems


(Windows or UNIX)
 any folder or sub-directory

OS/390 systems
 specially formatted sequential file
The LIBNAME Statement
The LIBNAME statement establishes the
library reference (or libref), which is an
alias for the SAS data library.

General form of the LIBNAME statement:

LIBNAME
LIBNAME libref
libref 'SAS-data-library'
'SAS-data-library' <options>;
<options>;

The libref must be 8 characters or fewer.


The LIBNAME Statement:
Examples
OS/390 Batch and TSO
 libname prog2 'edu.prog2.sasdata'
disp=shr;

Windows, DOS, and OS/2


 libname prog2 'c:\prog2';

UNIX
 libname prog2 '/user/prog2';
Two-Level SAS Data Set Names

libref.SAS-filename
libref.SAS-filename

SAS Data

SAS
Libref Data
Library
The WORK Library
The WORK library is the default library. If
you do not specify a library reference on a
SAS data set name, SAS assumes the
libref is work.
work.fltat1 fltat1
Accessing a Permanent SAS
Data Set
There are two steps when accessing a
permanent SAS dataset:

 Use a LIBNAME statement to set up a libref that


points to the location of the data set.

 Reference the data set using the libref as the


first part of the data set name.

 If the libref has already been assigned in the SAS


session,
 you do not need to assign it again.
Viewing a Permanent SAS Data
Set
Windows
libname prog2 'c:\workshop\winsas\prog2';

proc print data=prog2.test noobs;


run;

...
...
Viewing a Permanent SAS Data
Set

LName Score

SMITH 0.90
JONES 0.57
MOORE 0.85
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69
Viewing a Permanent SAS Data
Set
UNIX

libname prog2 '/users/prog2';

proc print data=prog2.test noobs;


run;

...
...
Viewing a Permanent SAS Data
Set

LName Score

SMITH 0.90
JONES 0.57
MOORE 0.85
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69
Viewing a Permanent SAS Data
Set
OS/390

libname prog2 '.prog2.sasdata';

proc print data=prog2.test noobs;


run;

...
...
Viewing a Permanent SAS Data
Set

LName Score

SMITH 0.90
JONES 0.57
MOORE 0.85
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69
Creating a Permanent SAS Data
Set
There are two steps when creating a permanent
SAS data set:

1. Use a LIBNAME statement to set up a libref that


points to the location you want to save to.

2. Use the libref as the first level of the SAS data set
name.

If the libref has already been assigned in the SAS


session, you do not need to assign it again.
Creating a Permanent SAS Data
Set
Windows
libname prog2 'c:\workshop\winsas\prog2';

data prog2.fltat1;
infile 'fltat1.dat';
input @1 EmpID $5.
@7 HireDate date9.
@17 Salary 5.;
Bonus=.05*Salary;
run;

...
...
Creating a Permanent SAS Data
Set
UNIX
libname prog2 '/users/prog2';

data prog2.fltat1;
infile 'fltat1.dat';
input @1 EmpID $5.
@7 HireDate date9.
@17 Salary 5.;
Bonus=.05*Salary;
run;

...
...
Creating a Permanent SAS Data
Set
OS/390
libname prog2 '.prog2.sasdata';

data prog2.fltat1;
infile '.prog2.rawdata(fltat1)';
input @1 EmpID $5.
@7 HireDate date9.
@17 Salary 5.;
Bonus=.05*Salary;
run;

c01s5d1.sas ...
...
Create a SAS Data Set with SAS
Data

The scores from a final


LName Score
exam are stored in the SAS
SMITH 0.90 data set prog2.test. The
JONES 0.57 professor needs to assign
MOORE 0.85 each student a passing
LEE 0.98 grade if the score is 0.7 or
LONG 0.67 above and a failing grade
GREEN 0.70
FOREMAN 0.69
otherwise. The variable
Score should not appear in
the output data set.
Desired Output
The data set work.fnlscores should
contain only the variables LName and
Grade.
LName Grade

SMITH Pass
JONES Failed
MOORE Pass
LEE Pass
LONG Failed
GREEN Pass
FOREMAN Failed
The SET Statement
Use a SET statement to read a SAS data set.
General form of a SET statement:

SET
SETSAS-data-set
SAS-data-set <options>;
<options>;

The SET statement points to the SAS data


set(s) to be read. Options in the SET
statement affect how the data is read.
IF-THEN ELSE Statements
One method used to assign values or
execute statements conditionally is IF-
THEN ELSE statements.

IF
IFcondition
conditionTHEN
THENstatement;
statement;
<ELSE
<ELSEIF IF condition
conditionTHEN
THENstatement;>
statement;>


<ELSE
<ELSEstatement;>
statement;>
The LENGTH Statement
When creating character variables with
conditional logic or functions, it is usually
a good idea to assign the lengths
explicitly using a LENGTH statement.
General form of a LENGTH statement:

LENGTH
LENGTHvariable-name
variable-name<$>
<$> length-specification
length-specification...;
...;
The DROP Statement
To drop variables that are read or created
during the DATA step, use a DROP statement.
General form of a DROP statement:

DROP
DROPSAS-variable(s);
SAS-variable(s);

Variables dropped with a DROP statement are


read into the PDV but are not output to the new
SAS data set. They are available for processing
during the DATA step.
Creating a Variable with
Conditional Logic

data fnlgrades;
length Grade $ 6;
drop Score;
set prog2.test;
if Score>=.7 then Grade='Pass';
else Grade='Failed';
run;
Compile data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69

PDV
D
GRADE LNAME SCORE

...
...
Execute data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69
PDV

D
GRADE LNAME SCORE
.

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69
PDV

D
GRADE LNAME SCORE
SMITH .
0.90

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90True if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69
PDV

D
GRADE LNAME SCORE
Pass SMITH .
0.90

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67 Implicit Return Implicit
GREEN 0.70 Output
FOREMAN 0.69
PDV
D
GRADE LNAME SCORE
Pass SMITH .
0.90
Write out observation to fnlgrades.

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67 Implicit
GREEN 0.70 Output
FOREMAN 0.69
PDV
D
GRADE LNAME SCORE
Pass SMITH .
0.90
Write out observation to fnlgrades.

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67 Implicit Return
GREEN 0.70
FOREMAN 0.69
PDV
D
GRADE LNAME SCORE
Pass SMITH .
0.90

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70 Reinitialize PDV. Values of
FOREMAN 0.69 variables not read from
SASPDV
are set to missing.

D
GRADE LNAME SCORE
SMITH .90

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69

PDV
D
GRADE LNAME SCORE
SMITH .90

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69

PDV
D
GRADE LNAME SCORE
SMITH
JONES .90
0.57

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
False
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.57 else Grade='Failed';
MOORE 0.85 run;
LEE 0.98
LONG 0.67
GREEN 0.70
FOREMAN 0.69

PDV
D
GRADE LNAME SCORE
Failed SMITH
JONES .90
0.57

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop score;
set prog2.test;
SMITH 0.90 if score>=.7 then Grade='Pass';
JONES 0.85 else grade='Failed';
MOORE 0.57 run;
LEE 0.98 Implicit Return Implicit
LONG 0.67 Output
GREEN 0.70
FOREMAN 0.69

PDV
D
GRADE LNAME SCORE
Failed JONES .
0.57
Write out observation to fnlgrades.
...
...
data fnlgrades;
length Grade $ 6;
LName Score drop score;
set prog2.test;
SMITH 0.90 if score>=.7 then Grade='Pass';
JONES 0.85 else grade='Failed';
MOORE 0.57 run;
LEE 0.98
Implicit
LONG 0.67 Output
GREEN 0.70
FOREMAN 0.69

PDV
D
GRADE LNAME SCORE
Failed JONES .
0.57
Write out observation to fnlgrades.
...
...
data fnlgrades;
length Grade $ 6;
LName Score drop score;
set prog2.test;
SMITH 0.90 if score>=.7 then Grade='Pass';
JONES 0.85 else grade='Failed';
MOORE 0.57 run;
LEE 0.98 Implicit Return
LONG 0.67
GREEN 0.70
FOREMAN 0.69

PDV
D
GRADE LNAME SCORE
Failed JONES .
0.57

...
...
data fnlgrades;
length Grade $ 6;
LName Score drop Score;
set prog2.test;
SMITH 0.90 if Score>=.7 then Grade='Pass';
JONES 0.85 else Grade='Failed';
MOORE 0.57 run;
LEE 0.98 Continue processing
LONG 0.67 until end of file marker.
GREEN 0.70
FOREMAN 0.69

PDV

GRADE LNAME SCORE


Failed JONES .
0.57

...
...
Creating a Variable with
Conditional Logic
proc print data=fnlgrades noobs;
run;

Grade LName

Pass SMITH
Failed JONES
Pass MOORE
Pass LEE
Failed LONG
Pass GREEN
Failed FOREMAN
The VAR Statement
To control which variables are displayed
and the order in which they are displayed,
use the VAR statement.

General form of the VAR statement:

VAR
VARSAS-variable
SAS-variable…
…;;
Using the VAR Statement
proc print data=fnlgrades noobs;
var LName Grade;
run;

LName Grade

SMITH Pass
JONES Failed
MOORE Pass
LEE Pass
LONG Failed
GREEN Pass
FOREMAN Failed

c01s5d2.sas
Questions