22:172 SAS for Data Management, Analysis, and …



22:172 SAS for Data Management, Analysis, and Reporting

Summer, 2003

Instructor: Cowles

Lab 5, June 24, 2003

File to download:

Please go to the Data and Story Library at



Choose ``List all topics"

Energy

Nuclear Power Story

Datafile: Nuclear Plants

We are going to save this file two different ways.

1. Use File menu and Save as.

Choose location for file and be sure to save as type: Text file (*.txt)

2. a. Right click on page were table is displayed. From popup menu, choose Export

to Microsoft Excel.

b. Click on the little arrow right where the table of data begins. Then click

Import.

c. Use "existing worksheet."

d. In Excel, Use File menu and Save as. Save as file type with extension .xls.

Using the saved text file.

First we will examine the text file visually to figure out how to use a data step

to read it.

Count how many lines of header need to be skipped (25).

Arrow across one of the rows of data to determine whether the delimiters are

spaces or tabs. Your cursor will jump the width of a tab.

Here are two versions of data steps that will read this file correctly.

options linesize = 75 ;

* reading in tab-delimited file using expandtabs option ;

data nuclear ;

infile 'a:nuclear plants datafile.txt' expandtabs firstobs = 26 ;

input Cost Mwatts Date ;

run ;

proc print data = nuclear ;

run ;

* reading in tab-delimited file using dlm = option ;

data nuclear2 ;

infile 'a:nuclear plants datafile.txt' dlm = '09'x firstobs = 26 ;

input Cost Mwatts Date ;

run ;

proc print data = nuclear2 ;

run ;

Reading in the Excel spreadsheet using proc import

Proc import creates SAS datasets by reading in external files. It can be used to read files in many different formats, including those created by Excel, Microsoft Access, SPSS, etc. Here is code for this Excel spreadsheet.

* reading in Excel spreadsheet using proc import and datafile statement with .xls extension ;

proc import

datafile = "a:nuclear.xls" /* where is data */

out = nuclear3 ; /* name SAS dataset */

run ;

proc print data = nuclear3 ;

run ;

Reading in the Excel spreadsheet using the Import wizard

From the File menu, choose Import data. Choose the appropriate type of input file,

in this case Excel 2000 or Excel 97. Press Next. Type in the file specification, or

browse for it. Press next. Fill in the desired SAS dataset name as "Member." Press

next. If you wish to save the proc import code that the Import wizard writes, fill in a

file specification with the extension .sas. Then click Finish.

The Import wizard is nice if you are going to use the external file only once in SAS.

If you need to access it and run the same program on it repeatedly, it is much more

efficient to use proc import in the SAS program. One way to get the appropriate proc

import code is to have the Import wizard generate it.

Creating permanent SAS datasets

A SAS dataset created by a data step or by proc import is a temporary structure in SAS’s memory. It disappears when we end the current session with SAS. It may be complicated – it may have been created by merging two or more files, and it may contain formats and labels. If we are likely to want to use the same SAS dataset as input to other programs, or if the program that created it is going to be run many, many times on the same data, it is efficient to save the dataset as a permanent file on disk. It is then a permanent SAS dataset. Permanent SAS datasets are also a good way to give prepared data to other SAS users.

Unlike plain ASCII text files, permanent SAS datasets can be read and manipulated only by SAS.

Two steps go into the creation of a permanent SAS dataset. First, the LIBNAME statement is used to assign a nickname to the subdirectory in which we want SAS to put the new permanent dataset. (That subdirectory or path must already exist.) Then, when we create the dataset in a data step, we use a two-part name for it: the first part is the nickname for the subdirectory; then a period; then the dataset name.

The following modified version of the data step to read the tab-delimited file will read that ASCII file and write it to a new permanent SAS dataset in the subdirectory a:\katelibs

LIBNAME mkc172 ‘a:\katelibs’ ;

data mkc172.nuclear ;

infile 'a:nuclear plants datafile.txt' expandtabs firstobs = 26 ;

input Cost Mwatts Date ;

label Date = ‘Construc permit; years since 1900’ ;

run ;

We could continue to run SAS procedures on the mkc172.nuclear dataset in the current session. Or we could exit from SAS and come back later and access the permanent SAS dataset without having to use a data step.

LIBNAME mkc172 ‘a:\katelibs’ ;

proc means data = mkc172.nuclear ;

run ;

proc print label data = mkc172.nuclear ;

run ;

The contents procedure will give information about a permanet dataset.

proc contents data = mkc172.nuclear ;

run ;

Note that we could have used a different nickname for the subdirectory when accessing the permanent dataset than when creating it.

Working with permanent SAS datasets with formats

If you create a permanent SAS dataset which uses formats that you created with proc format, then you must make the format library, as well as the dataset, permanent. (You do not have to do this if you have used only built-in SAS formats.) Often you will want to keep the format library and the associated permanent datasets all in the same subdirectory. The following code will create a permanent SAS dataset of the nuclear data, with formats.

LIBNAME mkc172 ‘a:\katelibs’ ;

OPTIONS FMTSEARCH=(mkc172) ; * tell SAS to put format library in directory nicknamed mkc172 ;

Proc format library = mkc172 ;

Value datefmt low-50 = ‘Old’ >50-high = ‘New’ ;

Run ;

data mkc172.nuclear ;

infile 'a:nuclear plants datafile.txt' expandtabs firstobs = 26 ;

input Cost Mwatts Date ;

format Date datefmt. ;

run ;

If we exit from SAS and bring it up again later, the following code would access the existing permanent dataset and permanent format library.

libname mkc172 'a:\katelibs' ;

options fmtsearch = (mkc172) ;

proc means data= mkc172.nuclear ;

run ;

proc print data = mkc172.nuclear ;

run ;

proc contents data = mkc172.nuclear ;

run ;

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download