the sas ® system additional information on statistical analysis programming

26
The SAS ® System Additional Information on Statistical Analysis Programming

Upload: oswaldo-shine

Post on 02-Apr-2015

229 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: The SAS ® System Additional Information on Statistical Analysis Programming

The SAS® System

Additional Information on

Statistical Analysis Programming

Page 2: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Other ways to read data

Page 3: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Four General Categories: Create SAS data set from raw data files

This is what we’ve been doing so far Enter directly into SAS data set Convert other software’s data files into SAS

data sets Read other software’s data files directly

Page 4: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

CAUTION! Why are ASCII and Text files better to work

with? Because they’re permanent! Most other ways that you can read data into

SAS data sets are temporary – they last while your session is open They must be converted into permanent data sets

to be reused later Data are easily lost

Page 5: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Raw data files: The DATA step is versatile enough to read

almost any type of raw data file (ASCII, Text) Internal, External, List files, Columns, Non-

standard (dates, dollars, etc.) Import Wizard reads particular types of raw

data files like comma-separated values (CSV), and other delimited files Can also import from Excel and Access

Page 6: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Import Wizard: File Menu Import

Data Select the type of data

(Excel, Access, Comma Delimited, Tab Delimited)

Next, choose the location of the file you want to import

Page 7: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Import Wizard: Choose a SAS library

(WORK is temporary, save in other to keep)

Name the data set (“Member”)

Last window gives you the option to save the PROC IMPORT commands

Page 8: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

If you’re not going to modify the data in any way, you can run procedures with the DATA command specifying where your data set is

SAS library.File name

Page 9: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

To read the data into a program, start with the “IMPORT” commands that you saved, then add your program

SAS library.file name

Original File

Typeof

File

Page 10: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Entering Data Directly with Viewtable Window The “Viewtable” window will let you both view

& modify existing SAS data sets as well as create new SAS data sets

Displays SAS data sets (also called “tables”) in a tabular view

Page 11: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Viewtable window Tools Menu Table

Editor Start typing & SAS will

determine numeric v. character data, or

Right click on a letter (column) & choose “Column Attributes”

Page 12: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Viewtable window Enter your data Save your data: File

menu Save As… Either select an existing

library, or create a new library

Name your file (‘Member Name’)

Page 13: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

If you’re not going to modify the data in any way, you can run direct DATA commands

Page 14: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Now, to read the data into a program, you use an “INFILE” statement with the location of the file you created

Page 15: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Reading Other Software’s Data Files Directly In most cases, SAS/ACCESS for PCs will

allow you to directly import Excel, Lotus, and dBase

You use the IMPORT procedure, and add a “DBMS” code for the type of data:

PROC IMPORT DATAFILE = ‘filename’ OUT = data-set

DBMS = identifier REPLACE;

Page 16: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

For Excel, Lotus, and dBase files

Type of File Extension DBMS Identifier

Microsoft Excel .xls EXCEL*

EXCEL5

EXCEL4

Lotus .wk4 WK4

.wk3 WK3

.wk1 WK1

dBase .dbf DBF

*The same identifier is used for Excel, Excel2002, Excel2000, Excel97

Page 17: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

Page 18: The SAS ® System Additional Information on Statistical Analysis Programming

Getting Data into SAS ®

To read Access files, instead of DATAFILE, you use TABLE and DATABASE

PROC IMPORT TABLE = ‘table-name’ OUT = data-set DBMS = identifier

REPLACE; DATABASE = ‘database-path’

Type of File Extension DBMS Identifier

Microsoft Access .mdb ACCESS*

ACCESS97

*The same identifier is used for Access, Access2002, Access2000

Page 19: The SAS ® System Additional Information on Statistical Analysis Programming

Other Useful Things

Arrays, correction commands, etc.

Page 20: The SAS ® System Additional Information on Statistical Analysis Programming

Array Statements

Sometimes you want to do the same thing to many variables. Instead of writing a long series of IF statements, you can build an array

In SAS, an array is a group of variables They need to be all numeric or all character

ARRAY name (n) variable-list;

Page 21: The SAS ® System Additional Information on Statistical Analysis Programming

Array Statements

Next, you tell SAS what you want it to do to this group of variables by creating a ‘DO Loop’ (everything between DO and END)

ARRAY name (n) variable-list;

DO code;

IF something THEN something;

END;

Page 22: The SAS ® System Additional Information on Statistical Analysis Programming

Array Statements

This array changes all values of “9” (refused) to missing data for the four variables in the array

Page 23: The SAS ® System Additional Information on Statistical Analysis Programming

Means

You may want to calculate a mean response value for two cross-tabbed variables What was the mean

response for the question on parking for people in each age category?

Page 24: The SAS ® System Additional Information on Statistical Analysis Programming

Correcting a Data Error

If you find that data has been incorrectly entered into the data set, rather than correcting it in the ASCII set, add a command to the SAS program

Tells the program that before running anything else, it must change the value for ‘age’ in id #123 to 35

Page 25: The SAS ® System Additional Information on Statistical Analysis Programming

Create a Random Number List

Sometimes you need to randomly select cases from your dataset to work with for analysis or assessment purposes

SAS can generate a random number list for you

This example creates 25 random numbers

Page 26: The SAS ® System Additional Information on Statistical Analysis Programming

The SAS® System

There’s really not much you can’t do in the way of working with data sets in SAS

Just a matter of learning the right code and a little trial and error!