44
SAS SAS Library Library And And Datasets Datasets Last Updated : 29June, 2004 Center of Excellence Data Warehousing

Sas Library Dataset Creation

Embed Size (px)

Citation preview

Page 1: Sas Library Dataset Creation

SASSAS LibraryLibrary

And And DatasetsDatasets

Last Updated : 29June, 2004

Center of Excellence

Data Warehousing

Page 2: Sas Library Dataset Creation

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.

Page 3: Sas Library Dataset Creation

SAS FilesSAS Files

SAS data sets and other files are stored in SAS data libraries. SASUSER

WORK

PROG2

Page 4: Sas Library Dataset Creation

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

Page 5: Sas Library Dataset Creation

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

Page 6: Sas Library Dataset Creation

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.

Page 7: Sas Library Dataset Creation

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';

Page 8: Sas Library Dataset Creation

Two-Level SAS Data Set NamesTwo-Level SAS Data Set Names

LibrefSAS Data

Library

SAS Data

libref.SAS-filenamelibref.SAS-filename

Page 9: Sas Library Dataset Creation

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

Page 10: Sas Library Dataset Creation

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.

Page 11: Sas Library Dataset Creation

......

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

Page 12: Sas Library Dataset Creation

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

Page 13: Sas Library Dataset Creation

......

libname prog2 '/users/prog2';

proc print data=prog2.test noobs;run;

Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set

UNIX

Page 14: Sas Library Dataset Creation

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

Page 15: Sas Library Dataset Creation

......

libname prog2 '.prog2.sasdata';

proc print data=prog2.test noobs;run;

Viewing a Permanent SAS Data SetViewing a Permanent SAS Data Set

OS/390

Page 16: Sas Library Dataset Creation

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

Page 17: Sas Library Dataset Creation

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.

Page 18: Sas Library Dataset Creation

......

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

Page 19: Sas Library Dataset Creation

......

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

Page 20: Sas Library Dataset Creation

...

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

Page 21: Sas Library Dataset Creation

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.

Page 22: Sas Library Dataset Creation

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

Page 23: Sas Library Dataset Creation

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>;

Page 24: Sas Library Dataset Creation

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;>

Page 25: Sas Library Dataset Creation

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 ...;

Page 26: Sas Library Dataset Creation

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);

Page 27: Sas Library Dataset Creation

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;

Page 28: Sas Library Dataset Creation

......

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

Page 29: Sas Library Dataset Creation

......

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

Page 30: Sas Library Dataset Creation

......

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

Page 31: Sas Library Dataset Creation

......

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

Page 32: Sas Library Dataset Creation

......

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

Page 33: Sas Library Dataset Creation

......

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

Page 34: Sas Library Dataset Creation

......

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.

Page 35: Sas Library Dataset Creation

......

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

Page 36: Sas Library Dataset Creation

......

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

Page 37: Sas Library Dataset Creation

......

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

Page 38: Sas Library Dataset Creation

......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

Page 39: Sas Library Dataset Creation

......

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

Page 40: Sas Library Dataset Creation

......

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.

Page 41: Sas Library Dataset Creation

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

Page 42: Sas Library Dataset Creation

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 … ;

Page 43: Sas Library Dataset Creation

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

Page 44: Sas Library Dataset Creation

Questions