base sas training

Upload: moataz-deiab

Post on 14-Apr-2018

236 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/27/2019 Base SAS Training

    1/486

    Introduction toProgramming Concepts

    Using SAS

    Software

    Course Notes

  • 7/27/2019 Base SAS Training

    2/486

    2

    Chapter 1 Learning the Concepts of SASProgramming

    1.1 Introduction

    1.2 Explaining the Basic Functionality of SAS Software1.3 Identifying the File Types Used in SAS

    1.4 Identifying the Components of a SAS Program

    1.5 Chapter Summary

    1.6 Solutions to Exercises

    Chapter 2 Introducing the Course Scenarios

    2.1 Introduction

    2.2 Defining the Lecture Scenario2.3 Defining the Exercise Scenario

    http://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C03/BASIC_001%20Chapter%203.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C03/BASIC_001%20Chapter%203.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C02/BASIC_001%20Chapter%202.ppt
  • 7/27/2019 Base SAS Training

    3/486

    3

    Chapter 3 Reading a Raw Data File with the DATA Step

    3.1 Introduction

    3.2 Planning to Read a Raw Data File3.3 Coding to Read a Raw Data File

    3.4 Chapter Summary

    3.5 Solutions to Exercises

    Chapter 4 Navigating the SASWindowing Environment

    4.1 Introduction

    4.2 Entering and Executing SAS Code

    4.3 Editing SAS Code

    4.4 Saving and Retrieving SAS Code

    4.5 Additional Topics (Optional)

    4.6 Chapter Summary

    4.7 Solutions to Exercises

    http://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C04/BASIC_001%20Chapter%204.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C05/BASIC_001%20Chapter%205.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C04/BASIC_001%20Chapter%204.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C04/BASIC_001%20Chapter%204.ppt
  • 7/27/2019 Base SAS Training

    4/486

    4

    Chapter 5 Creating a List Report with a PROC Step

    5.1 Introduction

    5.2 Planning to Create a List Report5.3 Coding to Create a List Report

    5.4 Chapter Summary

    5.5 Solutions to Exercises

    Chapter 6 Creating a Variable with the DATA Step

    6.1 Introduction

    6.2 Planning to Create a Variable

    6.3 Coding to Create a Variable

    6.4 Chapter Summary

    6.5 Solutions to Exercises

    http://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C06/BASIC_001%20Chapter%206.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C06/BASIC_001%20Chapter%206.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C06/BASIC_001%20Chapter%206.ppt
  • 7/27/2019 Base SAS Training

    5/486

    5

    Chapter 7 Performing Conditional Logic with the DATA Step

    7.1 Introduction

    7.2 Planning to Perform Conditional Logic to Create a Variable7.3 Coding to Perform Conditional Logic to Create a Variable

    7.4 Chapter Summary

    7.5 Solutions to Exercises

    Chapter 8 Creating Statistical Reports with PROC Steps

    8.1 Introduction

    8.2 Planning to Create a Summary Report

    8.3 Coding to Create a Summary Report

    8.4 Planning to Create a Frequency Report

    8.5 Coding to Create a Frequency Report

    8.6 Chapter Summary

    8.7 Solutions to Exercises

    http://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C08/BASIC_001%20Chapter%208.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C09/BASIC_001%20Chapter%209.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C09/BASIC_001%20Chapter%209.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C09/BASIC_001%20Chapter%209.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C08/BASIC_001%20Chapter%208.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C08/BASIC_001%20Chapter%208.ppt
  • 7/27/2019 Base SAS Training

    6/486

    6

    Chapter 9 Additional Topics

    9.1 Creating a Permanent SAS Data Set

    9.2 Sorting the Observations in a SAS Data Set9.3 Enhancing a Report

    9.4 Displaying Selected Observations in a Report

    9.5 Reading Excel Spreadsheets

    9.6 Chapter Summary

    Chapter 10 Learning More

    10.1 Where Do I Go From Here?

    10.2 SAS Institute Resources

    http://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C10/BASIC_001%20Chapter%2010.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C11/BASIC_001%20Chapter%2011.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C11/BASIC_001%20Chapter%2011.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C11/BASIC_001%20Chapter%2011.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C10/BASIC_001%20Chapter%2010.ppthttp://localhost/var/www/apps/conversion/tmp/scratch_6/basic_001/C10/BASIC_001%20Chapter%2010.ppt
  • 7/27/2019 Base SAS Training

    7/486

    Chapter 1

    Learning the Concepts ofSASProgramming

  • 7/27/2019 Base SAS Training

    8/486

    Section 1.1

    Introduction

  • 7/27/2019 Base SAS Training

    9/4869

    Objectives Explain the basic functionality of SAS software.

    Identify the file types used in SAS.

    Identify the components of a SAS program.

  • 7/27/2019 Base SAS Training

    10/48610 ......

    What Is SAS?SAS is a collection of components that enable you to

    manage, manipulate, and examine your data.

    Base SAS

    Reportingand

    Graphics

    Analytical

    Visualizationand Discovery

    Data Accessand

    Management

    BusinessSolutions

    UserInterfaces

    ApplicationDevelopment

    WebEnablement

  • 7/27/2019 Base SAS Training

    11/486

    Section 1.2

    Explaining the Basic

    Functionality of SAS Software

  • 7/27/2019 Base SAS Training

    12/48612

    Objectives Explain the basic functionality of SAS software.

  • 7/27/2019 Base SAS Training

    13/48613 ......

    Basic Functionality

    Access

    Data

    Manage

    Present Analyze

  • 7/27/2019 Base SAS Training

    14/486

    Section 1.3

    Identifying the File Types

    Used in SAS

  • 7/27/2019 Base SAS Training

    15/48615

    Objectives Identify the file types used in SAS.

  • 7/27/2019 Base SAS Training

    16/48616

    Types of Files Used with SAS

    SAS

    Program

    FilesSAS

    DATASets

    Raw

    Data

    Files

  • 7/27/2019 Base SAS Training

    17/486

    17

    Raw Data Files

    are nonsoftware-specific files thatcontain records and fields

    can be created by a variety ofsoftware products

    can be read by a variety ofsoftware products

    consist of no special attributes,such as field headings, pagebreaks, or titles

    are not reports.

    Raw

    Data

    Files

  • 7/27/2019 Base SAS Training

    18/486

    18

    Raw Data Files

    OperatingEnvironment

    File Location and Name

    Windows C:\workshop\winsas\basic\survey.dat

    UNIX /users/edu##/survey.dat

    OS/390edu###.basic.sascode(survey)

    oredu###.basic.survey

  • 7/27/2019 Base SAS Training

    19/486

    19

    SAS Data Sets

    are files specific to SAS thatcontain variables andobservations

    can be created only by SAS

    can be read only by SAS consist of a descriptor portion

    and a data portion

    are temporary files as used

    throughout this course.

    SAS

    Data

    Sets

  • 7/27/2019 Base SAS Training

    20/486

    20

    SAS Data Sets

    Data ProcessingTerminology

    SASTerminology

    File SAS Data Set

    Record Observation

    Field Variable

  • 7/27/2019 Base SAS Training

    21/486

    21

    SAS Data Sets The descriptor portion

    contains attributeinformation about thedata in a SAS data set.

    The data portion contains

    the data values in theform of a rectangulartable made up ofobservations andvariables.

    Descriptor

    Portion

    Data

    Portion

  • 7/27/2019 Base SAS Training

    22/486

    22

    SAS Data SetsSAS data sets names:

    are 1 to 32 characters in length start with a letter (A through Z) or an underscore (_)

    continue with any combination of numbers, letters, orunderscores

    are not case sensitive.

  • 7/27/2019 Base SAS Training

    23/486

    23 ......

    SAS Data Sets

    Operating Environment SAS Data Set Name

    Windows survey

    UNIX survey

    OS/390 survey

    The naming convention is the sameregardless of the operating environment.

  • 7/27/2019 Base SAS Training

    24/486

    24 ......

    SAS Data Sets

    Operating Environment SAS Data Set Name

    Windows survey

    UNIX survey

    OS/390 survey

    The naming convention is the sameregardless of the operating environment.

    Temporary

    SAS Data Sets

  • 7/27/2019 Base SAS Training

    25/486

    25

    SAS Data Sets

    Operating Environment SAS Data Set Name

    Windows work.survey

    UNIX work.survey

    OS/390 work.survey

    Another convention for naming a temporary SAS data setis to precede the SAS data set name with the prefix WORK.

  • 7/27/2019 Base SAS Training

    26/486

    26

    SAS Program Files

    contain SAS program code

    can be saved and re-used.SAS

    Program

    Files

  • 7/27/2019 Base SAS Training

    27/486

    27

    SAS Program Files

    OperatingEnvironment File Location and Name

    Windows C:\workshop\winsas\basic\survey.sas

    UNIX /users/edu##/survey.sas

    OS/390edu###.basic.sascode(survey)

    or

    edu###.basic.survey

  • 7/27/2019 Base SAS Training

    28/486

    28

    This exercise reinforces the concepts discussed

    previously.

    Exercises

  • 7/27/2019 Base SAS Training

    29/486

    29

    Identifying File Types Used in SAS Draw lines connecting the appropriate boxes for 1a.

    Mark each statement True or False for 1b. Name the two portions of a SAS data set for 1c.

  • 7/27/2019 Base SAS Training

    30/486

    30 ......

    Raw Data File

    SAS Data Set

    SAS ProgramFile

    File that containsSAS program code

    File that containsSAS-specific data

    File that containsnon-software-specific data

    Identifying File Types Used in SAS

    SAS ProgramFile

    File that containsSAS program code

    File that contains data specificto SAS

    File that containsnonsoftware-specific data

    Raw Data File

    SAS Data Set

  • 7/27/2019 Base SAS Training

    31/486

    31 ......

    Identifying File Types Used in SAS

    _______ Raw data files are only created by SAS.

    _______ SAS data sets are only created by SAS.

    _______ A SAS program file can be saved and reused.

    _______ A raw data file contains variables

    and observations.

    False

    True

    True

    False

  • 7/27/2019 Base SAS Training

    32/486

    32 ......

    Identifying File Types Used in SAS

    _______ A SAS data set contains variablesand observations.

    _______ A SAS data set name can be up to

    64 characters in length.

    _______ A SAS data set name can contain a

    dollar sign ($).

    _______ Raw data files are reports.

    True

    False

    False

    False

  • 7/27/2019 Base SAS Training

    33/486

    33 ......

    Identifying File Types Used in SAS

    _______ The prefix TEMP. can be added tothe beginning of a temporary SASdata set name.

    _______ The naming convention for a SASprogram file is the same regardlessof the operating environment.

    False

    False

  • 7/27/2019 Base SAS Training

    34/486

    34 ......

    Identifying File Types Used in SAS

    Name the two portions of a SAS data set.

    Descriptor Portion

    Data Portion

  • 7/27/2019 Base SAS Training

    35/486

    Section 1.4

    Identifying the Components of

    a SAS Program

  • 7/27/2019 Base SAS Training

    36/486

    36

    Objectives Identify the components of a SAS program.

  • 7/27/2019 Base SAS Training

    37/486

    37

    Components of a SAS ProgramA SAS program is a sequence of steps.

    There are only two kinds of steps: DATA steps

    PROC steps. A SAS Program

    PROC

    step(s)

    DATA

    step(s)

  • 7/27/2019 Base SAS Training

    38/486

    38 ......

    DATA Step(s)Typically, DATA steps read data, such as raw data files,

    to create SAS data sets.

    Raw Data FileSAS Data Set

    Descriptor

    DATA

    Step

  • 7/27/2019 Base SAS Training

    39/486

    39 ......

    DATA Step(s)In addition, DATA steps can modify existing variables or

    create new variables as necessary.

    Raw Data File SAS Data SetDescriptor

    DATA

    Step

  • 7/27/2019 Base SAS Training

    40/486

    40 ......

    PROC Step(s)PROC steps typically read SAS data sets to create

    reports.

    SAS Data SetDescriptor

    Report

    PROC

    Step

  • 7/27/2019 Base SAS Training

    41/486

    41 ......

    PROC Step(s)There are many different types of PROC steps.

    MEANS

    PRINT

    FREQ

    . . .

    PROC

    Step(s)

  • 7/27/2019 Base SAS Training

    42/486

    42

    Components of a StepA SAS program is a sequence of steps:

    DATA steps PROC steps.

    A step is a sequence of one or more statements.

  • 7/27/2019 Base SAS Training

    43/486

    43

    Components of a StepA statement usually starts with a keyword and always

    ends in a semicolon (;).

    KEYWORD. . . ;

  • 7/27/2019 Base SAS Training

    44/486

    44 ......

    Components of a DATA StepA DATA step starts with a DATA statement and ends with

    a RUN statement.

    data _______________ ;

    _______________ ;

    . . .

    _______________ ;run;

    Start

    End

  • 7/27/2019 Base SAS Training

    45/486

    45 ......

    Components of a PROC StepA PROC step starts with a PROC statement and ends

    with a RUN statement.

    Start

    End

    proc _______________ ;

    _______________ ;

    . . .

    _______________ ;run;

  • 7/27/2019 Base SAS Training

    46/486

    46

    This exercise reinforces the concepts discussed

    previously.

    Exercises

  • 7/27/2019 Base SAS Training

    47/486

    47

    Identifying the Components of aSAS Program Circle the appropriate answer(s) for 2.a through 2.i. Fill in the blanks for 2.j.

  • 7/27/2019 Base SAS Training

    48/486

    48 ......

    Identifying the Components of aSAS Program

    a. What are the two kinds of steps in a SAS program?

    1) DATA

    2) RUN

    3) Statement

    4) PROC

    5) Data Set

  • 7/27/2019 Base SAS Training

    49/486

    49 ......

    Identifying the Components of aSAS Program

    b. Will all SAS programs contain both a DATA step anda PROC step ?

    1) Yes

    2) No

    c. A DATA step typically reads what type of file?

    1) Raw Data File

    2) Program File3) Report

  • 7/27/2019 Base SAS Training

    50/486

    50 ......

    Identifying the Components of aSAS Program

    d. What does a DATA step typically create?1) Raw Data File

    2) Program File

    3) SAS Data Set

    4) Report

  • 7/27/2019 Base SAS Training

    51/486

    51 ......

    Identifying the Components of aSAS Program

    e. A PROC step typically reads what type of file?

    1) Raw Data File

    2) Program File

    3) SAS Data Set

    4) Report

  • 7/27/2019 Base SAS Training

    52/486

    52 ......

    Identifying the Components of aSAS Program

    f. What does a PROC step typically create?

    1) Raw Data File

    2) Program File

    3) SAS Data Set

    4) Report

  • 7/27/2019 Base SAS Training

    53/486

    53 ......

    Identifying the Components of aSAS Program

    g. A step is a sequence of what?1) Files

    2) Statements

    3) RUNs

    4) Data Sets

  • 7/27/2019 Base SAS Training

    54/486

    54 ......

    Identifying the Components of aSAS Program

    h. What does a statement usually start with?1) A Colon

    2) A Keyword

    3) RUN

    4) PROC

  • 7/27/2019 Base SAS Training

    55/486

    55 ......

    Identifying the Components of aSAS Program

    i. What does a statement end with?1) A Colon

    2) A Period

    3) A Comma

    4) A Semicolon

  • 7/27/2019 Base SAS Training

    56/486

    56

    Identifying the Components of aSAS Program

    j. A DATA step starts with a ______ statement andends with a _____ statement.

    A PROC step starts with a ______ statement andends with a _____ statement.

    DATARUN

    PROC

    RUN

    ...

  • 7/27/2019 Base SAS Training

    57/486

    Chapter 2

    Introducing the

    Course Scenarios

  • 7/27/2019 Base SAS Training

    58/486

    Section 2.1

    Introduction

  • 7/27/2019 Base SAS Training

    59/486

    59

    Objectives Define the business need for the lecture scenario.

    Outline the programming requirements for the lecturescenario.

    Define the business need for the exercise scenario.

    Outline the programming requirements for the exercise

    scenario.

  • 7/27/2019 Base SAS Training

    60/486

    60 ......

    Course Scenarios

    International Airlines is afictitious airline that transportspeople and cargo from city tocity around the world.

  • 7/27/2019 Base SAS Training

    61/486

    61

    Course ScenariosThe management of International Airlines has decided to

    give the pilots a bonus and a salary increase.Also, the management has decided to increase the cargorates and the passenger fares for all flights.

  • 7/27/2019 Base SAS Training

    62/486

    62

    Course Scenarios

    The lecture portion of thiscourse will address the scenario

    of the pilots receiving a bonusand a salary increase.

    C S

  • 7/27/2019 Base SAS Training

    63/486

    63

    Course Scenarios

    The exercise portion of

    this course will addressthe scenario of theincrease in cargo ratesand passenger fares for

    all flights.

    O tli th P i R i t

  • 7/27/2019 Base SAS Training

    64/486

    64 ......

    Outline the Programming Requirements

    Input

    Report/File

    Output

    Processing

  • 7/27/2019 Base SAS Training

    65/486

    Section 2.2

    Defining the

    Lecture Scenario

    Obj ti

  • 7/27/2019 Base SAS Training

    66/486

    66

    Objectives Define the business need for the lecture scenario.

    Outline the programming requirements for the lecturescenario.

    D fi th B i N d

  • 7/27/2019 Base SAS Training

    67/486

    67 ......

    Define the Business NeedAll the pilots of International Airlines will receive a bonus

    and a salary increase.

    Each pilots bonus will be equal to

    10% of the pilots current salary.

    Each pilots salary will increase as

    follows: 5% if a level-one pilot

    7% if a level-two pilot 9% if a level-three pilot.

    D fi th B i N d

  • 7/27/2019 Base SAS Training

    68/486

    68

    Define the Business NeedThe management of International Airlines needs three

    reports.

    Report #1 Report #2 Report #3

  • 7/27/2019 Base SAS Training

    69/486

    69

    Report #1

    A listing of all the pilotswith their expectedbonuses.

  • 7/27/2019 Base SAS Training

    70/486

    70

    EmployeeObs ID FirstName LastName JobCode Bonus1 E01046 DAVID CHAPMAN PILOT1 72662 E01682 VICTOR TAILOR PILOT1 44983 E02659 CLIFTON G. WILDER PILOT1 53634 E04042 SAMUEL BENNETT PILOT1 52875 E04732 CHRISTIAN EDMINSTON PILOT1 76126 E03740 CRAIG N. SAWYER PILOT1 62287 E03389 LOUISE STAINES PILOT1 74398 E04688 JOHN D. PERRY PILOT1 67689 E01702 ROBERTA J. CHADWICK PILOT1 622810 E02391 DONALD E TAYLOR PILOT1 449811 E01642 NANCY A. MCELOY PILOT2 782612 E04348 CAROLYN P. CARTER PILOT2 746213 E03875 PAUL J. GLENNON PILOT2 746214 E03587 YIQUN SANTIAGO PILOT2 782615 E03739 WILLIAM J. MCKENZIE PILOT2 746216 E03768 JAN STANDER PILOT2 8190

    Partial example of the desired report:

  • 7/27/2019 Base SAS Training

    71/486

    71

    Report #2

    A comparison between

    the average current salaryand the average newsalary for the domesticand international pilots.

  • 7/27/2019 Base SAS Training

    72/486

    72

    The MEANS ProcedureNCategory Obs Variable N Mean Std Dev Minimum Maximum----------------------------------------------------------------------------DOM 19 Salary 19 68405.79 11439.29 44980.00 79760.00NewSalary 19 72550.12 12562.02 47229.00 85342.20

    INT 31 Salary 31 99252.55 21019.22 67340.00 125250.00NewSalary 31 107365.26 23700.21 72052.80 136522.50----------------------------------------------------------------------------

    An example of the desired report:

  • 7/27/2019 Base SAS Training

    73/486

    73

    Report #3

    A breakdown ofthe percentage ofdomestic pilotsversus internationalpilots.

  • 7/27/2019 Base SAS Training

    74/486

    74

    The FREQ ProcedureCumulative CumulativeCategory Frequency Percent Frequency Percent-------------------------------------------------------------DOM 19 38.00 19 38.00

    INT 31 62.00 50 100.00

    An example of the desired report:

    Outline the Programming Requirements

  • 7/27/2019 Base SAS Training

    75/486

    75 ......

    Outline the Programming Requirements

    Input

    Report/File

    Output

    Processing

    Output

  • 7/27/2019 Base SAS Training

    76/486

    76

    OutputThe management of International Airlines needs three

    reports.

    Report #1 Report #2 Report #3

    Input

  • 7/27/2019 Base SAS Training

    77/486

    77

    InputPartial listing of PILOT raw data file:

    E01046DAVID CHAPMAN PILOT1 72660DOM

    E01682VICTOR TAILOR PILOT1 44980DOM

    E02659CLIFTON G. WILDER PILOT1 53630DOM

    E04042SAMUEL BENNETT PILOT1 52870DOME04732CHRISTIAN EDMINSTON PILOT1 76120DOM

    . . . . . .

    . . . . . .

    Input

  • 7/27/2019 Base SAS Training

    78/486

    78

    InputThe raw data file PILOT is available and contains the

    following information: Employee ID (starts with the letter E and contains five

    numbers)

    First Name

    Last Name Job Code (PILOT1, PILOT2, or PILOT3)

    Salary

    Category of Pilot (DOM for Domestic or INT for

    International).

    Processing

  • 7/27/2019 Base SAS Training

    79/486

    79 ......

    ProcessingTwo new variables need to be created.

    EmployeeID

    FirstName

    LastName

    JobCode Bonus

    NewSalarySalary

    PILOT raw data file Create

    Category

    Outline the Programming Requirements

  • 7/27/2019 Base SAS Training

    80/486

    80

    Outline the Programming RequirementsChapter 4: Read PILOT Raw Data File to Create

    SAS Data Set (DATA Step)

    Chapter 5: Navigate the SAS Windowing Environment

    Chapter 6: Create a List Report of SAS Data Set(PROC Step)

    Chapter 7: Modify SAS Data Set to Include

    Bonus Variable (DATA Step)Chapter 8: Modify SAS Data Set to Include New

    Salary Variable (DATA Step)

    Chapter 9: Create Statistical Reports of SAS

    Data Set (PROC Step)

  • 7/27/2019 Base SAS Training

    81/486

    Section 2.3

    Defining the

    Exercise Scenario

    Objectives

  • 7/27/2019 Base SAS Training

    82/486

    82

    Objectives Define the business need for the exercise scenario.

    Outline the programming requirements for the exercisescenario.

    Define the Business Need

  • 7/27/2019 Base SAS Training

    83/486

    83 ......

    Define the Business NeedCargo rates and passenger fares at International Airlines

    will increase on all flights.

    The cargo rates will increase 50cents per pound on all flights.

    The passenger fares will increaseas follows: 8% if a short-range flight

    10% if a medium-range flight 12% if a long-range flight.

    Define the Business Need

  • 7/27/2019 Base SAS Training

    84/486

    84

    Define the Business NeedThe management of International Airlines needs three

    reports.

    Report #1 Report #2 Report #3

  • 7/27/2019 Base SAS Training

    85/486

    85

    Report #1

    A listing of all theflights with the newcargo rate.

  • 7/27/2019 Base SAS Training

    86/486

    86

    NewCargoObs Origination Destination Range Category Rate1 FRA CPT LONG INT 5.772 CPT FRA LONG INT 5.773 RDU FRA LONG INT 4.364 SYD HKG LONG INT 4.655 SFO HND LONG INT 5.15

    6 LHR JNB LONG INT 5.607 JNB LHR LONG INT 5.608 FRA RDU LONG INT 4.369 HND SFO LONG INT 5.1510 HKG SYD LONG INT 4.6511 RDU ANC MEDIUM DOM 2.6512 SFO ANC MEDIUM DOM 2.3213 PEK CCU MEDIUM INT 2.3314 JRS DEL MEDIUM INT 2.7615 FRA DXB MEDIUM INT 2.22

    Partial example of the desired report:

  • 7/27/2019 Base SAS Training

    87/486

    87

    Report #2

    A comparison betweenthe average current

    passenger fare and theaverage new passengerfare for the domestic andinternational flights.

  • 7/27/2019 Base SAS Training

    88/486

    88

    The MEANS ProcedureNCategory Obs Variable N Mean Std Dev Minimum Maximum-----------------------------------------------------------------------------DOM 48 PassengerFare 48 219.3333 179.4964 30.0000 682.0000NewPassengerFare 48 239.6767 198.2444 32.4000 750.2000

    INT 64 PassengerFare 64 375.5000 336.1152 40.0000 1142.0000NewPassengerFare 64 414.0119 376.7187 42.2000 1279.0400-----------------------------------------------------------------------------

    An example of the desired report:

  • 7/27/2019 Base SAS Training

    89/486

    89

    Report #3

    A breakdown of the

    percentage of domesticflights versusinternational flights.

  • 7/27/2019 Base SAS Training

    90/486

    90

    The FREQ ProcedureCumulative CumulativeCategory Frequency Percent Frequency Percent-------------------------------------------------------------DOM 48 42.86 48 42.86

    INT 64 57.14 112 100.00

    An example of the desired report:

    Outline the Programming Requirements

  • 7/27/2019 Base SAS Training

    91/486

    91 ......

    Outline the Programming Requirements

    Input

    Report/File

    Output

    Processing

    Output

  • 7/27/2019 Base SAS Training

    92/486

    92

    OutputThe management of International Airlines needs three

    reports.

    Report #1 Report #2 Report #3

    Input

  • 7/27/2019 Base SAS Training

    93/486

    93

    InputPartial listing of RATES raw data file:

    FRA CPT LONG 5.27 1142.00 INT

    CPT FRA LONG 5.27 1142.00 INT

    RDU FRA LONG 2.86 836.00 INT

    SYD HKG LONG 4.15 898.00 INTSFO HND LONG 4.65 1007.00 INT

    . . . . . .

    . . . . . .

    Input

  • 7/27/2019 Base SAS Training

    94/486

    94

    InputThe raw data file RATES is available and contains the

    following information: Origination (three-letter code)

    Destination (three-letter code)

    Range of Flight (SHORT, MEDIUM, or LONG)

    Cargo Rate (price per pound) Passenger Fare (price per passenger)

    Category of Flight (DOM for Domestic or INT forInternational).

    Processing

  • 7/27/2019 Base SAS Training

    95/486

    95 ......

    ProcessingTwo new variables need to be created.

    Origi-nation

    Desti-nation

    RangeCargoRate

    RATES raw data file Create

    PassengerFare

    Category

    NewCargoRate

    NewPassenger

    Fare

    Outline the Programming Requirements

  • 7/27/2019 Base SAS Training

    96/486

    96

    Outline the Programming RequirementsChapter 4: Read RATES Raw Data File to Create

    SAS Data Set (DATA Step)

    Chapter 5: Navigate the SAS Windowing Environment

    Chapter 6: Create a List Report of SAS Data Set(PROC Step)

    Chapter 7: Modify SAS Data Set to Include

    New Cargo Rate Variable (DATA Step)Chapter 8: Modify SAS Data Set to Include New

    Passenger Fare Variable (DATA Step)

    Chapter 9: Create Statistical Reports of SAS

    Data Set (PROC Step)

  • 7/27/2019 Base SAS Training

    97/486

    Chapter 3

    Reading a Raw Data File with

    the DATA Step

  • 7/27/2019 Base SAS Training

    98/486

    Section 3.1

    Introduction

    Objectives

  • 7/27/2019 Base SAS Training

    99/486

    99

    Objectives Plan to read a raw data file to create a SAS data set.

    Code to read a raw data file to create a SAS data set.

    Why Create a SAS Data Set?

  • 7/27/2019 Base SAS Training

    100/486

    100 ...

    Why Create a SAS Data Set?To create a report, the data must be in the form of aSAS data set.

    Why Use a DATA Step?

  • 7/27/2019 Base SAS Training

    101/486

    101 ...

    Why Use a DATA Step?

    Raw Data FileSAS Data Set

    Descriptor

    DATA

    Step

  • 7/27/2019 Base SAS Training

    102/486

    Section 3.2

    Planning to Read a

    Raw Data File

    Objectives

  • 7/27/2019 Base SAS Training

    103/486

    103

    j Plan to read a raw data file to create a SAS data set.

    Requirements to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    104/486

    104

    q Name the output SAS data set.

    Locate and name the input raw data file. Examine the raw data file and file layout.

    Describe the fields in the raw data file.

    Determine the location of fields.

    Give SAS variable names to fields. Determine the variable type (character or numeric).

    Name the Output SAS Data Set

  • 7/27/2019 Base SAS Training

    105/486

    105

    pSAS data sets names:

    are 1 to 32 characters in length start with a letter (A through Z) or an underscore (_)

    continue with any combination of numbers, letters, orunderscores

    are not case sensitive.

    Name the Output SAS Data Set

  • 7/27/2019 Base SAS Training

    106/486

    106

    p

    Valid Data Set

    NamesInvalid Data Set Names

    monthly_totals monthly totals

    _1980_Sales_Data 1980_Sales_Data

    R_and_D R&D

    DollarAmount $Amount

    Name the Output SAS Data Set

  • 7/27/2019 Base SAS Training

    107/486

    107 ...

    p

    OperatingEnvironment Data SetName

    Windows

    UNIX

    OS/390

    pilotdata

    pilotdata

    pilotdata

    Locate and Name the Input Raw Data File

  • 7/27/2019 Base SAS Training

    108/486

    108

    p

    Operating

    Environment File Location and Name

    Windows C:\workshop\winsas\basic\pilot.dat

    UNIX /users/edu##/pilot.dat

    OS/390 edu###.basic.sascode(pilot)

    Examine the Raw Data File and File Layout

  • 7/27/2019 Base SAS Training

    109/486

    109

    yPartial listing of PILOT raw data file:

    ----5----10---15---20---25---30---35---40---45---50

    E01046DAVID CHAPMAN PILOT1 72660DOM

    E01682VICTOR TAILOR PILOT1 44980DOM

    E02659CLIFTON G. WILDER PILOT1 53630DOM

    E04042SAMUEL BENNETT PILOT1 52870DOM

    E04732CHRISTIAN EDMINSTON PILOT1 76120DOM

    . . . . ..

    . . . . ..

    . . . . ..

    Examine Raw Data File and File Layout

  • 7/27/2019 Base SAS Training

    110/486

    110

    yFile Layout of PILOT raw data file:

    Field Name Field Location

    Employee ID 1 - 6

    First Name 7 - 19

    Last Name 20 - 34

    Job Code 35 - 41

    Salary 42 - 47

    Category 48 - 50

    Determine Location of Fields

  • 7/27/2019 Base SAS Training

    111/486

    111 ...

    Partial listing of PILOT raw data file:

    ----5----10---15---20---25---30---35---40---45---50

    E01046DAVID CHAPMAN PILOT1 72660DOM

    E01682VICTOR TAILOR PILOT1 44980DOM

    E02659CLIFTON G. WILDER PILOT1 53630DOM

    E04042SAMUEL BENNETT PILOT1 52870DOM

    E04732CHRISTIAN EDMINSTON PILOT1 76120DOM

    Employee ID

    First Name

    Last Name

    Job Code

    Salary

    Category

    Determine Location of Fields

  • 7/27/2019 Base SAS Training

    112/486

    112 ...

    Field NameField

    Location

    Start

    Position

    End

    Position

    Employee ID 1 - 6

    First Name 7 - 19

    Last Name 20 - 34

    Job Code 35 - 41

    Salary 42 - 47

    Category 48 - 50

    1 6

    7 19

    20 34

    35 41

    42 4748 50

    Give SAS Variable Names to Fields

  • 7/27/2019 Base SAS Training

    113/486

    113

    Based on examining the raw data file layout,

    determine the fields to be included in the SAS data set provide a meaningful SAS variable name for each field.

    Give SAS Variable Names to Fields

  • 7/27/2019 Base SAS Training

    114/486

    114

    SAS variable names:

    are 1 to 32 characters in length start with a letter (A through Z) or an underscore (_)

    continue with any combination of numbers, letters, orunderscores

    are not case sensitive must be unique within a SAS data set.

    Give SAS Variable Names to Fields

  • 7/27/2019 Base SAS Training

    115/486

    115

    Valid Variable

    Names

    Invalid Variable Names

    social_security_number social security number

    _2005_YearlyReturns 2005_YearlyReturns

    day5 day#5

    PercentIncrease %Increase

    Give SAS Variable Names to Fields

  • 7/27/2019 Base SAS Training

    116/486

    116 ...

    Field Name SAS Variable Name

    Employee ID

    First Name

    Last Name

    Job Code

    Salary

    Category

    EmployeeID

    FirstName

    LastName

    JobCode

    SalaryCategory

    Determine Variable Type

  • 7/27/2019 Base SAS Training

    117/486

    117

    Based on examining the raw data file or the file layout,determine the variable type.

    A SAS variable can be one of two types:

    character

    numeric.

    Determine Variable Type

  • 7/27/2019 Base SAS Training

    118/486

    118

    A variable is considered to be character if it contains anycombination of the following:

    letters (A - Z, a - z)

    numbers (0-9)

    special characters (!, @, #, %, and so on).

    Determine Variable Type

  • 7/27/2019 Base SAS Training

    119/486

    119

    Examples of valid character variable values are

    Ms. Helen Jones 648 Pine St.

    Research & Development

    $21,756

    389.

    Determine Variable Type

  • 7/27/2019 Base SAS Training

    120/486

    120

    A variable is considered to be numeric if it contains

    numbers (0-9).It may also contain

    a decimal point (.)

    a negative sign (-)

    a letter E to indicate scientific notation.

    Determine Variable Type

  • 7/27/2019 Base SAS Training

    121/486

    121

    Examples of valid numeric variable values are

    175 4856281

    -30

    29.92

    2.1E6.

    Determine Variable Type

  • 7/27/2019 Base SAS Training

    122/486

    122

    Partial listing of PILOT raw data file:

    ----5----10---15---20---25---30---35---40---45---50

    E01046DAVID CHAPMAN PILOT1 72660DOM

    E01682VICTOR TAILOR PILOT1 44980DOM

    E02659CLIFTON G. WILDER PILOT1 53630DOM

    E04042SAMUEL BENNETT PILOT1 52870DOM

    E04732CHRISTIAN EDMINSTON PILOT1 76120DOM

    . . . . ..

    . . . . ..

    . . . . ..

    Determine Variable Type

  • 7/27/2019 Base SAS Training

    123/486

    123

    Field Name Field Location Variable Type

    Employee ID 1 6

    First Name 7 - 19

    Last Name 20 - 34

    Job Code 35 - 41

    Salary 42 - 47

    Category 48 - 50

    Character

    Character

    Character

    Character

    Character

    Numeric

  • 7/27/2019 Base SAS Training

    124/486

    124

    This exercise reinforces the concepts discussedpreviously.

    Exercises

    Planning to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    125/486

    125

    Based on the Exercise Scenario, plan to read the rawdata file. Refer to the Exercise Scenario Handout fordetails on the raw data file.

    Name the output SAS data set.

    Locate and name the input raw data file.

    Give SAS variable names to fields. Determine location of fields.

    Determine variable type (character or numeric).

    Planning to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    126/486

    126 ...

    Name the output SAS data set.

    Windows

    UNIX

    OS/390

    ratedata

    ratedata

    ratedata

    Planning to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    127/486

    127 ...

    Name the input raw data file, including the full path namebased on your operating environment.

    Windows

    UNIX

    OS/390

    C:\workshop\winsas\basic\rates.dat

    /users/edu##/rates.dat

    edu###.basic.sascode(rates)

    Planning to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    128/486

    128 ...

    Field Name Variable Name

    Origination

    Destination

    Flight Range

    Cargo Rate

    Passenger Fare

    Flight Category

    Origination

    Destination

    Range

    CargoRate

    PassengerFare

    Category

    Planning to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    129/486

    129 ...

    Field Name Start Position End Position

    Origination

    Destination

    Flight Range

    Cargo Rate

    Passenger Fare

    Flight Category

    1

    6

    12

    20

    28

    38

    3

    8

    17

    23

    34

    40

    Planning to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    130/486

    130 ...

    Field Name Variable Type

    Origination

    Destination

    Flight Range

    Cargo Rate

    Passenger FareFlight Category

    Character

    Character

    Character

    Numeric

    NumericCharacter

  • 7/27/2019 Base SAS Training

    131/486

    Section 3.3

    Coding to Read a

    Raw Data File

    Objectives

  • 7/27/2019 Base SAS Training

    132/486

    132

    Write program code to read a raw data file and createa SAS data set.

    Purpose of the DATA Step

  • 7/27/2019 Base SAS Training

    133/486

    133

    The DATA step

    names the SAS data set being created identifies the raw data file

    describes the fields in the raw data file

    reads a record from the raw data file

    processes the record writes the processed record to the SAS data set as a

    new observation.

    The DATA Step

  • 7/27/2019 Base SAS Training

    134/486

    134 ...

    data _______________ ;_______________ ;

    . . ._______________ ;run;

    Start

    End

    The DATA Step

  • 7/27/2019 Base SAS Training

    135/486

    135

    data _______________ ;infile ________ ;

    input _________ ;. . .run;

    The DATA StatementTh DATA t t t th SAS d t t b i

  • 7/27/2019 Base SAS Training

    136/486

    136

    The DATA statement names the SAS data set beingcreated and signals the beginning of the DATA step.

    General form of the DATA statement:

    Example:

    DATA SAS_data_set_name;

    data pilotdata;

    The INFILE StatementTh INFILE t t t th d t fil t b d

  • 7/27/2019 Base SAS Training

    137/486

    137

    The INFILE statement names the raw data file to be read.

    General form of the INFILE statement:

    INFILE 'input-raw-data-file';

    The INFILE StatementE l

  • 7/27/2019 Base SAS Training

    138/486

    138

    Example:

    OperatingEnvironment

    INFILE Statement

    Windows infile 'C:\workshop\winsas\basic\pilot.dat';

    UNIX infile '/users/edu##/pilot.dat';

    OS/390 infile 'edu###.basic.sascode(pilot)';

    The INPUT StatementTh INPUT t t t d ib th fi ld i h

  • 7/27/2019 Base SAS Training

    139/486

    139

    The INPUT statement describes the fields in each rawdata record to SAS.

    Each variable defined in the INPUT statement

    provides a name to represent the field

    indicates a type of character or numeric

    indicates the starting and ending position.

    The INPUT StatementG l f f th INPUT t t t

  • 7/27/2019 Base SAS Training

    140/486

    140

    General form of the INPUT statement:

    variable is a valid SAS variable name.

    $ indicates a character variable.start identifies the starting position.

    end identifies the ending position.

    INPUTvariable $ start - end . . . ;

    The INPUT StatementE l

  • 7/27/2019 Base SAS Training

    141/486

    141

    Example:

    input EmployeeID $ 1 - 6FirstName $ 7 - 19LastName $ 20 - 34JobCode $ 35 - 41Salary 42 - 47

    Category $ 48 - 50;

    The INPUT StatementThis a of describing the inp t ra data record to SAS is

  • 7/27/2019 Base SAS Training

    142/486

    142

    This way of describing the input raw data record to SAS iscalled column input because it defines the starting and

    ending positions of each field.

    This implies that each field in a raw data record is in thesame position in every record of the file.

    The RUN StatementThe RUN statement signals the end of a step

  • 7/27/2019 Base SAS Training

    143/486

    143

    The RUN statement signals the end of a step.

    General form of the RUN statement:

    RUN;

    The DATA StepGeneral form for the complete DATA step:

  • 7/27/2019 Base SAS Training

    144/486

    144

    General form for the complete DATA step:

    DATASAS_data_set_name;INFILE 'input-raw-data-file';

    INPUTvariable $ start - end. . .;

    RUN;

    The DATA Step - WindowsBased on the International Airlines lecture scenario the

  • 7/27/2019 Base SAS Training

    145/486

    145

    Based on the International Airlines lecture scenario, thecode for the complete DATA step is

    data pilotdata;infile 'c:\workshop\winsas\basic\pilot.dat';input EmployeeID $ 1 - 6

    FirstName $ 7 - 19

    LastName $ 20 - 34JobCode $ 35 - 41Salary 42 - 47Category $ 48 - 50;

    run;

  • 7/27/2019 Base SAS Training

    146/486

    146

    This exercise reinforces the concepts discussedpreviously.

    Exercises

    Coding to Read a Raw Data FileDraw lines connecting the appropriate boxes

  • 7/27/2019 Base SAS Training

    147/486

    147

    Draw lines connecting the appropriate boxes.

    Based on your previous Planning to Read a Raw DataFile exercise,

    Write the DATA statement.

    Write the INFILE statement.

    Write the INPUT statement. Write the RUN statement.

    Coding to Read a Raw Data File

  • 7/27/2019 Base SAS Training

    148/486

    148 ...

    1st Statement

    2nd Statement

    3rd Statement

    INFILE

    RUN

    DATA

    Signals end of theDATA step.

    Describes fields ineach record.

    Names raw data fileto be read.

    4th Statement INPUTNames data setbeing created.

    1st Statement

    2nd Statement

    3rd Statement

    4th Statement

    INFILE

    RUN

    DATA

    INPUT

    Signals end of theDATA step.

    Describes fields ineach record.

    Names raw data fileto be read.

    Names data setbeing created.

    Coding to Read a Raw Data FileWindows Solution:

  • 7/27/2019 Base SAS Training

    149/486

    149

    Windows Solution:

    data ratedata;infile 'c:\workshop\winsas\basic\rates.dat';input Origination $ 1 - 3

    Destination $ 6 - 8Range $ 12 - 17

    CargoRate 20 - 23PassengerFare 28 - 34Category $ 38 - 40;

    run;

  • 7/27/2019 Base SAS Training

    150/486

    Chapter 4

    Navigating the SAS

    Windowing Environment

  • 7/27/2019 Base SAS Training

    151/486

    Section 4.1

    Introduction

    Objectives Enter SAS program code in the SAS windowing

  • 7/27/2019 Base SAS Training

    152/486

    152

    Enter SAS program code in the SAS windowingenvironment and execute the program.

    Modify SAS program code and execute the program.

    Save and retrieve a SAS program.

  • 7/27/2019 Base SAS Training

    153/486

    Section 4.2

    Entering and Executing SAS

    Code

    Objectives Enter SAS program code in the SAS windowing

  • 7/27/2019 Base SAS Training

    154/486

    154

    Enter SAS program code in the SAS windowingenvironment and execute the program.

    Enter the SAS Program CodeAfter the planning and

  • 7/27/2019 Base SAS Training

    155/486

    155

    After the planning andcoding effort for a SAS

    program is complete, theSAS program code mustbe entered into thecomputer to process and

    to test the program.

    Program Execution ModeSAS programs can be processed in one of two modes:

  • 7/27/2019 Base SAS Training

    156/486

    156

    SAS programs can be processed in one of two modes:

    windowing

    batch.

    Windowing ModeWindowing mode is a facility that enables you

  • 7/27/2019 Base SAS Training

    157/486

    157

    Windowingmode is a facility that enables youto enter and execute SAS programs and view

    the results in an interactive environment.

    An interactive environmentpermits the program to beprocessed immediately when submitted for execution.

    Batch ModeBatch mode is used to run SAS programs in the

  • 7/27/2019 Base SAS Training

    158/486

    158

    Batch mode is used to run SAS programs in thebackground by submitting them to the operating system

    for batch execution.Batch execution enables the program to be processedwhen the computer has resources available, notnecessarily at the moment when submitted for execution.

    The SAS Windowing EnvironmentSAS software provides the SAS

  • 7/27/2019 Base SAS Training

    159/486

    159

    SAS software provides the SASwindowing environment, an

    interactive environment thatenables the entry and executionof SAS program code.

    The SAS Windowing Environment

  • 7/27/2019 Base SAS Training

    160/486

    160

    The SAS windowing environment is madeup of a collection of windows.

    There are three primary windows in the

    windowing environment.

    3 2 1

    The Program Editor Window

  • 7/27/2019 Base SAS Training

    161/486

    161

    The Program Editor windowenables SASprogram code to be

    entered from the keyboard

    submitted for execution.

    The Log Window

  • 7/27/2019 Base SAS Training

    162/486

    162

    The Log windowdisplays

    the SAS program code submitted forexecution

    messages from SAS indicating thestatus of the program execution.

    The Output Window

  • 7/27/2019 Base SAS Training

    163/486

    163

    The Output windowdisplays reports

    generated by the SAS program.

    Commands

  • 7/27/2019 Base SAS Training

    164/486

    164

    Commands are used to navigateamong the various windows of theSAS windowing environment andare used to execute a program.

    Depending upon the operatingenvironment, commands can be

    issued by...

    continued...

    Selecting from pull-down menus

  • 7/27/2019 Base SAS Training

    165/486

    165

    Typing the command Clicking on a tool button

    Using function keys (F1 - F12)

    Entering and

  • 7/27/2019 Base SAS Training

    166/486

    166

    This demonstration illustrates entering

    SAS program code into the SAS windowingenvironment and executing the program.

    Entering and

    Executing SAS Code

  • 7/27/2019 Base SAS Training

    167/486

    Section 4.3

    Editing SAS Code

    Objectives Modify SAS program code and execute the program.

  • 7/27/2019 Base SAS Training

    168/486

    168

    Editing SAS CodeAfter the SAS program code is entered into the Program

  • 7/27/2019 Base SAS Training

    169/486

    169

    Editor window and executed, there might be a need to

    modify, oredit, the code because of program errors

    program specification changes

    the need to add additional code.

  • 7/27/2019 Base SAS Training

    170/486

    Program Specification ChangesThe purpose or function of a program may change due to

  • 7/27/2019 Base SAS Training

    171/486

    171

    insufficient analysis

    project evolution

    user changes.

    The Need to Add Additional CodeA program can be written in stages. Programmers often

  • 7/27/2019 Base SAS Training

    172/486

    172

    test their code in stages. They know that they will add

    statements or steps to the program after each stageworks correctly.

    Debugging and Editing SAS CodeDebugging and editing program code is an iterative

  • 7/27/2019 Base SAS Training

    173/486

    173

    process.

    Code

    Modification

    Program

    TestingResults

    Verification

  • 7/27/2019 Base SAS Training

    174/486

    E i

  • 7/27/2019 Base SAS Training

    175/486

    175

    This exercise reinforces the concepts discussedpreviously.

    Exercises

    Editing SAS Code Enter the DATA step code from your previous Coding

  • 7/27/2019 Base SAS Training

    176/486

    176

    to Read a Raw Data File exercise into the Program

    Editor window. Execute the program and review the log.

    Recall the DATA step code.

    Add a dollar sign, $, to the beginning of the variable

    name CargoRate.

    Execute the program and review the log.

    Recall the DATA step code and correct the error.

  • 7/27/2019 Base SAS Training

    177/486

    Section 4.4

    Saving and Retrieving

    SAS Code

  • 7/27/2019 Base SAS Training

    178/486

    Saving and Retrieving SAS CodeAfter a program is entered, tested, and the results

    ifi d th d b

  • 7/27/2019 Base SAS Training

    179/486

    179

    verified, the program code can be

    saved for future use

    retrieved at a later time.

    Saving SAS Program CodeProgram code is saved in program files because it can be

  • 7/27/2019 Base SAS Training

    180/486

    180

    rerun later without being re-entered

    copied to be modified into a new program

    shared with other people

    printed for documentation.

    Retrieving SAS Program CodeProgram code that has been saved in a program file canb t i d i t th P Edit h it b

  • 7/27/2019 Base SAS Training

    181/486

    181

    be retrieved into the Program Editor where it can be

    rerun

    modified.

    Saving SAS Program Code

  • 7/27/2019 Base SAS Training

    182/486

    182

    Program File

    StoredSAS

    Program

    Code

    Retrieving SAS Program Code

  • 7/27/2019 Base SAS Training

    183/486

    183

    Program File

    StoredSAS

    Program

    Code

  • 7/27/2019 Base SAS Training

    184/486

    Exercises

  • 7/27/2019 Base SAS Training

    185/486

    185

    This exercise reinforces the concepts discussedpreviously.

    Exercises

    Saving and Retrieving SAS Code Recall the DATA step code from your previous

    exercise into the Program Editor window

  • 7/27/2019 Base SAS Training

    186/486

    186

    exercise into the Program Editor window.

    Save the code to a program file called ch5ex2.

    Clear the contents of the Program Editor window.

    Retrieve your program called ch5ex2.

  • 7/27/2019 Base SAS Training

    187/486

    Section 4.5Additional Topics

    (Optional)

    Objectives Copy, move, delete, and insert text.

    Cl th t t f i d

  • 7/27/2019 Base SAS Training

    188/486

    188

    Clear the contents of a window.

    Navigate windows.

    Correct unbalanced quotation marks.

  • 7/27/2019 Base SAS Training

    189/486

  • 7/27/2019 Base SAS Training

    190/486

    Section 5.1Introduction

    Objectives Plan to create a list report based on a

    SAS data set

  • 7/27/2019 Base SAS Training

    191/486

    191

    SAS data set.

    Code to create a list report based on aSAS data set.

    Why Create a List Report?

  • 7/27/2019 Base SAS Training

    192/486

    192 ...

    List reports are createdto view the data in aSAS data set.

    Definition of a List ReportA list report

    shows the data in a SAS data set

  • 7/27/2019 Base SAS Training

    193/486

    193

    shows the data in a SAS data set

    displays one line for each observation in theSAS data set

    displays all variables or only those specified.

    Example of a List Report

    Employee

  • 7/27/2019 Base SAS Training

    194/486

    194

    Obs ID FirstName LastName JobCode1 E01046 DAVID CHAPMAN PILOT12 E01682 VICTOR TAILOR PILOT13 E02659 CLIFTON G. WILDER PILOT14 E04042 SAMUEL BENNETT PILOT15 E04732 CHRISTIAN EDMINSTON PILOT16 E03740 CRAIG N. SAWYER PILOT17 E03389 LOUISE STAINES PILOT18 E04688 JOHN D. PERRY PILOT1

    Why Use a PROC Step?

  • 7/27/2019 Base SAS Training

    195/486

    195 ...

    SAS Data Set

    DescriptorReport

    PROC

    Step

  • 7/27/2019 Base SAS Training

    196/486

    Objectives Plan to create a list report based on a

    SAS data set

  • 7/27/2019 Base SAS Training

    197/486

    197

    SAS data set.

    Partial List Output

    Employee Job

  • 7/27/2019 Base SAS Training

    198/486

    198

    Obs ID FirstName LastName Code1 E01046 DAVID CHAPMAN PILOT12 E01682 VICTOR TAILOR PILOT13 E02659 CLIFTON G. WILDER PILOT14 E04042 SAMUEL BENNETT PILOT15 E04732 CHRISTIAN EDMINSTON PILOT16 E03740 CRAIG N. SAWYER PILOT17 E03389 LOUISE STAINES PILOT1

  • 7/27/2019 Base SAS Training

    199/486

    DATA Step

    data pilotdata;i fil 'i t d t fil '

  • 7/27/2019 Base SAS Training

    200/486

    200

    infile 'input-raw-data-file';input EmployeeID $ 1 - 6

    FirstName $ 7 - 19LastName $ 20 - 34JobCode $ 35 - 41Salary 42 - 47

    Category $ 48 - 50;run;

    Name the SAS Data Set to be Viewed

    OperatingE i t

    SAS Data Set Name

  • 7/27/2019 Base SAS Training

    201/486

    201

    EnvironmentSAS Data Set Name

    Windows pilotdata

    UNIX pilotdata

    OS/390 pilotdata

    Determine the Variables to be Displayed

    SAS Variable Name

  • 7/27/2019 Base SAS Training

    202/486

    202 ...

    SAS Variable Name

    EmployeeID

    FirstName

    LastName

    JobCode

    Salary

    Category

    Specify the Desired Order of the Variables

  • 7/27/2019 Base SAS Training

    203/486

    203 ...

    EmployeeID FirstName LastName JobCode

  • 7/27/2019 Base SAS Training

    204/486

    Planning to Create a List Report Based on your program called ch5ex2 or the program

    called ch6ex, plan to create a list report.

  • 7/27/2019 Base SAS Training

    205/486

    205

    , p p

    The desired list report is Report #1 of the ExerciseScenario, except do not include the

    NewCargoRate variable.

  • 7/27/2019 Base SAS Training

    206/486

  • 7/27/2019 Base SAS Training

    207/486

  • 7/27/2019 Base SAS Training

    208/486

    Section 5.3Coding to Create

    a List Report

    Objectives Code to create a list report based on a

    SAS data set.

  • 7/27/2019 Base SAS Training

    209/486

    209

    Choosing the PROC PRINT Step

  • 7/27/2019 Base SAS Training

    210/486

    210

    PROC

    Step(s)

    MEANS

    PRINT

    FREQ

    . . .

  • 7/27/2019 Base SAS Training

    211/486

    The PROC PRINT StepThe PROC PRINT statement names the SAS data setbeing displayed in the list report.

  • 7/27/2019 Base SAS Training

    212/486

    212

    General form of the PROC PRINT statement:

    The RUN statement signals the end of the step.

    General form of the RUN statement:

    PROC PRINT DATA = SAS_data_set_name;

    RUN;

  • 7/27/2019 Base SAS Training

    213/486

  • 7/27/2019 Base SAS Training

    214/486

    214

    This demonstration illustrates using thePROC PRINT statement and the RUN

    statement.

    The PROC PRINT Step

  • 7/27/2019 Base SAS Training

    215/486

    The VAR StatementThe VAR statement names the variables to be displayedand specifies the desired order of the variables.

  • 7/27/2019 Base SAS Training

    216/486

    216

    General form of the VAR statement:

    Example:

    VARvariable . . . ;

    var EmployeeID FirstName LastName JobCode;

    The VAR StatementBased on the International Airlines lecture scenario, thecode for the complete PROC PRINT step is

  • 7/27/2019 Base SAS Training

    217/486

    217

    proc print data = pilotdata;var EmployeeID FirstName LastName JobCode;

    run;

  • 7/27/2019 Base SAS Training

    218/486

    218

    This demonstration illustrates using thePROC PRINT statement, the VAR statement,

    and the RUN statement.

    The VAR Statement

  • 7/27/2019 Base SAS Training

    219/486

    Coding to Create a List Report Draw lines connecting the appropriate boxes.

    Based on your previous Planning to Create a List

  • 7/27/2019 Base SAS Training

    220/486

    220

    Report exercise: write the PROC PRINT statement

    write the VAR statement

    write the RUN statement.

    Retrieve your program named ch5ex2orthe programcalled ch6ex.

    Add the PROC PRINT step to the bottom of theprogram.

    Save the code to a program file named ch6ex2.

    Coding to Create a List Report

  • 7/27/2019 Base SAS Training

    221/486

    221

    1st Statement

    2nd Statement

    3rd Statement

    VAR

    RUN

    PROC

    Signals end ofPROC step

    Names SAS dataset to display inreport

    Names variables tobe displayed

    1st Statement

    PROC

    Names SAS dataset to display inreport

    2nd Statement

    Names variables tobe displayed

    VAR

    3rd Statement

    RUN

    Signals end ofPROC step

    ...

  • 7/27/2019 Base SAS Training

    222/486

  • 7/27/2019 Base SAS Training

    223/486

    Section 6.1Introduction

    Objectives Plan to create a variable in a SAS data set.

    Code to create a variable in a SAS data set.

  • 7/27/2019 Base SAS Training

    224/486

    224

  • 7/27/2019 Base SAS Training

    225/486

    Why Create a Variable?

    Employee

  • 7/27/2019 Base SAS Training

    226/486

    226

    Obs ID FirstName LastName JobCode1 E01046 DAVID CHAPMAN PILOT12 E01682 VICTOR TAILOR PILOT13 E02659 CLIFTON G. WILDER PILOT14 E04042 SAMUEL BENNETT PILOT15 E04732 CHRISTIAN EDMINSTON PILOT16 E03740 CRAIG N. SAWYER PILOT17 E03389 LOUISE STAINES PILOT1

    Why Create a Variable?

  • 7/27/2019 Base SAS Training

    227/486

    227

    EmployeeObs ID FirstName LastName JobCode1 E01046 DAVID CHAPMAN PILOT12 E01682 VICTOR TAILOR PILOT13 E02659 CLIFTON G. WILDER PILOT14 E04042 SAMUEL BENNETT PILOT15 E04732 CHRISTIAN EDMINSTON PILOT16 E03740 CRAIG N. SAWYER PILOT17 E03389 LOUISE STAINES PILOT1

    Bonus7266449853635287761262287439

    Why Use a DATA Step?

  • 7/27/2019 Base SAS Training

    228/486

    228 ......

    Raw Data File SAS Data SetDescriptor

    DATA

    Step

  • 7/27/2019 Base SAS Training

    229/486

    Section 6.2Planning to Create

    a Variable

    Objectives Plan to create a variable in a SAS data set.

  • 7/27/2019 Base SAS Training

    230/486

    230

  • 7/27/2019 Base SAS Training

    231/486

    Requirements to Create a Variable1. Specify the DATA step that defines the SAS data set.

    2. Define the variable.

  • 7/27/2019 Base SAS Training

    232/486

    232

  • 7/27/2019 Base SAS Training

    233/486

    Define the VariableTo define a variable,

    1. name the new variable

  • 7/27/2019 Base SAS Training

    234/486

    234

    2. determine the expression.

  • 7/27/2019 Base SAS Training

    235/486

    Name the New VariableBecause the variable being created represents a bonus of10% of each pilots current salary, a reasonable variable

  • 7/27/2019 Base SAS Training

    236/486

    236 ......

    name is

    Bonus

    Determine the ExpressionThe expression can be any valid combination of

    constants

  • 7/27/2019 Base SAS Training

    237/486

    237

    variables operators

    parentheses.

  • 7/27/2019 Base SAS Training

    238/486

    Numeric ConstantsA numeric constant is a valid numerical value.

    4922

  • 7/27/2019 Base SAS Training

    239/486

    239

    -728 2.1415926

    -0.56

    1.86E05

  • 7/27/2019 Base SAS Training

    240/486

    Character ConstantsA character constant is a string of text enclosed inmatching quotes.

  • 7/27/2019 Base SAS Training

    241/486

    241

    Wednesday 123 Main Street

    INSTRUCTOR

    % of revenue

    7251

  • 7/27/2019 Base SAS Training

    242/486

    VariablesA variable referenced in an expression uses the value ofthat variable.

  • 7/27/2019 Base SAS Training

    243/486

    243

    SocialSecurityNumber _2005_YearlyReturns

    day5

    PercentIncrease

    last_name

  • 7/27/2019 Base SAS Training

    244/486

  • 7/27/2019 Base SAS Training

    245/486

    Operators and ParenthesesAn expression can include operators and parentheses,along with numeric constants and variables.

  • 7/27/2019 Base SAS Training

    246/486

    246

    price * quantity GrossPay - tax

    x**2

    (jan_amt + feb_amt + mar_amt) / 3

    CurrentRate + 10.27

    Operators and Parentheses - Examples

    TotalSales = price * quantity

  • 7/27/2019 Base SAS Training

    247/486

    247

    net_pay = gross_pay - tax

    squared = x**2

    avg = (jan_amt + feb_amt + mar_amt) / 3

    NewRate = CurrentRate + 10.27

  • 7/27/2019 Base SAS Training

    248/486

    Define the Variable

    Salary * 0.10Bonus =

  • 7/27/2019 Base SAS Training

    249/486

    249 ......

    Exercises

  • 7/27/2019 Base SAS Training

    250/486

    250

    This exercise reinforces the concepts discussedpreviously.

  • 7/27/2019 Base SAS Training

    251/486

  • 7/27/2019 Base SAS Training

    252/486

    Planning to Create a Variableb. The two types of constants are

    h t

  • 7/27/2019 Base SAS Training

    253/486

    253 ......

    character

    numeric

  • 7/27/2019 Base SAS Training

    254/486

  • 7/27/2019 Base SAS Training

    255/486

    Planning to Create a Variablee. An expression can include ,

    , , and/or

    constants

    variables operators parentheses.

  • 7/27/2019 Base SAS Training

    256/486

    256 ......

  • 7/27/2019 Base SAS Training

    257/486

  • 7/27/2019 Base SAS Training

    258/486

  • 7/27/2019 Base SAS Training

    259/486

    Planning to Create a Variableh. Circle the valid operators for an expression from the list

    below.

  • 7/27/2019 Base SAS Training

    260/486

    260 ......

    1) * 6) %

    2) / 7) -

    3) # 8) &

    4) ** 9) =

    5) +

    Planning to Create a Variablei. Name the variable to create that will represent a 50

    cents per pound increase in cargo rate.

    NewCargoRate

  • 7/27/2019 Base SAS Training

    261/486

    261 ......

    Name the numeric constant that is needed in theexpression.

    0.50

    NewCargoRate

    Planning to Create a VariableName the existing variable that is needed in theexpression.

    CargoRate

  • 7/27/2019 Base SAS Training

    262/486

    262 ......

    Name the operator that is needed in the expression.

    CargoRate

    +

  • 7/27/2019 Base SAS Training

    263/486

  • 7/27/2019 Base SAS Training

    264/486

    Section 6.3

    Coding to Create

    a Variable

    Objectives Code to create a variable in a SAS data set.

  • 7/27/2019 Base SAS Training

    265/486

    265

    The DATA StepAs a starting point for this section, the DATA step consistsof the following statements:

    the DATA statement

  • 7/27/2019 Base SAS Training

    266/486

    266

    the DATA statement the INFILE statement

    the INPUT statement

    the RUN statement.

    The DATA StepAn assignment statement can be added to the DATAstep.

    The DATA step then consists of the following statements:

  • 7/27/2019 Base SAS Training

    267/486

    267

    The DATA step then consists of the following statements: the DATA statement

    the INFILE statement

    the INPUT statement

    the assignment statement

    the RUN statement.

    The Assignment StatementThe assignment statement defines a variable beingcreated.

    General form of the assignment statement:

  • 7/27/2019 Base SAS Training

    268/486

    268 ......

    General form of the assignment statement:

    Example:

    Remember the semicolon.

    variable = expression;

    Bonus = Salary * 0.10;

    The DATA StepBased on the International Airlines lecture scenario, thecode for the complete DATA step is

    data pilotdata;

  • 7/27/2019 Base SAS Training

    269/486

    269 ......

    data pilotdata;infile 'input-raw-data-file';input EmployeeID $ 1 - 6

    FirstName $ 7 - 19LastName $ 20 - 34

    JobCode $ 35 - 41Salary 42 - 47Category $ 48 - 50;

    Bonus = Salary * 0.10;run;

    Executing the Assignment Statement

  • 7/27/2019 Base SAS Training

    270/486

    270 ......

    Salary * 0.10;Bonus =

    4268 52000. . .ANGELA

    EmployeeID FirstName Salary Bonus

    Executing the Assignment Statement

    EVALUATE

  • 7/27/2019 Base SAS Training

    271/486

    271

    * 0.10Bonus =

    4268 52000. . .ANGELA

    EmployeeID FirstName Salary Bonus

    52000

    5200 5200

    5200

    The Assignment Statement

  • 7/27/2019 Base SAS Training

    272/486

    272

    This demonstration illustrates usingthe assignment statement in an existing SAS

    program.

    Exercises

  • 7/27/2019 Base SAS Training

    273/486

    273

    This exercise reinforces the concepts discussedpreviously.

    Coding to Create a VariableBased on your previous Planning to Create a Variableexercise:

    Write the assignment statement

  • 7/27/2019 Base SAS Training

    274/486

    274

    Write the assignment statement. Retrieve your program named CH6EX2 OR the

    program named CH7EX.

    Add the assignment statement to the program after the

    INPUT statement. Save the code to a program named CH7EX2.

  • 7/27/2019 Base SAS Training

    275/486

    Chapter 7

    Performing Conditional Logic

    with the DATA Step

  • 7/27/2019 Base SAS Training

    276/486

    Section 7.1

    Introduction

    Objectives Plan to perform conditional logic to create a variable in

    a SAS data set.

    Code to perform conditional logic to create a variablein a SAS data set

  • 7/27/2019 Base SAS Training

    277/486

    277

    Code to perform conditional logic to create a variablein a SAS data set.

    Why Use Conditional Logic?Conditional logic is used to create values for a variablebased on whether a situation is true or false.

  • 7/27/2019 Base SAS Training

    278/486

    278 ......

    Ifi

    ti

    s

    rain

    ing,

    ta eyouru

    m

    brella

    .

    Oth

    erw

    ise

    ,take

    apairofsung

    l

    asses

    .

    EmployeeObs ID LastName JobCode Salary

    Why Use Conditional Logic?

    NewSalary

  • 7/27/2019 Base SAS Training

    279/486

    279 ......

    1 E01046 CHAPMAN PILOT1 726602 E01682 TAILOR PILOT1 44980

    . . .11 E01642 MCELROY PILOT2 7826012 E04348 CARTER PILOT2 74620

    . . .36 E03728 MASON PILOT3 11651037 E03892 LEE PILOT3 112690

    A new variable needs

    to be created,representing an increase in salary.

    Employee NewObs ID LastName JobCode Salary Salary

    Why Use Conditional Logic?

    5% 76293

  • 7/27/2019 Base SAS Training

    280/486

    280 ......

    1 E01046 CHAPMAN PILOT1 726602 E01682 TAILOR PILOT1 44980

    . . .11 E01642 MCELROY PILOT2 7826012 E04348 CARTER PILOT2 74620

    . . .36 E03728 MASON PILOT3 11651037 E03892 LEE PILOT3 112690

    7%

    Increase

    5%

    Increase

    9%

    Increase

    76293

    47229

    83738

    79843

    126996

    122832

  • 7/27/2019 Base SAS Training

    281/486

    Section 7.2

    Planning to Perform

    Conditional Logic to Create a

    Variable

    Objectives Plan to perform conditional logic to create a variable in

    a SAS data set.

  • 7/27/2019 Base SAS Training

    282/486

    282

    Partial List Report

    EmployeeObs ID LastName JobCode Salary

  • 7/27/2019 Base SAS Training

    283/486

    283

    1 E01046 CHAPMAN PILOT1 726602 E01682 TAILOR PILOT1 44980

    . . .11 E01642 MCELROY PILOT2 7826012 E04348 CARTER PILOT2 74620

    . . .36 E03728 MASON PILOT3 11651037 E03892 LEE PILOT3 112690

    Employee NewObs ID LastName JobCode Salary Salary

    76293

    Partial List Report

  • 7/27/2019 Base SAS Training

    284/486

    284

    1 E01046 CHAPMAN PILOT1 726602 E01682 TAILOR PILOT1 44980

    . . .11 E01642 MCELROY PILOT2 7826012 E04348 CARTER PILOT2 74620

    . . .36 E03728 MASON PILOT3 11651037 E03892 LEE PILOT3 112690

    7%Increase

    5%

    Increase

    9%

    Increase

    76293

    47229

    83738

    79843

    126996

    122832

    Requirements to Perform Conditional Logic Specify the DATA step that defines the

    SAS data set.

    Determine the condition.D t i th ti

  • 7/27/2019 Base SAS Training

    285/486

    285

    Determine the action.

  • 7/27/2019 Base SAS Training

    286/486

    Determine the ConditionThe condition is an expression that can be evaluated aseither true or false.

    Example:

  • 7/27/2019 Base SAS Training

    287/486

    287 ......

    Example:

    If it is raining,

    take your umbrella.

    Determine the ConditionThe condition can be expressed as any valid combination of

    constants

    variablest d/ th

  • 7/27/2019 Base SAS Training

    288/486

    288

    operator and/or parentheses.

    Example:

    variable = constant

    Employee NewObs ID LastName JobCode Salary Salary

    Determine the Condition

    5% 76293

  • 7/27/2019 Base SAS Training

    289/486

    289 ......

    1 E01046 CHAPMAN PILOT1 726602 E01682 TAILOR PILOT1 44980

    . . .11 E01642 MCELROY PILOT2 7826012 E04348 CARTER PILOT2 74620

    . . .36 E03728 MASON PILOT3 11651037 E03892 LEE PILOT3 112690

    7%Increase

    5%

    Increase

    9%

    Increase

    76293

    47229

    83738

    79843

    126996

    122832

    Determine the Condition

    variable = constant

  • 7/27/2019 Base SAS Training

    290/486

    290 ......

    =JobCode 'PILOT1'

    =JobCode 'PILOT2'

    =JobCode 'PILOT3'

    Character values

    must be enclosedin quotes and

    match the caseof the data.

    Determine the ActionThe action is what SAS should do when the condition istrue.

    Example:

  • 7/27/2019 Base SAS Training

    291/486

    291 ......

    Example:

    If it is raining,

    take your umbrella.

  • 7/27/2019 Base SAS Training

    292/486

    Employee NewObs ID LastName JobCode Salary Salary

    Determine the Action

    5% 76293

  • 7/27/2019 Base SAS Training

    293/486

    293 ......

    1 E01046 CHAPMAN PILOT1 726602 E01682 TAILOR PILOT1 44980

    . . .11 E01642 MCELROY PILOT2 7826012 E04348 CARTER PILOT2 74620

    . . .36 E03728 MASON PILOT3 11651037 E03892 LEE PILOT3 112690

    7%Increase

    5%

    Increase

    9%

    Increase

    76293

    47229

    83738

    79843

    126996

    122832

  • 7/27/2019 Base SAS Training

    294/486

    Exercises

  • 7/27/2019 Base SAS Training

    295/486

    295

    This exercise reinforces the concepts discussedpreviously.

    Planning to Perform Conditional Logic toCreate a Variable

    Based on your program named CH7EX2 orthe program

    named CH8EX, plan to perform conditional logic to createa variable

  • 7/27/2019 Base SAS Training

    296/486

    296

    a variable.

    The variable that needs to be created will represent anincrease in passenger fare. The passenger fares will

    increase 8% for a short-range flight, 10% for a medium-range flight, and 12% for a long-range flight.

    A list report needs to be displayed after creating thevariable.

    Determine the condition.

    Planning to Perform Conditional Logic toCreate a Variable

  • 7/27/2019 Base SAS Training

    297/486

    297 ......

    Determine the action.

    NewPassengerFare = PassengerFare * 1.08

    NewPassengerFare = PassengerFare * 1.10

    NewPassengerFare = PassengerFare * 1.12

    Range = 'SHORT'

    Range = 'MEDIUM'

    Range = 'LONG'

  • 7/27/2019 Base SAS Training

    298/486

    Section 7.3

    Coding to Perform Conditional

    Logic to Create a Variable

    Objectives Code to perform conditional logic to create a variable

    in a SAS data set.

  • 7/27/2019 Base SAS Training

    299/486

    299

    The DATA StepAs a starting point for this section, the DATA step consistsof the following statements:

    the DATA statement the INFILE statement

  • 7/27/2019 Base SAS Training

    300/486

    300

    the INFILE statement

    the INPUT statement

    the assignment statement

    the RUN statement.

    The DATA StepConditional statements can be added.

    The DATA step then consists of the following statements:

    the DATA statement the INFILE statement

  • 7/27/2019 Base SAS Training

    301/486

    301

    the INFILE statement

    the INPUT statement

    the assignment statement

    conditional statements

    the RUN statement.

    Conditional StatementsConditional statements can create values for a newvariable based on whether a condition is true or false.

    Conditional statements can be expressed in the form ofIF-THEN statements.

  • 7/27/2019 Base SAS Training

    302/486

    302

    General form of the IF-THEN statement:

    IF condition THEN action;

    if job_code = PILOT1 then1 0

    Conditional StatementsExample:

    condition

  • 7/27/2019 Base SAS Training

    303/486

    303

    new_salary = salary * 1.05;

    if job_code = PILOT2 then

    new_salary = salary * 1.07;

    if job_code = PILOT3 thennew_salary = salary * 1.09;

    action

    condition

    action

    condition

    action

    Conditional StatementsExample:

    if JobCode = 'PILOT1' thenl l * 1 05

  • 7/27/2019 Base SAS Training

    304/486

    304

    NewSalary = Salary * 1.05;

    if JobCode = 'PILOT2' then

    NewSalary = Salary * 1.07;

    if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;

    action

    action

    action

    Conditional StatementsExample:

    if JobCode = 'PILOT1' then

  • 7/27/2019 Base SAS Training

    305/486

    305

    NewSalary = Salary * 1.05;

    if JobCode = 'PILOT2' thenNewSalary = Salary * 1.07;

    if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;

    condition

    TRUE

  • 7/27/2019 Base SAS Training

    306/486

    306 ......

    new_variable expression=

    value

    EVALUATED

    value

    ASSIGNED

    condition

    FALSE

    BYPASSED

  • 7/27/2019 Base SAS Training

    307/486

    307 ......

    new_variable expression=

    next s tatement

    EXECUTED

    Conditional StatementsIn a series of related IF-THEN statements, eachstatement is processed even if a prior IF-THEN statementis true.

  • 7/27/2019 Base SAS Training

    308/486

    308

    if JobCode = 'PILOT1' thenNewSalary = Salary * 1.05;

    if JobCode = 'PILOT2' thenNewSalary = Salary * 1.07;

    if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;

    EmployeeID LastName JobCode SalaryE01046 CHAPMAN PILOT1 72660

    Conditional Statements

  • 7/27/2019 Base SAS Training

    309/486

    309 ......

    if JobCode = 'PILOT1' thenNewSalary = Salary * 1.05;

    if JobCode = 'PILOT2' thenNewSalary = Salary * 1.07;

    if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;

    True

    False

    False

    Conditional StatementsA more efficient approach uses an ELSE IF-THENsequence of statements. After a condition is true, nofurther statements in the series are processed.

    General form of the ELSE IF-THEN statement:

  • 7/27/2019 Base SAS Training

    310/486

    310

    IF condition THEN action;

    ELSE IF condition THEN action;. . .

    Conditional StatementsExample:

    if JobCode = 'PILOT1' thenNewSalary = Salary * 1.05;

  • 7/27/2019 Base SAS Training

    311/486

    311

    else if JobCode = 'PILOT2' thenNewSalary = Salary * 1.07;

    else if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;

    Conditional StatementsEmployeeID LastName JobCode Salary

    E01046 CHAPMAN PILOT1 72660

  • 7/27/2019 Base SAS Training

    312/486

    312 ......

    if JobCode = 'PILOT1' thenNewSalary = Salary * 1.05;

    else if JobCode = 'PILOT2' thenNewSalary = Salary * 1.07;

    else if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;

    True

    The rest

    of the

    series is

    skipped.

    The DATA Stepdata pilotdata;

    infile 'input-raw-data-file';input EmployeeID $ 1 - 6

    FirstName $ 7 - 19LastName $ 20 - 34

  • 7/27/2019 Base SAS Training

    313/486

    313 ......

    $JobCode $ 35 - 41Salary 42 - 47Category $ 48 - 50;

    Bonus = Salary * 0.10;if JobCode = 'PILOT1' thenNewSalary = Salary * 1.05;

    else if JobCode = 'PILOT2' thenNewSalary = Salary * 1.07;

    else if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;run;

    Conditional Statements

  • 7/27/2019 Base SAS Training

    314/486

    314

    This demonstration illustrates using IF/THENstatement and ELSE IF/THEN statements in an

    existing SAS program.

    Exercises

  • 7/27/2019 Base SAS Training

    315/486

    315

    This exercise reinforces the concepts discussedpreviously.

    Coding to Perform Conditional Logic toCreate a Variable

    Based on your previous Planning to Perform Conditional

    Logic to Create a Variable exercise: Write the three conditional statements.

  • 7/27/2019 Base SAS Training

    316/486

    316

    Retrieve your program named CH7EX2 ortheprogram named CH8EX.

    Add the conditional statements to the program afterthe assignment statement.

    Save the code to a program named CH8EX2.

  • 7/27/2019 Base SAS Training

    317/486

    Chapter 8

    Creating Statistical Reports

    with PROC Steps

  • 7/27/2019 Base SAS Training

    318/486

    Section 8.1

    Introduction

    Objectives Plan to create a summary report based on a

    SAS data set.

    Code to create a summary report based on aSAS data set.

  • 7/27/2019 Base SAS Training

    319/486

    319

    Plan to create a frequency report based on aSAS data set.

    Code to create a frequency report based on aSAS data set.

    Why Create a Statistical Report?You create a statistical report

    to display statistical informationthat is not available in a list report.

  • 7/27/2019 Base SAS Training

    320/486

    320 ......

    A SAS Statistical ReportA statistical report

    displays statistics calculated based on the SASdata set

    consolidates data from the SAS data set

  • 7/27/2019 Base SAS Training

    321/486

    321

    represents multiple observations from the SAS dataset in each line of the report

    may be either summary

    frequency.

    A SAS Summary ReportA SAS summary report

    produces simple descriptive statistics for numericvariables

    calculates descriptive statistics including

  • 7/27/2019 Base SAS Training

    322/486

    322

    number of nonmissing values

    mean

    standard deviation minimum value

    maximum value.

    Example of a SAS Summary ReportThe MEANS Procedure

    NCategory Obs Variable N Mean Std Dev Minimum Maximum____________________________________________________________________DOM 19 Salary 19 68405.79 11439.29 44980.00 79760.00

  • 7/27/2019 Base SAS Training

    323/486

    323

    NewSalary 19 72550.12 12562.02 47229.00 85342.20INT 31 Salary 31 99252.55 21019.22 67340.00 125250.00

    NewSalary 31 107365.26 23700.21 72052.80 136522.50____________________________________________________________________

    A SAS Frequency ReportA SAS frequency report

    produces simple statistics that count how oftenindividual values occur within a SAS data set

    calculates statistics including

  • 7/27/2019 Base SAS Training

    324/486

    324

    frequency

    percent

    cumulative frequency cumulative percent.

    Example of a SAS Frequency ReportThe FREQ Procedure

    Cumulative CumulativeCategory Frequency Percent Frequency Percent------------------------------------------------------DOM 19 38.00 19 38.00

  • 7/27/2019 Base SAS Training

    325/486

    325

    INT 31 62.00 50 100.00

  • 7/27/2019 Base SAS Training

    326/486

    Section 8.2

    Planning to Create a Summary

    Report

    Objectives Plan to create a summary report based on a SAS

    data set.

  • 7/27/2019 Base SAS Training

    327/486

    327

    Example of a SAS Summary ReportThe MEANS Procedure

    NCategory Obs Variable N Mean Std Dev Minimum Maximum____________________________________________________________________DOM 19 Salary 19 68405.79 11439.29 44980.00 79760.00

  • 7/27/2019 Base SAS Training

    328/486

    328

    NewSalary 19 72550.12 12562.02 47229.00 85342.20INT 31 Salary 31 99252.55 21019.22 67340.00 125250.00

    NewSalary 31 107365.26 23700.21 72052.80 136522.50_____________________________________________________________________

    Requirements to Create a SASSummary Report

    1. Name the SAS data set to be referenced.

    2. Determine the variables on which to calculate thestatistics.

  • 7/27/2019 Base SAS Training

    329/486

    329

    2. Determine the variable by which to group the data.

    DATA Step Exampledata pilotdata;

    infile 'input-raw-data-file';input EmployeeID $ 1 - 6

    FirstName $ 7 - 19LastName $ 20 - 34JobCode $ 35 41

  • 7/27/2019 Base SAS Training

    330/486

    330

    JobCode $ 35 - 41Salary 42 - 47Category $ 48 - 50;

    Bonus = Salary * 0.10;if JobCode = 'PILOT1' thenNewSalary = Salary * 1.05;

    else if JobCode = 'PILOT2' thenNewSalary = Salary * 1.07;

    else if JobCode = 'PILOT3' thenNewSalary = Salary * 1.09;run;

    Name the SAS Data Set to Be Referenced

    OperatingEnvironment

    SAS Data SetName

    Windows pilotdata

  • 7/27/2019 Base SAS Training

    331/486

    331

    Windows pilotdata

    UNIX pilotdata

    OS/390 pilotdata

    Determine the Variables on Which toCalculate the Statistics

    SAS Variable NameEmployeeID

  • 7/27/2019 Base SAS Training

    332/486

    332 ......

    FirstName

    LastName

    JobCode

    Salary

    Category

    Bonus

    NewSalary

    Determine the Variable by Which to Groupthe Data

    SAS Variable NameEmployeeID

  • 7/27/2019 Base SAS Training

    333/486

    333 ......

    FirstName

    LastName

    JobCode

    Salary

    Category

    Bonus

    NewSalary

    This exercise reinforces the concepts discussed

    Exercises

  • 7/27/2019 Base SAS Training

    334/486

    334

    This exercise reinforces the concepts discussedpreviously.

    Planning to Create a Summary ReportBased on your program named CH8EX2 orthe programnamed CH9EX, plan to create a summary report.

    The desired summary report is a comparison between theaverage current passenger fare and the average newpassenger fare for the domestic and international flights.

  • 7/27/2019 Base SAS Training

    335/486

    335

    passenger fare for the domestic and international flights.This is Report #2 of the exercise scenario.

    Planning to Create a Summary ReportName the SAS data set to be referenced.

    Name the variables on which to calculate the descriptivestatistics

    ratedata

  • 7/27/2019 Base SAS Training

    336/486

    336 ......

    statistics.

    PassengerFareNewPassengerFare

    Planning to Create a Summary ReportName the variable by which to group the data.

    Category