s as Workshop

download s as Workshop

of 30

Transcript of s as Workshop

  • 7/31/2019 s as Workshop

    1/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    SAS WORKSHOP

    Yuexiao Dong

    Department of StatisticsFox School of Business & Management

    Temple University

    August 24, 2010

    Yuexiao Dong SAS WORKSHOP

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    2/30

  • 7/31/2019 s as Workshop

    3/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Outline

    1 Introduction to SAS

    2 Using SAS for Windows

    3 SAS data handling: Data Step

    Read dataUseful statements

    4 SAS Procedures

    Yuexiao Dong SAS WORKSHOP

    http://find/
  • 7/31/2019 s as Workshop

    4/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Objectives

    This workshop is designed for beginning users who want to getstarted with the program, and experienced users who want to

    get to know the new generation of SAS system. Through this

    workshop, you may

    Get accustomed to the SAS system;

    Understand how SAS processes a program;

    Get familiar with the data handling process;

    Manage data sets in SAS;

    Perform simple statistical analysis.

    Yuexiao Dong SAS WORKSHOP

    http://goforward/http://find/http://goback/
  • 7/31/2019 s as Workshop

    5/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    What is SAS

    SAS stands for Statistical Analysis System. It is both a

    statistical language and a system that performs sophisticated

    data management and statistical analysis.

    SAS is available in multiple computing environments, includingWindows, Mac, UNIX, etc.

    SAS for Windows will do every task that other editions of SAS

    do, it is easy to use, and is more powerful.

    Yuexiao Dong SAS WORKSHOP

    I d i SAS

    http://find/
  • 7/31/2019 s as Workshop

    6/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Availability of SAS 9 for windows

    At Temple, SAS 9 is available for...

    Yuexiao Dong SAS WORKSHOP

    I t d ti t SAS

    http://find/
  • 7/31/2019 s as Workshop

    7/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Outline

    1 Introduction to SAS

    2 Using SAS for Windows

    3 SAS data handling: Data Step

    Read dataUseful statements

    4 SAS Procedures

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    8/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Windows display system

    Explorer/Result window, provide control and organizationof the SAS objects and system output;

    Editor window, color coding, edit program;

    Log window, diagnostics of the programming syntax and

    hints for debugging;

    Output window, show results output by SAS procedures,

    blank if program does not go through correctly;

    Many other windows, such as Library window, Filename

    window, Options window, Graphic windows, etc;

    Keys window is a road map to SAS windows and gives you

    shortcuts to different windows;

    Type "keys" in the command box at the upper left corner.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    9/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Steps of using SAS

    Entering SAS statements.

    Each statement can go on several lines, but it MUST endwith a semi-colon;

    Conventions on Windows operations are applicable, cut,paste and save for example;Format of display, cascade or tile under Window option atthe menu bar.

    Submitting SAS statements.

    Saving SAS statements.Retrieve SAS programs. File option at the menu bar.

    Ending a SAS session.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    10/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Processing of a SAS program

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/
  • 7/31/2019 s as Workshop

    11/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Outline

    1 Introduction to SAS

    2 Using SAS for Windows

    3 SAS data handling: Data Step

    Read dataUseful statements

    4 SAS Procedures

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/
  • 7/31/2019 s as Workshop

    12/30

    Introduction to SAS

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Read data by CARDS

    A DATA step consists of a group of statements that reads ASCII

    text data (in a computer file) or existing SAS data sets in orderto create a new SAS data set.

    DATA one;INPUT x y z @@;

    CARDS;1 2 3 4 5 6 7 8 9

    ;

    RUN;

    PROC PRINT DATA=one;RUN;

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    13/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Read data by CARDS

    This sample SAS program creates a temporary data set using

    the CARDS statement to read in the in-line data.

    The DATA statement defines a temporary data set;

    The INPUT statement defines the variables and theirformats;

    The CARDS statement gives instruction to start reading inthe data that follow.

    The RUN statement ends this session of the program andsubmits for processing;

    Data manipulation must be done in a DATA step andcannot be done in a PROC step.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/
  • 7/31/2019 s as Workshop

    14/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Read free formatted data

    DATA cars1;

    INPUT make $ model $ mpg weight price;CARDS;

    AMC Concord 22 2930 4099

    AMC Pacer 17 3350 4749Buick Electra 15 4080 7827

    ;PROC PRINT DATA=cars1(obs=1);

    RUN;It is usually a good idea to print the first few cases of your

    dataset to check that things were read correctly. $ means theprevious variable is characteristic.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    15/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Read fixed formatted data

    DATA cars2;

    INPUT make $ 1-5 model $ 6-12 mpg 13-14

    weight 15-18 price 19-22;

    CARDS;

    AMC Concord2229304099

    AMC Pacer 1733504749

    AMC Spirit 2226403799

    BuickCentury2032504816

    BuickElectra1540807827

    ;RUN;

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    http://find/
  • 7/31/2019 s as Workshop

    16/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Read free formatted data (space delimited) by INFILE

    Heres what the data in the file cars3.txt look like:

    AMC Concord 22 2930 4099AMC Pacer 17 3350 4749

    AMC Spirit 22 2640 3799Buick Century 20 3250 4816

    Buick Electra 15 4080 7827

    To read the data from cars3.txt into SAS, use the following

    syntax:

    DATA cars3;INFILE "c : \cars3.txt";INPUT make $ model $ mpg weight price;

    RUN;

    Yuexiao Dong SAS WORKSHOP

    Introduction to SAS

    U i SAS f Wi d R d d

    http://find/
  • 7/31/2019 s as Workshop

    17/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Read free formatted data (comma delimited) by

    INFILEHeres what the data in the file cars4.txt look like:

    AMC,Concord,22,2930,4099AMC,Pacer,17,3350,4749

    AMC,Spirit,22,2640,3799Buick,Century,20,3250,4816Buick,Electra,15,4080,7827

    To read the data from cars4.txt into SAS, use the following

    syntax:DATA cars4;INFILE "c : \cars4.txt" delimiter=,;INPUT make $ model $ mpg weight price;

    RUN; Yuexiao Dong SAS WORKSHOP

    Introduction to SASU i SAS f Wi d R d d t

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    18/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Reading fixed formatted data by INFILE

    Heres what the data in the file cars5.txt look like:

    AMC Concord2229304099

    AMC Pacer 1733504749

    AMC Spirit 2226403799

    BuickCentury2032504816

    BuickElectra1540807827

    To read the data from cars5.txt into SAS, use the following

    syntax:

    DATA cars5;INFILE "c : \cars5.txt";INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price

    19-22;RUN;

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows Read data

    http://find/
  • 7/31/2019 s as Workshop

    19/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Create a library by LIBNAME

    Previous examples use CARDS or INFILE to create temporarydata set. Now we create a permanent data set is and storeunder the library project1.

    LIBNAME myproj c : \sasworkshop;

    DATA myproj.cars1;SET cars1;PROC PRINT;

    RUN;

    A SAS library is like a special SAS pointer to a location whereyour SAS files are stored. To create a library, we useLIBNAME nickname name-of-directory;

    where nickname should begin with either a letter or anunderscore and having no more than 8 characters.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows Read data

    http://find/
  • 7/31/2019 s as Workshop

    20/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    SET/DROP/KEEP/RENAME

    DATA temp1;SET cars1(DROP=make model);RENAME price=newprice;

    PROC PRINT;RUN;

    DATA temp2(KEEP=price);SET cars1(DROP=make model);

    PROC PRINT;RUN;

    DATA temp3;SET cars1;IF upcase(make) EQ BUICK;PROC PRINT;

    VAR make model;RUN;Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows Read data

    http://find/
  • 7/31/2019 s as Workshop

    21/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    IF-THEN/ELSE statements

    LIBNAME myproj c : \sasworkshop;DATA myproj.new;SET cars1;

    IF mpg GE 20 THEN fuelefficiency =

    high

    ;ELSE fuelefficiency=low;RUN;

    PROC PRINT;

    RUN;Here we create a new variable fuelefficiency. Then we can print

    the most recently created data set.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows Read data

    http://find/
  • 7/31/2019 s as Workshop

    22/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Read data

    Useful statements

    Subsetting datasets

    DATA high low;SET myproj.new;

    IF fuelefficiency = high THEN output high;

    ELSE output low;RUN;

    The IF-THEN statements draw subsets from the data set by the

    fuelefficiency variable. We have created two temporary

    datasets named high and low. What is the print result next?PROC PRINT;RUN;

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    23/30

    Using SAS for Windows

    SAS data handling: Data Step

    SAS Procedures

    Outline

    1 Introduction to SAS

    2

    Using SAS for Windows

    3 SAS data handling: Data Step

    Read dataUseful statements

    4 SAS Procedures

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    24/30

    g

    SAS data handling: Data Step

    SAS Procedures

    General syntax

    A PROCedure step calls a SAS procedure to analyze or

    process a SAS dataset. The PROC step begins with a PROCstatement and ends with a RUN statement. All of the statistical

    procedures require the input of a SAS data set.

    The general syntax for a PROC step is:

    PROC name [DATA=dataset] [options];[other PROC-specific statements;][BY varlist;]RUN;

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    http://find/
  • 7/31/2019 s as Workshop

    25/30

    g

    SAS data handling: Data Step

    SAS Procedures

    General syntax

    PROC name [DATA=dataset] [options];[other PROC-specific statements;][BY varlist;]

    RUN;

    SAS keywords are in uppercase;

    User-supplied words are in lowercase;

    Options are in brackets [ ] . Note that you do not type thebrackets when programming.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    http://find/
  • 7/31/2019 s as Workshop

    26/30

    SAS data handling: Data Step

    SAS Procedures

    General syntax

    A SAS program can contain any number of DATA and PROC

    steps.

    Once a dataset has been created, it can be processed byany subsequent DATA or PROC step;

    All SAS statements start with a keyword (DATA, INPUT,PROC, etc.) and end with a semicolon;

    Uppercase and lowercase are equivalent, except insidequote marks ( sex = m is not the same as sex = M).

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    http://goforward/http://find/http://goback/
  • 7/31/2019 s as Workshop

    27/30

    SAS data handling: Data Step

    SAS Procedures

    Useful procedures

    PROC PRINT;

    check the data being read by SAS;

    PROC CONTENTS [data = _all_];descriptions of the contents of one or more files;

    PROC SORT; BY var; RUN;

    sort the data by the specified variable var;

    PROC MEANS;

    compute statistics for an entire SAS data set or for groupsof observations in the data set.

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    SAS d h dli D S

    http://find/
  • 7/31/2019 s as Workshop

    28/30

    SAS data handling: Data Step

    SAS Procedures

    Useful procedures

    PROC UNIVARIATE provides detail on the distribution of onevariable.

    DATA Measures;

    INPUT Diameter @@;CARDS;5.501 5.251 5.404 5.366 5.445 5.576 5.607 5.200 5.977 ...

    ;RUN;

    title Normal Q-Q Plot for Diameters;PROC UNIVARIATE data=Measures noprint;QQPLOT Diameter / normal;

    RUN;

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    SAS d t h dli D t St

    http://goforward/http://find/http://goback/
  • 7/31/2019 s as Workshop

    29/30

    SAS data handling: Data Step

    SAS Procedures

    PROC UNIVARIATE

    Normal Q-Q Plot

    -3 -2 -1 0 1 2 3

    5.0

    5.2

    5.4

    5.6

    5.8

    6.0

    6.2

    6.4

    Diameter

    Normal Quantiles

    Yuexiao Dong SAS WORKSHOP

    Introduction to SASUsing SAS for Windows

    SAS data handling: Data Step

    http://find/http://goback/
  • 7/31/2019 s as Workshop

    30/30

    SAS data handling: Data Step

    SAS Procedures

    Some references

    The Little SAS Book: A Primer, Third Edition (Paperback),by Lora D. Delwiche and Susan J. Slaughter.

    SAS Certification Prep Guide: Base Programming(Paperback), by SAS Institute.

    http://www.uc.edu/sashtml/stat/index.htm

    http://ftp.sas.com/samples/A59216

    Yuexiao Dong SAS WORKSHOP

    http://find/http://goback/