Upload
dilbag-sokhi
View
60
Download
2
Tags:
Embed Size (px)
Citation preview
SASSAS LibraryLibrary
And And DatasetsDatasets
Last Updated : 29June, 2004
Center of Excellence
Data Warehousing
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 FilesSAS Files
SAS data sets and other files are stored in SAS data libraries. SASUSER
WORK
PROG2
SAS Data LibrariesSAS 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 - temporary library
SASUSER - permanent library
You can create and access your own permanent libraries.
PROG2 - permanent library
WORK
SASUSER
PROG2
SAS Data LibrariesSAS 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 systemsspecially formatted sequential file
The LIBNAME StatementThe 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 libref 'SAS-data-library' <options>;LIBNAME libref 'SAS-data-library' <options>;
The libref must be 8 characters or fewer.
The LIBNAME Statement: ExamplesThe LIBNAME Statement: Examples
OS/390 Batch and TSOlibname prog2 'edu.prog2.sasdata' disp=shr;
Windows, DOS, and OS/2libname prog2 'c:\prog2';
UNIXlibname prog2 '/user/prog2';
Two-Level SAS Data Set NamesTwo-Level SAS Data Set Names
LibrefSAS Data
Library
SAS Data
libref.SAS-filenamelibref.SAS-filename
The WORK LibraryThe 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 SetAccessing a Permanent SAS Data SetThere 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.
......
libname prog2 'c:\workshop\winsas\prog2';
proc print data=prog2.test noobs;run;
Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set
Windows
Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
......
libname prog2 '/users/prog2';
proc print data=prog2.test noobs;run;
Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set
UNIX
Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
......
libname prog2 '.prog2.sasdata';
proc print data=prog2.test noobs;run;
Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set
OS/390
Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
Creating a Permanent SAS Data SetCreating a Permanent SAS Data SetThere are two steps when creating a
permanent SAS data set:
If the libref has already been assigned in the SAS session, you do not need to assign it again.
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.
......
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 SetCreating a Permanent SAS Data Set
Windows
......
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 SetCreating a Permanent SAS Data Set
UNIX
...
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;
Creating a Permanent SAS Data SetCreating a Permanent SAS Data Set
OS/390
...c01s5d1.sas
Create a SAS Data Set with SAS Data Create a SAS Data Set with SAS Data
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 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 OutputDesired Output
The data set work.fnlscores should contain only the variables LName and Grade.
LName Grade
SMITH PassJONES FailedMOORE PassLEE PassLONG FailedGREEN PassFOREMAN Failed
The SET StatementThe SET Statement
Use a SET statement to read a SAS data set.
General form of a SET statement:
The SET statement points to the SAS data set(s) to be read. Options in the SET statement affect how the data is read.
SET SAS-data-set <options>;SET SAS-data-set <options>;
IF-THEN ELSE StatementsIF-THEN ELSE Statements
One method used to assign values or execute statements conditionally is IF-THEN ELSE statements.
IF condition THEN statement; <ELSE IF condition THEN statement;> … <ELSE statement;>
IF condition THEN statement; <ELSE IF condition THEN statement;> … <ELSE statement;>
The LENGTH StatementThe 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 variable-name <$> length-specification ...;LENGTH variable-name <$> length-specification ...;
The DROP StatementThe DROP Statement
To drop variables that are read or created during the DATA step, use a DROP statement.
General form of a DROP statement:
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.
DROP SAS-variable(s);DROP SAS-variable(s);
Creating a Variable with Conditional LogicCreating 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;
......
LNAME
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
Compile
GRADE SCORED
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
Execute
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAMEGRADE SCORE
.
D
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAMEGRADE SCORE
.
D
0.90SMITH
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
True
LNAMEGRADE SCORE
.
D
Pass 0.90SMITH
......
Write out observation to fnlgrades.
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAMEGRADE SCORE
.
D
Pass 0.90SMITH
Implicit Output
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAMEGRADE SCORE
.
D
Pass 0.90SMITH
Implicit Return
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAME
SMITH
GRADE SCORE
.90
D
Reinitialize PDV. Values of variables not read from SAS are set to missing.
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAME
SMITH
GRADE SCORE
.90
D
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAME
SMITH
GRADE SCORE
.90
D
0.57JONES
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.57MOORE 0.85LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAME
SMITH
GRADE SCORE
.90
D
Failed 0.57JONES
False
......Write out observation to fnlgrades.
PDV
data fnlgrades; length Grade $ 6; drop score; set prog2.test; if score>=.7 then Grade='Pass'; else grade='Failed';run;
LName Score
SMITH 0.90JONES 0.85MOORE 0.57LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAMEGRADE SCORE
.
D
Failed 0.57JONES
Implicit Output
......
PDV
data fnlgrades; length Grade $ 6; drop score; set prog2.test; if score>=.7 then Grade='Pass'; else grade='Failed';run;
LName Score
SMITH 0.90JONES 0.85MOORE 0.57LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAMEGRADE SCORE
.
D
Failed 0.57JONES
Implicit Return
......
PDV
data fnlgrades; length Grade $ 6; drop Score; set prog2.test; if Score>=.7 then Grade='Pass'; else Grade='Failed';run;
LName Score
SMITH 0.90JONES 0.85MOORE 0.57LEE 0.98LONG 0.67GREEN 0.70FOREMAN 0.69
LNAMEGRADE SCORE
.Failed 0.57JONES
Continue processing until end of file marker.
proc print data=fnlgrades noobs;run;
Grade LName
Pass SMITHFailed JONESPass MOOREPass LEEFailed LONGPass GREENFailed FOREMAN
Creating a Variable with Conditional LogicCreating a Variable with Conditional Logic
The VAR StatementThe 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 SAS-variable … ;VAR SAS-variable … ;
proc print data=fnlgrades noobs; var LName Grade;run;
LName Grade
SMITH PassJONES FailedMOORE PassLEE PassLONG FailedGREEN PassFOREMAN Failed
Using the VAR StatementUsing the VAR Statement
c01s5d2.sas
Questions