Practice

Computer Programs

SAS headstart
SAS procedures
Program 1
Program 2
Program 3
Program 4
Program 5
Program 6
Program 7
Program 8
Program 9
Program 10
Program 11
Program 12
Program 13
Program 14

Topical Bibliographies (Readings)

Datasets and Stories

Software Help  


Search for 

Headstart Lesson in SAS

A. What is SAS?

The SAS system is the world's leading enterprisewide Information Delivery System. It supports data access, data management, data analysis, and data presentation. At IU, SAS is available in Windows, Mac, and a few mainframe platforms

B. SAS for Windows

SAS for Windows is a complete implementation of the SAS system. It enables you to perform many analyses on your PC that were once possible only on much larger machines. Windows enables you to switch between applications. Furthermore, SAS for Windows reads data files form a variety of file formats including SPSS, Excel spreadsheets, dBase, and Lotus.

C. Your first SAS program

SAS sample program 1


DATA grades;

INPUT id $ sex $ test1 test2 test3;
average=(test1+test2+test3)/3;

DATALINES;
01 f 83 85 91
02 f 65 . .
03 f 90 94 90
04 m 87 80 82
05 m 78 86 80
;

PROC PRINT DATA=grades;

RUN;



D. Steps for creating and submitting the SAS Sample Program 1 in Windows

Step 1:

Initiate SAS in Windows.

Step 2:

Compose (or Edit) the SAS Sample Program 1 (from the previous page) in PROGRAM EDITOR window [or, in the future, you may open a SAS program file already created but stored in C: or A: drive].

Save your SAS program (filename.sas).

Step 3:

Submit the program by clicking on either the Submit command in Locals menu or the submit icon.

Step 4:

Examine the LOG window for possible error diagnosis.

Step 5:

Examine the OUTPUT window for results of analysis, if no error is found in Step 4.


If there is/are error(s) in the LOG or the OUTPUT window, proceed with Steps 6 and 7 :

Step 6:

Clear the content of both LOG and OUTPUT windows by clicking on the Clear text command on the Edit menu.

Step 7:

Recall the SAS program into PROGRAM EDITOR window. Repeat Step 2 to Step 5 until no error is noticed in the LOG and OUTPUT windows and results are correct.


Step 8:

Print the content of the OUTPUT window. OR

Save the content of the OUTPUT window (filename.lst).



E. Windows in SAS/Windows environment and pull-down menus

As you already witnessed, three windows are most likely encountered in this SAS computing environment: PROGRAM EDITOR, LOG, and OUTPUT windows. These windows behave like any other window in that you can maximize, minimize, scroll, and resize them.

There are different pull-down menus associated with each window mentioned above. The following are brief descriptions of some of the important menus:

File pull-down menu

Open

Enables you to copy a SAS program into the PROGRAM EDITOR.

Save or Save As

Enables you to save a text file to a disk.

Print

Enables you to print out the content of that window.

Print Setup

Enables you to choose and configure your default printer to be used with the SAS System.

Exit

Enables you to end your SAS session.


Edit pull-down menu

Copy

Enables you to copy the selected content into the clipboard.

Paste

Enables you to paste the content of the clipboard into the window.

Clear Text

Enables you to clear the content of the window.

Select All

Enables you to select all the content of the window.


Local pull-down menu

Submit

Enables you to submit your SAS program.

Recall Text

Enables you to recall text in PROGRAM EDITOR.


Window pull-down menu

Program Editor

Enables you to switch to PROGRAM EDITOR window.

Log

Enables you to switch to LOG window.

Output

Enables you to switch to OUTPUT window.



F. The general appearance of any SAS program

DATA datasetname;
INPUT a_list_of_variable_names;
[data transformation statements or LABEL statements;]

DATALINES;
[data lines]
;
RUN;
PROC procedurename [options];

[substatements;]

RUN;

datasetname --

should not exceed 8 characters and should begin with an English character. Underscore "_" is allowed, as long as the numerals are preceded by at least one English character

variable name --

follows the same rules as the datasetname

procedurename --

specifies the name of a statistical procedure

; (semi-colon)--

All SAS statements end with a ";" and there is no need to start or end a statement in a particular column, unlike the SPSS or BMDP commands.

RUN; --

Executes a series of statements



G. More on the DATA step

INPUT statement

The INPUT statement links data values in an input record to corresponding SAS variables. Two styles of writing the INPUT statement are explained below--the list style and the formatted style.

  1. The List style
    Example 1: INPUT ID $ NAME $ PRETEST POSTTEST;

    The List style abide by these rules:
    • All the variable values must be separated by at least one blank space.
    • The character variables must be denoted by a "$" sign after the variable name. Character values must not exceed 8 columns and can be numerical values but blanks are not allowed. Numerical variable values, by default, are 80 columns long. You can override this rule by specifying the width of numerical variables.
    • Missing values must be registered by a "." on data lines.
    • All variable names must be 8 characters or fewer. Variable name starts with an alphabet and can include a "_".
  2. The formatted style uses pointer_controls and formats, such as these:

    @n

    go to column n

    /

    go to the next record

    #n

    go to the nth record

    w.

    numeric width

    w.d

    numeric with decimal

    $ w.

    standard character width


    Parentheses are used to group variables and formats; see Example 2(b) below.

    Example 2:
    • INPUT name $ 20. / sex $ 1. @5 midterm 3.1 #3 final 5.2;
    • INPUT (x1-x5)(8.);
  3. The sign "@@" means that INPUT format is repeatedly applied to data lines till all values are read into the SAS data set.

    Example 3:

    DATA result;
    INPUT GROUP SCORE @@;
    DATALINES;
    1 67 1 87 1 98 1 50
    2 44 2 67 2 49 2 82
    3 99 3 98 3 88 3 69
    ;


