Information Technology Services

Visit Us at Carothers Library Lower Level or call/email us at HelpDesk@uri.edu - 401-874-HELP (4357)

Handout 1

The purpose of this handout is to introduce key components of the SAS System through the development of a simple SAS program and SAS basics for Windows, including:

Overview Of A SAS Program
The DATA Step
Points To Remember When Writing A SAS Program
Writing a DATA Step
The PROC Step
Sample SAS Programs and SAS basics for Windows

Part 1: Overview of A SAS Program

The SAS System helps you to organize and analyze a collection of data items using SAS programming statements. A SAS program is a collection of SAS statements in a logical sequence. There are generally two major components of a SAS program:

a. The DATA Step
b. The PROC Step

It is important to note that a SAS program may have several DATA steps or PROC steps. It may also have no DATA steps and all PROC steps, or vice versa.

Part 2: The DATA Step

The DATA step has two major functions:

a. Create a SAS data set from ASCII data
b. Modify other previously created SAS data sets

In this handout we shall concentrate on part (a). For discussion purposes assume we have the following data set:

City Dept Revenue
LA A100 5000
Chicago B100 3000
Texas C100 6000
Dallas D100 4000

Remarks:

The data set has three variables (number of columns).
The data set has four observations.
There is only one record per observation.
The first two variables are character variables.
The third variable is a numeric variable.

Part 3: Points to Remember When Writing A SAS Program

1. All SAS statements begin with a keyword and end with a semicolon.
2. Except for within the data section, SAS is not sensitive to spacing between words.
3. Comments are entered in a SAS program using either one of the following formats:

a. /* text */ (use for large comment blocks)
b. * text ; (use for single line comments)

Part 4: Writing a Data Step

The DATA Statement

Purpose: Start a DATA step.

Form: DATA data_ set_name;

Keyword: DATA

Naming rules: Limited to 32 characters. First character must be alphabetic or an underscore ‘_’. The entire data set name must contain only letters, numbers, or underscore.

The INPUT Statement

Purpose: Describe the format of the ASCII data.

Form: INPUT Variable names Variable formats;

Keyword: INPUT

Naming rules: Limited to 32 characters. First character must be alphabetic or an underscore ‘_’.

The entire data set name must contain only letters, numbers, or underscore.

Notes:
As a rule SAS assumes all variables to be numeric. If the variable has character values the variable can be defined as character by placing a $ sign after the variable’s name on the INPUT statement in LIST input.

The default missing data value is a dot ‘.’ in LIST input.

The INFILE Statement

Purpose: Direct SAS to read an external data file.

Form: INFILE fileref;

or

INFILE ‘path’;

Keyword: INFILE

The CARDS Statement

Purpose: Indicate the beginning of instream data.

Form: CARDS;

Keyword: CARDS

Notes: Do not use this statement if using an external data file.

Part 5: The PROC Step

Statistical analysis in SAS consists of using one or more procedures (PROCs). After the creation of a SAS data set, you can invoke any of the SAS procedures to analyze the data set. The PROC step indicates which SAS data set is to be processed, lists the variables to be included in the analysis, and other options specific to the procedure. Discussion of the commonly used PROCs will follow in a later handout.

Part 6: Sample SAS Programs and SAS basics for Windows

I. A sample SAS program using instream data.

1. Create a sample SAS program; enter the following in the Editor window:

DATA Budget;
INPUT Name $ Dept $ Revenue;
CARDS;
LA A100 5000
Chicago B100 3000
Texas C100 6000
Dallas D100 4000
;
PROC PRINT;

RUN;

2. Submit the SAS job by highlighting the code and execute the menu command:

Run -> Submit (or click on the toolbar icon ‘Submit’). When the Output

window becomes the active window, your job is complete. It may or may not

be successful.

3. Check the Log window to see if your job is successful. If there is any error in

your program, make sure that active window is the Editor window. Click on

the bottom cursor to switch the windows.

4. Before resubmit your program clear the Log and Output windows: Edit ->

Clear. This step is not required it may prove useful since SAS appends

information from subsequent runs.

II. A sample SAS program using data from an external file.

The main advantage of using an External data file is ease of reading the program code and debugging. The data might come from other sources and can be used without being in the SAS code.

Assume the SAS data file ‘revenue.dat’ is saved in “C: My documents My SAS documents”. It contains the following information:

LA A100 5000
Chicago B100 3000
Texas C100 6000
Dallas D100 4000

1. Enter the following SAS program in the Editor window:

DATA Budget;
INFILE ‘c:My documentsMy SAS documentsrevenue.dat’;
INPUT Name $ Dept $ Revenue;
PROC PRINT;

RUN;

2. Submit the job.

3. Check the Output and Log windows.

III. Creating and reading a permanent SAS data set.

All of the above SAS programs create temporary SAS data sets. That is, once SAS finishes executing the program all temporary data sets are annihilated. This prevents you from reusing your data set in other SAS programs unless you recreate the SAS data set. However, you do have the option of saving your SAS data set permanently on your disk. The only modification is in the DATA statement. That is, change all DATA statements to a two-level name.

In the above examples change all data statements to read as follows:

LIBNAME dept ‘d:’;

DATA dept.budget;

…(insert your other statements here)…

PROC PRINT DATA=Dept.Payroll;
RUN;

The above statements will create a new permanent library named ‘dept’ on drive ‘d’ and a new SAS data file named ‘budget’ in the library ‘dept’. The information in this file can then be used in other SAS programs without having to recreate the entire data set.

* You also can create a new library under SAS window by the following steps:

1. Create a new folder named ‘dept’ in drive ‘d’.

2. Click on ‘New Library’ icon on the tool bar and type the new library referece ‘dept’ in the Name field. Use the Browse button to find the folder ‘dept’. In addition, check the box for ‘Enable at startup’. Then click OK.

IV. Importing Data from a Spreadsheet.

SAS allows you to import data from a spreadsheet as a raw data file. The following example reads an Excel file spreadsheet into a SAS data set.

a. SAS Programs:

Assume the Excel data file ‘revenue.xls’ is saved in “C: My documentsMy SAS documents”. The following PROC process shows how to import the ‘sheet1′ of the revenue.xls data, and save it in SAS work library with a name ‘budget’.

Enter the following SAS program in the Editor window:

PROC import out=work.budget’;
Datafile= “c:My documentsMy SAS documentsrevenue.xls”
Dbms=Excel2000 Replace;
Sheet=”Sheet1″;

Getnames=yes;

RUN;

b. SAS Windows:

1. Execute the menu command: File -> Import Data.

2. Under the ‘SAS Import Wizard Window’, select Microsoft Excel 97, 2000 or

2002 as a data source from the checkbox, and chick on ‘Next’.

3. Under the ‘Connect to MS’ window, click on ‘browse’ to find the excel file you

wish to import. Then select your excel table in the next window.

4. Choose a SAS destination: Confirm that WORK is selected in the Library, and

name your SAS data file.

5. At the next screen (prompting for a file name in which to store PROC IMPORT statements), click on ‘Finish’ button.

The imported data will be accessed through the Explorer window by double clicking on Libraries then Work then the name of the Member data set you assigned.

Think Big We Do

Copyright © 2014 University of Rhode Island.