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 permanent libraries.
PROG2 - permanent library
PROG2
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' ; ; 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
Libref
SAS 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 SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 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 SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 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 SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 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 LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
The scores from a final exam are stored in the SAS data set prog2.test. The professor needs to assign each student a passing grade if the score is 0.7 or above and a failing grade 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 JONES MOORE LEE LONG GREEN FOREMAN
Pass Failed Pass Pass Failed Pass 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 ; ;
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 IFTHEN 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 LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
PDV D
GRADE
LNAME
SCORE
... ...
Execute LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
PDV D
GRADE
LNAME
SCORE
. ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
PDV D
GRADE
LNAME SMITH
SCORE 0.90 . ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
data fnlgrades; length Grade $ 6; Score drop Score; set prog2.test; 0.90True if Score>=.7 then Grade='Pass'; 0.57 else Grade='Failed'; 0.85 run; 0.98 0.67 0.70 0.69
PDV
D
GRADE Pass
LNAME SMITH
SCORE 0.90 . ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run; Implicit Return
Implicit Output
PDV D
GRADE
LNAME
Pass
SMITH
SCORE 0.90 .
Write out observation to fnlgrades. ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run; Implicit Output
PDV D
GRADE
LNAME
Pass
SMITH
SCORE 0.90 .
Write out observation to fnlgrades. ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run; Implicit Return
PDV D
GRADE
LNAME
Pass
SMITH
SCORE 0.90 . ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
Reinitialize PDV. Values of variables not read from SASPDV are set to missing. D
GRADE
LNAME
SMITH
SCORE
.90 ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
PDV D
GRADE
LNAME
SMITH
SCORE
.90 ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
PDV D
GRADE
LNAME
SCORE
JONES SMITH
0.57 .90 ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.57 0.85 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; False set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
PDV D
GRADE Failed
LNAME
SCORE
JONES SMITH
0.57 .90 ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.85 0.57 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop score; set prog2.test; if score>=.7 then Grade='Pass'; else grade='Failed'; run; Implicit Return
Implicit Output
PDV D
GRADE Failed
LNAME JONES
SCORE 0.57 .
Write out observation to fnlgrades.
... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.85 0.57 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop score; set prog2.test; if score>=.7 then Grade='Pass'; else grade='Failed'; run; Implicit Output
PDV D
GRADE Failed
LNAME JONES
SCORE 0.57 .
Write out observation to fnlgrades.
... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.85 0.57 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop score; set prog2.test; if score>=.7 then Grade='Pass'; else grade='Failed'; run; Implicit Return
PDV D
GRADE Failed
LNAME JONES
SCORE 0.57 . ... ...
LName SMITH JONES MOORE LEE LONG GREEN FOREMAN
Score 0.90 0.85 0.57 0.98 0.67 0.70 0.69
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed'; run;
Continue processing until end of file marker.
PDV GRADE Failed
LNAME JONES
SCORE 0.57 . ... ...
Creating a Variable with Conditional Logic proc print data=fnlgrades noobs; run; Grade
LName
Pass Failed Pass Pass Failed Pass Failed
SMITH JONES MOORE LEE LONG GREEN 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 JONES MOORE LEE LONG GREEN FOREMAN
Pass Failed Pass Pass Failed Pass Failed c01s5d2.sas
Questions