Variable creation and transformation

Syntax:

new_var = transformation of original variable(s);
  1. Constant assignment

    Example 4:

    n = 0;
    sex = 'Female';

  2. Simple arithmetic operations: +, -, *, /, **

    Example 5:

    sum = x + y;
    dif = x - y;
    twice = x * 2;
    half = x / 2;
    cubic = x ** 3
    sign = - x;

  3. Some arithmetic functions

    Example 6:

    Absolute value

    s = ABS (x);

    Square root

    r = SQRT (x);

    Natural logarithm

    l = LOG (x);

    Common logarithm

    c = LOG10 (x);



  4. Alphas or cumulative probabilities

    Example 7:

    Standard normal distribution

    PROBNORM(x);

    z_prob = PROBNORM(1.96);

    Student's t distribution

    PROBT(x, df);

    t_prob = PROBT(2.086, 20);

    chi-square distribution

    PROBCHI(x, df);

    chi_prob = PROBCHI(31.2, 11);

    F distribution

    PROBF(x, df1, df2);

    f_prob = PROBF(3.32, 2, 30);



  5. Critical values

    Example 8:

    Standard normal distribution

    PROBIT(p)

    z_value = PROBIT(0.025);

    Student's t distribution

    TINV(p, df)

    t_value = TINV(0.92, 2);

    chi-square distribution

    CINV(p, df)

    chi_value = CINV(0.93, 3);

    F distribution

    FINV(p, df1, df2)

    f_value = FINV(0.95, 2, 10);


  6. Selected functions that calculate simple statistics for each observation (i.e., a person)

    Example 9:

    Total score

    SUM

    a = SUM(OF y1-y3); or a = SUM(y1,y2,y3);

    Arithmetic average

    MEAN

    b = MEAN(OF y1-y3);

    Variance

    VAR

    c = VAR(OF y1-y3);

    Minimum

    MIN

    d = MIN(OF y1-y3);

    Maximum

    MAX

    e = MAX(OF y1-y3);

    Standard deviation

    STD

    f = STD(OF y1-y3);

    Standard error

    STDERR

    g = STDERR(OF y1-y3);

    No. of nonmissing observations

    N

    h = N(OF y1-y3);

    No. of missing observations

    NMISS

    i = NMISS(OF y1-y3);

    Range

    RANGE

    j = RANGE(OF y1-y3);

    Kurtosis

    KURTOSIS

    k = KURTOSIS(OF y1-y3);

    Skewness

    SKEWNESS

    l = SKEWNESS(OF y1-y3);

    Corrected sum of squares

    CSS

    m = CSS(OF y1-y3);

    Uncorrected sum of squares

    USS

    o = USS(OF y1-y3);


Label statement


Label statement allows you to assign label (up to 40 characters) to each SAS variable.

Syntax:

LABEL

variable1 = 'variable label for variable1'
variable2 = 'variable label for variable2'
-
-
-
variablen = 'variable label for variablen';



SAS sample program 2

DATA

roster;
INPUT name $ id $ pretest first second final;
mastery=(first + second + final)/3;
LABEL name='Name of Student'

id='Student Identification Number'
mastery='Mastery score';

DATALINES;
THABET 0595 . . . .

LYNN
DAN
PAUL
HUA
LIN
CATHY
SAINT
HOPE
TIM
MARK
Sassy
WOOD
TED
YOUNG
;

0775 16 29 53 44
0574 18 46 53 54
0462 7 38 33 43
0144 12 41 54 47
7310 12 34 50 32
0887 14 31 47 43
9619 20 45 51 57
2267 17 34 46 50
7391 12 44 52 47
0573 16 33 46 49
4742 18 50 57 56
0719 15 29 39 42
6045 15 24 48 49
8198 18 48 54 54

PROC PRINT DATA=roster;
PROC MEANS DATA=roster;
RUN;



INFILE statement

Raw data can be enclosed along with a SAS program (see sample programs 1 and 2), or they can be stored in a separate file (in ASCII format). If raw data are stored in a separate file (e.g., "class1.dat" stored in A: drive), an INFILE statement needs to be used to read the raw data (see the following example).

Example 10:

DATA

roster;
INFILE 'a:\class1.dat';
INPUT id sex $ pretest first second final;

PROC PRINT DATA=roster;
PROC MEANS DATA=roster; RUN;


H. SAS procedures used in Y603

SAS procedure

Description

PROC PRINT
PROC MEANS
PROC SORT


PROC GLM
PROC CORR
PROC REG

requests SAS to print a SAS data set.
requests SAS to calculate descriptive statistics.
requests SAS to sorts observations in a SAS data set by one or more variables, storing the resulting sorted observations in a SAS data set or replacing the original.
requests SAS to perform ANOVA.
requests SAS to calculate correlations among variables.
requests SAS to perform regression analysis.


I. Documentation

SAS Version 6 documentation is available for reference on Reference shelf (adjacent to the computer workstations) in the Education Library. Several SAS manuals are also available in Room 2011 of the Education Building. The basic documents include:

  • SAS Language and Procedures, Version 6
  • SAS Language, Version 6
  • SAS Procedures Guide, Version 6
  • SAS/STAT User's Guide, Vol. 1 & 2, Version 6

J. Further help

In the future, if you need help in using SAS for Windows, contact the UCS Stat/Math Center (e-mail: statmath@indiana.edu; phone: 855-4724). Valuable locally developed documents and other Internet resources can be obtained through the UCS Stat/Math Center webpage.



Comments: peng@indiana.edu
Dr. Peng's Home Page: Dr. Chao-Ying Joanne Peng
Copyright 1999, The Trustees of Indiana University