Introduction to Programming Concepts Using SAS Software
Course Notes
Chapter 1 Learning the Concepts of SAS Programming
1.1 1.2 1.3 1.4 1.5 1.6 Introduction Explaining the Basic Functionality of SAS Software Identifying the File Types Used in SAS Identifying the Components of a SAS Program Chapter Summary Solutions to Exercises
Chapter 2 Introducing the Course Scenarios
2.1 Introduction 2.2 Defining the Lecture Scenario 2.3 Defining the Exercise Scenario
Chapter 3 Reading a Raw Data File with the DATA Step
3.1 3.2 3.3 3.4 3.5 Introduction Planning to Read a Raw Data File Coding to Read a Raw Data File Chapter Summary Solutions to Exercises
Chapter 4 Navigating the SAS Windowing Environment
4.1 4.2 4.3 4.4 4.5 4.6 4.7
3
Introduction Entering and Executing SAS Code Editing SAS Code Saving and Retrieving SAS Code Additional Topics (Optional) Chapter Summary Solutions to Exercises
Chapter 5 Creating a List Report with a PROC Step
5.1 5.2 5.3 5.4 5.5 Introduction Planning to Create a List Report Coding to Create a List Report Chapter Summary Solutions to Exercises
Chapter 6 Creating a Variable with the DATA Step
6.1 6.2 6.3 6.4 6.5 Introduction Planning to Create a Variable Coding to Create a Variable Chapter Summary Solutions to Exercises
Chapter 7 Performing Conditional Logic with the DATA Step
7.1 7.2 7.3 7.4 7.5 Introduction Planning to Perform Conditional Logic to Create a Variable Coding to Perform Conditional Logic to Create a Variable Chapter Summary Solutions to Exercises
Chapter 8 Creating Statistical Reports with PROC Steps
8.1 8.2 8.3 8.4 8.5 8.6 8.7
5
Introduction Planning to Create a Summary Report Coding to Create a Summary Report Planning to Create a Frequency Report Coding to Create a Frequency Report Chapter Summary Solutions to Exercises
Chapter 9 Additional Topics
9.1 9.2 9.3 9.4 9.5 9.6 Creating a Permanent SAS Data Set Sorting the Observations in a SAS Data Set Enhancing a Report Displaying Selected Observations in a Report Reading Excel Spreadsheets Chapter Summary
Chapter 10 Learning More
10.1 Where Do I Go From Here? 10.2 SAS Institute Resources
Chapter 1
Learning the Concepts of SAS Programming
Section 1.1
Introduction
Objectives
Explain the basic functionality of SAS software. Identify the file types used in SAS. Identify the components of a SAS program.
What Is SAS?
SAS is a collection of components that enable you to manage, manipulate, and examine your data.
Reporting and Graphics Data Access and Management User Interfaces
Analytical
Base SAS
Application Development
Visualization and Discovery
Business Solutions
Web Enablement
10
... ...
Section 1.2
Explaining the Basic Functionality of SAS Software
Objectives
Explain the basic functionality of SAS software.
12
Basic Functionality
Access Manage
Data
Present
Analyze
13
... ...
Section 1.3
Identifying the File Types Used in SAS
Objectives
Identify the file types used in SAS.
15
Types of Files Used with SAS
Raw Data Files
SAS DATA Sets
SAS Program Files
16
Raw Data Files
Raw Data Files
are nonsoftware-specific files that contain records and fields can be created by a variety of software products can be read by a variety of software products consist of no special attributes, such as field headings, page breaks, or titles are not reports.
17
Raw Data Files
Operating Environment Windows UNIX File Location and Name C:\workshop\winsas\basic\survey.dat /users/edu##/survey.dat edu###.basic.sascode(survey) or edu###.basic.survey
OS/390
18
SAS Data Sets
SAS Data Sets
are files specific to SAS that contain variables and observations 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.
19
SAS Data Sets
Data Processing Terminology File Record Field SAS Terminology SAS Data Set Observation Variable
20
SAS Data Sets
Descriptor Portion
Data Portion
The descriptor portion contains attribute information about the data in a SAS data set. The data portion contains the data values in the form of a rectangular table made up of observations and variables.
21
SAS Data Sets
SAS 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, or underscores are not case sensitive.
22
SAS Data Sets
Operating Environment SAS Data Set Name
Windows
UNIX
survey
survey
OS/390
survey
The naming convention is the same regardless of the operating environment.
23
... ...
SAS Data Sets
Operating Environment SAS Data Set Name
Windows Temporary UNIX SAS Data Sets OS/390
survey
survey
survey
The naming convention is the same regardless of the operating environment.
24
... ...
SAS Data Sets
Operating Environment SAS Data Set Name
Windows
UNIX
work.survey
work.survey
OS/390
work.survey
Another convention for naming a temporary SAS data set is to precede the SAS data set name with the prefix WORK.
25
SAS Program Files
SAS Program Files
contain SAS program code can be saved and re-used.
26
SAS Program Files
Operating Environment Windows UNIX File Location and Name C:\workshop\winsas\basic\survey.sas /users/edu##/survey.sas edu###.basic.sascode(survey) or edu###.basic.survey
OS/390
27
Exercises
This exercise reinforces the concepts discussed previously.
28
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.
29
Identifying File Types Used in SAS
SAS Data Set
File File that thatcontains contains SAS SASprogram program code code
Raw Data File
File that File contains that contains data specific SAS-specific to SAS data
SAS Program File
File that contains non-software-specific nonsoftware-specific data data
... ...
30
Identifying File Types Used in SAS
False _______ Raw data files are only created by SAS.
True _______
SAS data sets are only created by SAS.
_______ True
A SAS program file can be saved and reused.
False _______
A raw data file contains variables and observations.
31
... ...
Identifying File Types Used in SAS
True _______ A SAS data set contains variables and observations.
False _______
A SAS data set name can be up to 64 characters in length.
False _______
A SAS data set name can contain a dollar sign ($).
False _______
32
Raw data files are reports.
... ...
Identifying File Types Used in SAS
_______ False The prefix TEMP. can be added to the beginning of a temporary SAS data set name.
False _______
The naming convention for a SAS program file is the same regardless of the operating environment.
33
... ...
Identifying File Types Used in SAS
Name the two portions of a SAS data set.
Descriptor Portion
Data Portion
34
... ...
Section 1.4
Identifying the Components of a SAS Program
Objectives
Identify the components of a SAS program.
36
Components of a SAS Program
A SAS program is a sequence of steps. There are only two kinds of steps: DATA steps PROC steps. A SAS Program
DATA step(s)
PROC step(s)
37
DATA Step(s)
Typically, DATA steps read data, such as raw data files, to create SAS data sets.
Raw Data File
SAS Data Set
Descriptor
DATA Step
38
... ...
DATA Step(s)
In addition, DATA steps can modify existing variables or create new variables as necessary.
Raw Data File
SAS Data Set
Descriptor
DATA Step
39
... ...
PROC Step(s)
PROC steps typically read SAS data sets to create reports.
SAS Data Set
Descriptor Report
PROC Step
40
... ...
PROC Step(s)
There are many different types of PROC steps.
PROC Step(s) PRINT ...
MEANS
FREQ
41
... ...
Components of a Step
A SAS program is a sequence of steps: DATA steps PROC steps. A step is a sequence of one or more statements.
42
Components of a Step
A statement usually starts with a keyword and always ends in a semicolon (;).
KEYWORD . . . ;
43
Components of a DATA Step
A DATA step starts with a DATA statement and ends with a RUN statement.
Start
data _______________ ;
_______________ ;
. . .
_______________ ;
End
run;
... ...
44
Components of a PROC Step
A PROC step starts with a PROC statement and ends with a RUN statement.
Start
proc _______________ ;
_______________ ;
. . .
_______________ ;
End
run;
45
... ...
Exercises
This exercise reinforces the concepts discussed previously.
46
Identifying the Components of a SAS Program
Circle the appropriate answer(s) for 2.a through 2.i. Fill in the blanks for 2.j.
47
Identifying the Components of a SAS Program
a. What are the two kinds of steps in a SAS program? 1) DATA 2) RUN 3) Statement 4) PROC 5) Data Set
48
... ...
Identifying the Components of a SAS Program
b. Will all SAS programs contain both a DATA step and a PROC step ? 1) Yes 2) No c. A DATA step typically reads what type of file? 1) Raw Data File 2) Program File 3) Report
49
... ...
Identifying the Components of a SAS Program
d. What does a DATA step typically create? 1) Raw Data File 2) Program File 3) SAS Data Set 4) Report
50
... ...
Identifying the Components of a SAS Program
e. A PROC step typically reads what type of file? 1) Raw Data File 2) Program File 3) SAS Data Set 4) Report
51
... ...
Identifying the Components of a SAS Program
f. What does a PROC step typically create? 1) Raw Data File 2) Program File 3) SAS Data Set 4) Report
52
... ...
Identifying the Components of a SAS Program
g. A step is a sequence of what? 1) Files 2) Statements 3) RUNs 4) Data Sets
53
... ...
Identifying the Components of a SAS Program
h. What does a statement usually start with? 1) A Colon 2) A Keyword 3) RUN 4) PROC
54
... ...
Identifying the Components of a SAS Program
i. What does a statement end with? 1) A Colon 2) A Period 3) A Comma 4) A Semicolon
55
... ...
Identifying the Components of a SAS Program
j. DATA statement and A DATA step starts with a ______ RUN statement. ends with a _____ A PROC step starts with a ______ PROC statement and ends with a _____ RUN statement.
56
...
Chapter 2
Introducing the Course Scenarios
Section 2.1
Introduction
Objectives
Define the business need for the lecture scenario. Outline the programming requirements for the lecture scenario. Define the business need for the exercise scenario. Outline the programming requirements for the exercise scenario.
59
Course Scenarios
International Airlines is a fictitious airline that transports people and cargo from city to city around the world.
60
... ...
Course Scenarios
The management of International Airlines has decided to give the pilots a bonus and a salary increase. Also, the management has decided to increase the cargo rates and the passenger fares for all flights.
61
Course Scenarios
The lecture portion of this course will address the scenario of the pilots receiving a bonus and a salary increase.
62
Course Scenarios
The exercise portion of this course will address the scenario of the increase in cargo rates and passenger fares for all flights.
63
Outline the Programming Requirements
Output Input
Report/File Processing
64
... ...
Section 2.2
Defining the Lecture Scenario
Objectives
Define the business need for the lecture scenario. Outline the programming requirements for the lecture scenario.
66
Define the Business Need
All 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.
67
... ...
Define the Business Need
The management of International Airlines needs three reports.
Report #1
Report #2
Report #3
68
Report #1
A listing of all the pilots with their expected bonuses.
69
Partial example of the desired report:
Obs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Employee ID E01046 E01682 E02659 E04042 E04732 E03740 E03389 E04688 E01702 E02391 E01642 E04348 E03875 E03587 E03739 E03768
FirstName DAVID VICTOR CLIFTON G. SAMUEL CHRISTIAN CRAIG N. LOUISE JOHN D. ROBERTA J. DONALD E NANCY A. CAROLYN P. PAUL J. YIQUN WILLIAM J. JAN
LastName CHAPMAN TAILOR WILDER BENNETT EDMINSTON SAWYER STAINES PERRY CHADWICK TAYLOR MCELOY CARTER GLENNON SANTIAGO MCKENZIE STANDER
JobCode PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT2 PILOT2 PILOT2 PILOT2 PILOT2 PILOT2
Bonus 7266 4498 5363 5287 7612 6228 7439 6768 6228 4498 7826 7462 7462 7826 7462 8190
70
Report #2
A comparison between the average current salary and the average new salary for the domestic and international pilots.
71
An example of the desired report:
The MEANS Procedure N Category Obs Variable N Mean Std Dev Minimum Maximum ---------------------------------------------------------------------------DOM 19 Salary 19 68405.79 11439.29 44980.00 79760.00 NewSalary 19 72550.12 12562.02 47229.00 85342.20
INT
Salary 31 99252.55 21019.22 67340.00 125250.00 NewSalary 31 107365.26 23700.21 72052.80 136522.50 ----------------------------------------------------------------------------
31
72
Report #3
A breakdown of the percentage of domestic pilots versus international pilots.
73
An example of the desired report:
The FREQ Procedure Cumulative Cumulative Category Frequency Percent Frequency Percent ------------------------------------------------------------DOM 19 38.00 19 38.00 INT 31 62.00 50 100.00
74
Outline the Programming Requirements
Output Input
Report/File Processing
75
... ...
Output
The management of International Airlines needs three reports.
Report #1
Report #2
Report #3
76
Input
Partial listing of PILOT raw data file:
E01046DAVID E01682VICTOR E02659CLIFTON G. E04042SAMUEL E04732CHRISTIAN CHAPMAN TAILOR WILDER BENNETT EDMINSTON PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 72660DOM 44980DOM 53630DOM 52870DOM 76120DOM
. .
. .
. .
. .
.. ..
77
Input
The 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).
78
Processing
Two new variables need to be created.
Employee First Last ID Name Name
Job Code
Salary
Category
Bonus
New Salary
PILOT raw data file
Create
79
... ...
Outline the Programming Requirements
Chapter 4:
Chapter 5: Chapter 6: Chapter 7: Chapter 8: Chapter 9:
Read PILOT Raw Data File to Create SAS Data Set (DATA Step) Navigate the SAS Windowing Environment Create a List Report of SAS Data Set (PROC Step) Modify SAS Data Set to Include Bonus Variable (DATA Step) Modify SAS Data Set to Include New Salary Variable (DATA Step) Create Statistical Reports of SAS Data Set (PROC Step)
80
Section 2.3
Defining the Exercise Scenario
Objectives
Define the business need for the exercise scenario. Outline the programming requirements for the exercise scenario.
82
Define the Business Need
Cargo rates and passenger fares at International Airlines will increase on all flights.
The cargo rates will increase 50 cents per pound on all flights.
The passenger fares will increase as follows: 8% if a short-range flight 10% if a medium-range flight 12% if a long-range flight.
83
... ...
Define the Business Need
The management of International Airlines needs three reports.
Report #1
Report #2
Report #3
84
Report #1
A listing of all the flights with the new cargo rate.
85
Partial example of the desired report:
New Cargo Rate 5.77 5.77 4.36 4.65 5.15 5.60 5.60 4.36 5.15 4.65 2.65 2.32 2.33 2.76 2.22
Obs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Origination FRA CPT RDU SYD SFO LHR JNB FRA HND HKG RDU SFO PEK JRS FRA
Destination CPT FRA FRA HKG HND JNB LHR RDU SFO SYD ANC ANC CCU DEL DXB
Range LONG LONG LONG LONG LONG LONG LONG LONG LONG LONG MEDIUM MEDIUM MEDIUM MEDIUM MEDIUM
Category INT INT INT INT INT INT INT INT INT INT DOM DOM INT INT INT
86
Report #2
A comparison between the average current passenger fare and the average new passenger fare for the domestic and international flights.
87
An example of the desired report:
The MEANS Procedure N Category Obs Variable N Mean Std Dev Minimum Maximum ----------------------------------------------------------------------------DOM 48 PassengerFare 48 219.3333 179.4964 30.0000 682.0000 NewPassengerFare 48 239.6767 198.2444 32.4000 750.2000 INT PassengerFare 64 375.5000 336.1152 40.0000 1142.0000 NewPassengerFare 64 414.0119 376.7187 42.2000 1279.0400 ----------------------------------------------------------------------------64
88
Report #3
A breakdown of the percentage of domestic flights versus international flights.
89
An example of the desired report:
The FREQ Procedure Cumulative Cumulative Category Frequency Percent Frequency Percent ------------------------------------------------------------DOM 48 42.86 48 42.86 INT 64 57.14 112 100.00
90
Outline the Programming Requirements
Output Input
Report/File Processing
91
... ...
Output
The management of International Airlines needs three reports.
Report #1
Report #2
Report #3
92
Input
Partial listing of RATES raw data file:
FRA CPT RDU SYD SFO . . CPT FRA FRA HKG HND . . LONG LONG LONG LONG LONG . . 5.27 5.27 2.86 4.15 4.65 . . 1142.00 1142.00 836.00 898.00 1007.00 . . INT INT INT INT INT . .
93
Input
The 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 for International).
94
Processing
Two new variables need to be created.
Origination
Destination
Range
Cargo Rate
Passenger Fare
Category
New Cargo Rate
New Passenger Fare
RATES raw data file
Create
95
... ...
Outline the Programming Requirements
Chapter 4:
Chapter 5: Chapter 6: Chapter 7: Chapter 8: Chapter 9:
Read RATES Raw Data File to Create SAS Data Set (DATA Step) Navigate the SAS Windowing Environment Create a List Report of SAS Data Set (PROC Step) Modify SAS Data Set to Include New Cargo Rate Variable (DATA Step) Modify SAS Data Set to Include New Passenger Fare Variable (DATA Step) Create Statistical Reports of SAS Data Set (PROC Step)
96
Chapter 3
Reading a Raw Data File with the DATA Step
Section 3.1
Introduction
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.
99
Why Create a SAS Data Set?
To create a report, the data must be in the form of a SAS data set.
100
... ...
Why Use a DATA Step?
Raw Data File
SAS Data Set
Descriptor
DATA Step
101
... ...
Section 3.2
Planning to Read a Raw Data File
Objectives
Plan to read a raw data file to create a SAS data set.
103
Requirements to Read a Raw Data File
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).
104
Name the Output SAS Data Set
SAS 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, or underscores are not case sensitive.
105
Name the Output SAS Data Set
Valid Data Set Names monthly_totals _1980_Sales_Data R_and_D DollarAmount Invalid Data Set Names monthly totals 1980_Sales_Data R&D $Amount
106
Name the Output SAS Data Set
Operating Environment Windows UNIX OS/390 Data Set Name
pilotdata pilotdata pilotdata
107
... ...
Locate and Name the Input Raw Data File
Operating Environment Windows UNIX OS/390 File Location and Name C:\workshop\winsas\basic\pilot.dat /users/edu##/pilot.dat edu###.basic.sascode(pilot)
108
Examine the Raw Data File and File Layout
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 . . . . .. . . . . .. . . . . ..
109
Examine Raw Data File and File Layout
File Layout of PILOT raw data file:
Field Name Employee ID First Name Last Name Job Code Salary Category Field Location 1-6 7 - 19 20 - 34 35 - 41 42 - 47 48 - 50
110
Determine Location of Fields
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
Salary Job Code Category
111
... ...
Determine Location of Fields
Field Name Employee ID First Name Last Name Job Code Salary Category Field Location 1-6 7 - 19 20 - 34 35 - 41 42 - 47 48 - 50 Start Position End Position
1 7 20 35 42 48
6 19 34 41 47 50
... ...
112
Give SAS Variable Names to Fields
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.
113
Give SAS Variable Names to Fields
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, or underscores are not case sensitive must be unique within a SAS data set.
114
Give SAS Variable Names to Fields
Valid Variable Names social_security_number _2005_YearlyReturns day5 PercentIncrease Invalid Variable Names social security number 2005_YearlyReturns day#5 %Increase
115
Give SAS Variable Names to Fields
Field Name Employee ID First Name Last Name Job Code Salary Category SAS Variable Name
EmployeeID FirstName
LastName
JobCode Salary Category
... ...
116
Determine Variable Type
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.
117
Determine Variable Type
A variable is considered to be character if it contains any combination of the following: letters (A - Z, a - z) numbers (0-9) special characters (!, @, #, %, and so on).
118
Determine Variable Type
Examples of valid character variable values are Ms. Helen Jones 648 Pine St. Research & Development $21,756 389.
119
Determine Variable Type
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.
120
Determine Variable Type
Examples of valid numeric variable values are 175 4856281 -30 29.92 2.1E6.
121
Determine Variable Type
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 . . . . .. . . . . .. . . . . ..
122
Determine Variable Type
Field Name Field Location Variable Type Character Character
Employee ID
First Name Last Name Job Code Salary Category
1 6
7 - 19 20 - 34 35 - 41 42 - 47 48 - 50
Character
Character
Numeric
Character
123
Exercises
This exercise reinforces the concepts discussed previously.
124
Planning to Read a Raw Data File
Based on the Exercise Scenario, plan to read the raw data file. Refer to the Exercise Scenario Handout for details 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).
125
Planning to Read a Raw Data File
Name the output SAS data set.
Windows UNIX OS/390 ratedata ratedata ratedata
126
... ...
Planning to Read a Raw Data File
Name the input raw data file, including the full path name based on your operating environment.
Windows UNIX
C:\workshop\winsas\basic\rates.dat /users/edu##/rates.dat edu###.basic.sascode(rates)
OS/390
127
... ...
Planning to Read a Raw Data File
Field Name Variable Name
Origination
Destination
Origination
Destination Range CargoRate PassengerFare Category
Flight Range
Cargo Rate Passenger Fare Flight Category
128
... ...
Planning to Read a Raw Data File
Field Name Origination Destination Flight Range Cargo Rate Passenger Fare Flight Category Start Position End Position
1 6 12 20
8
17 23 34 40
28
38
129
... ...
Planning to Read a Raw Data File
Field Name Origination Destination Flight Range Cargo Rate Passenger Fare Flight Category Variable Type
Character
Character Character Numeric
Numeric
Character
... ...
130
Section 3.3
Coding to Read a Raw Data File
Objectives
Write program code to read a raw data file and create a SAS data set.
132
Purpose of the DATA Step
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.
133
The DATA Step
Start
End
data _______________ ; _______________ ; . . . _______________ ; run;
134
... ...
The DATA Step
data _______________ ; infile ________ ; input _________ ; . . . run;
135
The DATA Statement
The DATA statement names the SAS data set being created and signals the beginning of the DATA step. General form of the DATA statement:
DATA SAS_data_set_name; Example: data pilotdata;
136
The INFILE Statement
The INFILE statement names the raw data file to be read. General form of the INFILE statement:
INFILE 'input-raw-data-file';
137
The INFILE Statement
Example:
Operating Environment Windows UNIX OS/390 INFILE Statement infile 'C:\workshop\winsas\basic\pilot.dat'; infile '/users/edu##/pilot.dat'; infile 'edu###.basic.sascode(pilot)';
138
The INPUT Statement
The INPUT statement describes the fields in each raw data 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.
139
The INPUT Statement
General form of the INPUT statement:
INPUT variable $ start - end . . . ;
variable $ start end is a valid SAS variable name. indicates a character variable. identifies the starting position. identifies the ending position.
140
The INPUT Statement
Example:
input EmployeeID FirstName LastName JobCode Salary Category $ 1 $ 7 $ 20 $ 35 42 $ 48 6 19 34 41 47 50;
141
The INPUT Statement
This way of describing the input raw data record to SAS is called 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 the same position in every record of the file.
142
The RUN Statement
The RUN statement signals the end of a step.
General form of the RUN statement: RUN;
143
The DATA Step
General form for the complete DATA step:
DATA SAS_data_set_name; INFILE 'input-raw-data-file'; INPUT variable $ start - end . . .; RUN;
144
The DATA Step - Windows
Based on the International Airlines lecture scenario, the code for the complete DATA step is data pilotdata; infile 'c:\workshop\winsas\basic\pilot.dat'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; run;
145
Exercises
This exercise reinforces the concepts discussed previously.
146
Coding to Read a Raw Data File
Draw lines connecting the appropriate boxes. Based on your previous Planning to Read a Raw Data File exercise, Write the DATA statement. Write the INFILE statement. Write the INPUT statement. Write the RUN statement.
147
Coding to Read a Raw Data File
1st 1st Statement Statement
INFILE
Signals end of the DATA step. Names raw data file to be read.
2nd Statement
3rd Statement 4th Statement
RUN
DATA INPUT
Describes fields in each record.
Names data set being created.
... ...
148
Coding to Read a Raw Data File
Windows Solution:
data ratedata; infile 'c:\workshop\winsas\basic\rates.dat'; input Origination $ 1 - 3 Destination $ 6 - 8 Range $ 12 - 17 CargoRate 20 - 23 PassengerFare 28 - 34 Category $ 38 - 40; run;
149
Chapter 4
Navigating the SAS Windowing Environment
Section 4.1
Introduction
Objectives
Enter SAS program code in the SAS windowing environment and execute the program. Modify SAS program code and execute the program. Save and retrieve a SAS program.
152
Section 4.2
Entering and Executing SAS Code
Objectives
Enter SAS program code in the SAS windowing environment and execute the program.
154
Enter the SAS Program Code
After the planning and coding effort for a SAS program is complete, the SAS program code must be entered into the computer to process and to test the program.
155
Program Execution Mode
SAS programs can be processed in one of two modes: windowing batch.
156
Windowing Mode
Windowing mode is a facility that enables you to enter and execute SAS programs and view the results in an interactive environment. An interactive environment permits the program to be processed immediately when submitted for execution.
157
Batch Mode
Batch mode is used to run SAS programs in the background by submitting them to the operating system for batch execution. Batch execution enables the program to be processed when the computer has resources available, not necessarily at the moment when submitted for execution.
158
The SAS Windowing Environment
SAS software provides the SAS windowing environment, an interactive environment that enables the entry and execution of SAS program code.
159
The SAS Windowing Environment
The SAS windowing environment is made up of a collection of windows. There are three primary windows in the windowing environment.
1
... ...
160
The Program Editor Window
The Program Editor window enables SAS program code to be
entered from the keyboard submitted for execution.
161
... ...
The Log Window
The Log window displays
the SAS program code submitted for execution
messages from SAS indicating the status of the program execution.
162
... ...
The Output Window
The Output window displays reports generated by the SAS program.
163
... ...
Commands
Commands are used to navigate among the various windows of the SAS windowing environment and are used to execute a program.
Depending upon the operating environment, commands can be issued by...
continued...
164
... ...
Selecting from pull-down menus
Typing the command
Clicking on a tool button
Using function keys (F1 - F12)
165
... ...
Entering and Executing SAS Code
This demonstration illustrates entering SAS program code into the SAS windowing environment and executing the program.
166
Section 4.3
Editing SAS Code
Objectives
Modify SAS program code and execute the program.
168
Editing SAS Code
After the SAS program code is entered into the Program Editor window and executed, there might be a need to modify, or edit, the code because of program errors program specification changes the need to add additional code.
169
Program Errors
A program might not run successfully, or at all, due to several different kinds of errors: typographical errors syntax errors logical errors.
170
Program Specification Changes
The purpose or function of a program may change due to insufficient analysis project evolution user changes.
171
The Need to Add Additional Code
A program can be written in stages. Programmers often test their code in stages. They know that they will add statements or steps to the program after each stage works correctly.
172
Debugging and Editing SAS Code
Debugging and editing program code is an iterative process.
Code Modification
Results Verification
Program Testing
173
... ...
Editing SAS Code
This demonstration illustrates editing SAS program code in the SAS Program Editor and executing the program.
174
Exercises
This exercise reinforces the concepts discussed previously.
175
Editing SAS Code
Enter the DATA step code from your previous Coding 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.
176
Section 4.4
Saving and Retrieving SAS Code
Objectives
Save and retrieve a SAS program.
178
Saving and Retrieving SAS Code
After a program is entered, tested, and the results verified, the program code can be saved for future use retrieved at a later time.
179
Saving SAS Program Code
Program code is saved in program files because it can be rerun later without being re-entered copied to be modified into a new program shared with other people printed for documentation.
180
Retrieving SAS Program Code
Program code that has been saved in a program file can be retrieved into the Program Editor where it can be rerun modified.
181
Saving SAS Program Code
Program File
Stored SAS Program Code
182
... ...
Retrieving SAS Program Code
Program File
Stored SAS Program Code
183
... ...
Saving and Retrieving SAS Code
This demonstration illustrates saving SAS program code from the Program Editor and retrieving code into the Program Editor.
184
Exercises
This exercise reinforces the concepts discussed previously.
185
Saving and Retrieving SAS Code
Recall the DATA step code from your previous 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.
186
Section 4.5
Additional Topics (Optional)
Objectives
Copy, move, delete, and insert text. Clear the contents of a window. Navigate windows. Correct unbalanced quotation marks.
188
Chapter 5
Creating a List Report with a PROC Step
Section 5.1
Introduction
Objectives
Plan to create a list report based on a SAS data set. Code to create a list report based on a SAS data set.
191
Why Create a List Report?
List reports are created to view the data in a SAS data set.
192
... ...
Definition of a List Report
A list report shows the data in a SAS data set displays one line for each observation in the SAS data set displays all variables or only those specified.
193
Example of a List Report
Obs 1 2 3 4 5 6 7 8 Employee ID E01046 E01682 E02659 E04042 E04732 E03740 E03389 E04688 FirstName DAVID VICTOR CLIFTON G. SAMUEL CHRISTIAN CRAIG N. LOUISE JOHN D. LastName CHAPMAN TAILOR WILDER BENNETT EDMINSTON SAWYER STAINES PERRY JobCode PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1
194
Why Use a PROC Step?
SAS Data Set
Descriptor
Report
PROC Step
195
... ...
Section 5.2
Planning to Create a List Report
Objectives
Plan to create a list report based on a SAS data set.
197
Partial List Output
Obs 1 2 3 4 5 6 7 Employee ID E01046 E01682 E02659 E04042 E04732 E03740 E03389 FirstName DAVID VICTOR CLIFTON G. SAMUEL CHRISTIAN CRAIG N. LOUISE LastName CHAPMAN TAILOR WILDER BENNETT EDMINSTON SAWYER STAINES Job Code PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1
198
Requirements to Create a List Report
To create a list report, 1. name the SAS data set to be viewed 2. determine the variables to be displayed 2. specify the desired order of the variables.
199
DATA Step
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; run;
200
Name the SAS Data Set to be Viewed
Operating Environment Windows UNIX OS/390 SAS Data Set Name pilotdata pilotdata pilotdata
201
Determine the Variables to be Displayed
SAS Variable Name EmployeeID FirstName LastName JobCode Salary Category
... ...
202
Specify the Desired Order of the Variables
EmployeeID
FirstName
LastName
JobCode
203
... ...
Exercises
This exercise reinforces the concepts discussed previously.
204
Planning to Create a List Report
Based on your program called ch5ex2 or the program called ch6ex, plan to create a list report. The desired list report is Report #1 of the Exercise Scenario, except do not include the NewCargoRate variable.
205
Planning to Create a List Report
Name the SAS data set to be viewed.
ratedata
Name the variables to be displayed.
Origination Destination Range Category
206
... ...
Planning to Create a List Report
Specify the desired order of the variables.
1st variable: 2nd variable:
3rd variable:
4th variable:
Origination Destination Range Category
207
... ...
Section 5.3
Coding to Create a List Report
Objectives
Code to create a list report based on a SAS data set.
209
Choosing the PROC PRINT Step
PROC Step(s) PRINT ...
MEANS
FREQ
210
The PROC PRINT Step
Start proc print _________ ; _______________ ; . . . _______________ ; run;
End
211
... ...
The PROC PRINT Step
The PROC PRINT statement names the SAS data set being displayed in the list report. General form of the PROC PRINT statement:
PROC PRINT DATA = SAS_data_set_name; The RUN statement signals the end of the step. General form of the RUN statement: RUN;
212
The PROC PRINT Step
Based on the International Airlines lecture scenario, the code for the complete PROC PRINT step is
proc print data = pilotdata; run;
213
The PROC PRINT Step
This demonstration illustrates using the PROC PRINT statement and the RUN statement.
214
The VAR Statement
The VAR statement can be added to the PROC PRINT step. The PROC PRINT step then consists of the following statements: the PROC PRINT statement the VAR statement the RUN statement.
215
The VAR Statement
The VAR statement names the variables to be displayed and specifies the desired order of the variables. General form of the VAR statement:
VAR variable . . . ; Example: var EmployeeID FirstName LastName JobCode;
216
The VAR Statement
Based on the International Airlines lecture scenario, the code for the complete PROC PRINT step is proc print data = pilotdata; var EmployeeID FirstName LastName JobCode; run;
217
The VAR Statement
This demonstration illustrates using the PROC PRINT statement, the VAR statement, and the RUN statement.
218
Exercises
This exercise reinforces the concepts discussed previously.
219
Coding to Create a List Report
Draw lines connecting the appropriate boxes. Based on your previous Planning to Create a List Report exercise: write the PROC PRINT statement write the VAR statement write the RUN statement. Retrieve your program named ch5ex2 or the program called ch6ex. Add the PROC PRINT step to the bottom of the program. Save the code to a program file named ch6ex2.
220
Coding to Create a List Report
1st 1st Statement Statement
VAR
Signals Signals end end of of PROC PROC step step
2nd 2nd Statement
RUN
Names SAS data set to display in report Names variables to be displayed
3rd 3rd Statement
PROC
221
...
Chapter 6
Creating a Variable with the DATA Step
Section 6.1
Introduction
Objectives
Plan to create a variable in a SAS data set. Code to create a variable in a SAS data set.
224
Why Create a Variable?
Variables are created to make available information not currently contained in a SAS data set.
225
... ...
Why Create a Variable?
Obs Employee ID FirstName LastName JobCode
1 2 3 4 5 6 7
E01046 E01682 E02659 E04042 E04732 E03740 E03389
DAVID VICTOR CLIFTON G. SAMUEL CHRISTIAN CRAIG N. LOUISE
CHAPMAN TAILOR WILDER BENNETT EDMINSTON SAWYER STAINES
PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1
226
Why Create a Variable?
Employee ID E01046 E01682 E02659 E04042 E04732 E03740 E03389
Obs 1 2 3 4 5 6 7
FirstName DAVID VICTOR CLIFTON G. SAMUEL CHRISTIAN CRAIG N. LOUISE
LastName CHAPMAN TAILOR WILDER BENNETT EDMINSTON SAWYER STAINES
JobCode PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1
Bonus 7266 4498 5363 5287 7612 6228 7439
227
Why Use a DATA Step?
Raw Data File
SAS Data Set
Descriptor DATA Step
228
... ...
Section 6.2
Planning to Create a Variable
Objectives
Plan to create a variable in a SAS data set.
230
Partial List Report
Obs 1 2 3 4 5 6 7 Employee ID E01046 E01682 E02659 E04042 E04732 E03740 E03389 FirstName DAVID VICTOR CLIFTON G. SAMUEL CHRISTIAN CRAIG N. LOUISE LastName CHAPMAN TAILOR WILDER BENNETT EDMINSTON SAWYER STAINES JobCode PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 PILOT1 Bonus 7266 4498 5363 5287 7612 6228 7439
231
Requirements to Create a Variable
1. Specify the DATA step that defines the SAS data set. 2. Define the variable.
232
Specify the DATA Step that Defines the SAS Data Set
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; run;
233
Define the Variable
To define a variable, 1. name the new variable 2. determine the expression.
234
Define the Variable
EVALUATE new_variable_name = expression
value
ASSIGN
value
235
... ...
Name the New Variable
Because the variable being created represents a bonus of 10% of each pilots current salary, a reasonable variable name is
Bonus
236
... ...
Determine the Expression
The expression can be any valid combination of constants variables operators parentheses.
237
Constants
Constants can be of two types: numeric character.
238
Numeric Constants
A numeric constant is a valid numerical value. 4922 -728 2.1415926 -0.56 1.86E05
239
Numeric Constants - Examples
MonthlyAmount = 4922 rev_difference = -728 pi = 3.1415926 PercentChange = -0.56 speed_of_light = 1.86E05
240
Character Constants
A character constant is a string of text enclosed in matching quotes. Wednesday 123 Main Street INSTRUCTOR % of revenue 7251
241
Character Constants - Examples
DayOfWeek = Wednesday address = 123 Main Street employee_title = INSTRUCTOR column_definition = % of revenue EmployeeNumber = 7251
242
Variables
A variable referenced in an expression uses the value of that variable. SocialSecurityNumber _2005_YearlyReturns day5 PercentIncrease last_name
243
Variables - Examples
ssn = social_security_number last_tax_data = _2005_YearlyReturns DailyRevenue = day5 pct_inc = PercentIncrease LastName = last_name
244
Operators and Parentheses
Symbol + * / ** ( ) Definition Addition Subtraction Multiplication Division Exponentiation Grouping
245
Operators and Parentheses
An expression can include operators and parentheses, along with numeric constants and variables. price * quantity GrossPay - tax x**2 (jan_amt + feb_amt + mar_amt) / 3 CurrentRate + 10.27
246
Operators and Parentheses - Examples
TotalSales = price * quantity net_pay = gross_pay - tax squared = x**2 avg = (jan_amt + feb_amt + mar_amt) / 3 NewRate = CurrentRate + 10.27
247
Determine the Expression
The expression that represents a 10% bonus for the pilots is Salary * 0.10
248
... ...
Define the Variable
Bonus = Salary * 0.10
249
... ...
Exercises
This exercise reinforces the concepts discussed previously.
250
Planning to Create a Variable
Fill in the blanks and circle the correct answers for questions 1.a through 1.h. Based on your program named CH6EX2 or the program named CH7EX, plan to create a variable. The variable that needs to be created will represent a 50 cents per pound increase in cargo rate. A list report needs to be displayed after you create the variable. The desired report is Report #1 of the Exercise Scenario.
251
Planning to Create a Variable
a. The two steps to define a new variable are name the new variable
determine the expression
252
... ...
Planning to Create a Variable
b. The two types of constants are character
numeric
253
... ...
Planning to Create a Variable
c. Place the letter corresponding to the component in its proper place in the diagram.
A expression B variable name
B A ____________ = ____________
254
... ...
Planning to Create a Variable
d. Circle the valid variable names below. 1) dept10 2) division_revenue 3) 1040_data 4) Sales&Marketing 5) number_of_participants 6) 1040-data
255
... ...
Planning to Create a Variable
e. An expression can include constants, variables, operators, and/or parentheses.
256
... ...
Planning to Create a Variable
f. Place the letter corresponding to the expression component in the blank preceding each example. Avariable Bcharacter constant Cnumeric constant
C __________ B __________ A __________ B __________ 7528 10920 Sanderson Ave. yearly_sales
999-99-9999
257
... ...
Planning to Create a Variable
Avariable Bcharacter constant Cnumeric constant
B __________ A __________
Boston gross_profit 29.92 29.92
C __________
B __________
258
... ...
Planning to Create a Variable
g. Place the letter corresponding to the expression component in front of its name. CurrentRate + 15.28 A B C
C __________ A __________ __________ B constant variable operator
259
... ...
Planning to Create a Variable
h. Circle the valid operators for an expression from the list below.
1) 2) 3) 4) 5) * / # ** + 6) 7) 8) 9) % & =
260
... ...
Planning to Create a Variable
i. Name the variable to create that will represent a 50 cents per pound increase in cargo rate. NewCargoRate
Name the numeric constant that is needed in the expression. 0.50
261
... ...
Planning to Create a Variable
Name the existing variable that is needed in the expression. CargoRate
Name the operator that is needed in the expression.
262
... ...
Planning to Create a Variable
Write the expression.
CargoRate + 0.50 Complete the following diagram. NewCargoRate = ________________ CargoRate + 0.50 ______________
263
... ...
Section 6.3
Coding to Create a Variable
Objectives
Code to create a variable in a SAS data set.
265
The DATA Step
As a starting point for this section, the DATA step consists of the following statements: the DATA statement the INFILE statement the INPUT statement the RUN statement.
266
The DATA Step
An assignment statement can be added to the DATA step. The DATA step then consists of the following statements: the DATA statement the INFILE statement the INPUT statement the assignment statement the RUN statement.
267
The Assignment Statement
The assignment statement defines a variable being created. General form of the assignment statement:
variable = expression; Example:
Bonus = Salary * 0.10;
Remember the semicolon.
268
... ...
The DATA Step
Based on the International Airlines lecture scenario, the code for the complete DATA step is
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; Bonus = Salary * 0.10; run;
269
... ...
Executing the Assignment Statement
Bonus
Salary
0.10;
EmployeeID
FirstName
Salary
Bonus
4268
ANGELA
...
52000
270
... ...
Executing the Assignment Statement
EVALUATE Bonus = 52000 * 0.10
5200
EmployeeID FirstName
5200
Salary Bonus
4268
ANGELA
...
52000
5200
271
The Assignment Statement
This demonstration illustrates using the assignment statement in an existing SAS program.
272
Exercises
This exercise reinforces the concepts discussed previously.
273
Coding to Create a Variable
Based on your previous Planning to Create a Variable exercise: 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.
274
Chapter 7
Performing Conditional Logic with the DATA Step
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 variable in a SAS data set.
277
Why Use Conditional Logic?
Conditional logic is used to create values for a variable based on whether a situation is true or false.
take yo , ur g in
a pair o e k f a t
nglasse s su .
it is rain f I
erwise, h t
bre l l a . um
278
... ...
Why Use Conditional Logic?
Obs
1 2 11 12 Employee ID LastName E01046 E01682
JobCode
Salary
New Salary
36 37
CHAPMAN PILOT1 72660 TAILOR PILOT1 44980 A new needs . variable . . to be PILOT2 created, E01642 MCELROY 78260 representing increase in 74620 salary. E04348 CARTER an PILOT2 . . . E03728 MASON PILOT3 116510 E03892 LEE PILOT3 112690
279
... ...
Why Use Conditional Logic?
Obs 1 2 11 12 36 37 Employee ID E01046 E01682 E01642 E04348 E03728 E03892 LastName JobCode Salary 72660 5% Increase 44980 78260 7% Increase 74620 116510 9% Increase 112690 New Salary 76293 47229 83738 79843 126996 122832
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
280
... ...
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.
282
Partial List Report
Obs 1 2 11 12 36 37 Employee ID E01046 E01682 E01642 E04348 E03728 E03892 LastName JobCode Salary 72660 44980 78260 74620 116510 112690
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
283
Partial List Report
Obs 1 2 11 12 36 37 Employee ID LastName E01046 E01682 E01642 E04348 E03728 E03892 JobCode Salary 72660 5% Increase 44980 78260 7% 74620 Increase 116510 9% 112690 Increase New Salary 76293 47229
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
83738
79843 126996
122832
284
Requirements to Perform Conditional Logic
Specify the DATA step that defines the SAS data set. Determine the condition. Determine the action.
285
Specify the DATA Step that Defines the SAS Data Set
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; Bonus = Salary * 0.10; run;
286
Determine the Condition
The condition is an expression that can be evaluated as either true or false.
Example: If it is raining, take your umbrella.
287
... ...
Determine the Condition
The condition can be expressed as any valid combination of constants variables operator and/or parentheses.
Example: variable = constant
288
Determine the Condition
Obs 1 2 11 12 36 37 Employee ID LastName E01046 E01682 E01642 E04348 E03728 E03892 JobCode Salary 72660 5% Increase 44980 78260 7% 74620 Increase 116510 9% Increase 112690 New Salary 76293 47229 83738 79843 126996 122832
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
289
... ...
Determine the Condition
variable = constant
JobCode JobCode
= 'PILOT1' = 'PILOT2'
Character values must be enclosed in quotes and match the case of the data.
JobCode
= 'PILOT3'
290
... ...
Determine the Action
The action is what SAS should do when the condition is true.
Example: If it is raining,
take your umbrella.
291
... ...
Determine the Action
The action can involve creating a new variable. Example: new_variable = expression
292
Determine the Action
Obs 1 2 11 12 36 37 Employee ID LastName E01046 E01682 E01642 E04348 E03728 E03892 JobCode Salary 72660 5% Increase 44980 78260 7% Increase 74620 116510 9% Increase 112690 New Salary 76293 47229 83738 79843 126996 122832
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
293
... ...
Determine the Action
new_variable = expression
NewSalary NewSalary
= Salary * 1.05 = Salary * 1.07
NewSalary
= Salary * 1.09
294
... ...
Exercises
This exercise reinforces the concepts discussed previously.
295
Planning to Perform Conditional Logic to Create a Variable
Based on your program named CH7EX2 or the program named CH8EX, plan to perform conditional logic to create a variable. The variable that needs to be created will represent an increase in passenger fare. The passenger fares will increase 8% for a short-range flight, 10% for a mediumrange flight, and 12% for a long-range flight. A list report needs to be displayed after creating the variable.
296
Planning to Perform Conditional Logic to Create a Variable
Determine the condition.
Range = 'SHORT' Range = 'MEDIUM' Range = 'LONG' Determine the action. NewPassengerFare = PassengerFare * 1.08 NewPassengerFare = PassengerFare * 1.10 NewPassengerFare = PassengerFare * 1.12
297
... ...
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.
299
The DATA Step
As a starting point for this section, the DATA step consists of the following statements: the DATA statement the INFILE statement the INPUT statement the assignment statement the RUN statement.
300
The DATA Step
Conditional statements can be added. The DATA step then consists of the following statements: the DATA statement the INFILE statement the INPUT statement the assignment statement conditional statements the RUN statement.
301
Conditional Statements
Conditional statements can create values for a new variable based on whether a condition is true or false. Conditional statements can be expressed in the form of IF-THEN statements. General form of the IF-THEN statement:
IF condition THEN action;
302
Conditional Statements
Example:
if job_code = PILOT1 then condition new_salary action = salary * 1.05; if job_code = PILOT2 then condition new_salary action = salary * 1.07; if job_code = PILOT3 then condition new_salary action = salary * 1.09;
303
Conditional Statements
Example:
if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; action if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; action if JobCode = 'PILOT3' then NewSalary = Salary * 1.09; action
304
Conditional Statements
Example:
if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; if JobCode = 'PILOT3' then NewSalary = Salary * 1.09;
305
TRUE
condition
EVALUATED new_variable value = expression value
ASSIGNED
306
... ...
FALSE condition new_variable = BYPASSED expression
next statement EXECUTED
307
... ...
Conditional Statements
In a series of related IF-THEN statements, each statement is processed even if a prior IF-THEN statement is true.
if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; if JobCode = 'PILOT3' then NewSalary = Salary * 1.09;
308
Conditional Statements
EmployeeID E01046 LastName CHAPMAN JobCode PILOT1 Salary 72660
True False False
if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; if JobCode = 'PILOT3' then NewSalary = Salary * 1.09;
309
... ...
Conditional Statements
A more efficient approach uses an ELSE IF-THEN sequence of statements. After a condition is true, no further statements in the series are processed. General form of the ELSE IF-THEN statement:
IF condition THEN action; ELSE IF condition THEN action; ...
310
Conditional Statements
Example:
if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; else if JobCode = 'PILOT2' then NewSalary = Salary * 1.07;
else if JobCode = 'PILOT3' then NewSalary = Salary * 1.09;
311
Conditional Statements
EmployeeID E01046 LastName CHAPMAN JobCode PILOT1 Salary 72660
True
if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; else if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; else if JobCode = 'PILOT3' then NewSalary = Salary * 1.09;
The rest of the series is skipped.
312
... ...
The DATA Step
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; Bonus = Salary * 0.10; if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; else if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; else if JobCode = 'PILOT3' then NewSalary = Salary * 1.09; run;
313
... ...
Conditional Statements
This demonstration illustrates using IF/THEN statement and ELSE IF/THEN statements in an existing SAS program.
314
Exercises
This exercise reinforces the concepts discussed previously.
315
Coding to Perform Conditional Logic to Create a Variable
Based on your previous Planning to Perform Conditional Logic to Create a Variable exercise: Write the three conditional statements. Retrieve your program named CH7EX2 or the program named CH8EX. Add the conditional statements to the program after the assignment statement. Save the code to a program named CH8EX2.
316
Chapter 8
Creating Statistical Reports with PROC Steps
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 a SAS data set. Plan to create a frequency report based on a SAS data set. Code to create a frequency report based on a SAS data set.
319
Why Create a Statistical Report?
You create a statistical report to display statistical information that is not available in a list report.
320
... ...
A SAS Statistical Report
A statistical report displays statistics calculated based on the SAS data set consolidates data from the SAS data set represents multiple observations from the SAS data set in each line of the report may be either summary frequency.
321
A SAS Summary Report
A SAS summary report produces simple descriptive statistics for numeric variables calculates descriptive statistics including number of nonmissing values mean standard deviation minimum value maximum value.
322
Example of a SAS Summary Report
The MEANS Procedure N Category Obs Variable N Mean Std Dev Minimum Maximum ____________________________________________________________________ DOM 19 Salary 19 68405.79 11439.29 44980.00 79760.00 NewSalary 19 72550.12 12562.02 47229.00 85342.20 INT 31 Salary 31 99252.55 21019.22 67340.00 125250.00 NewSalary 31 107365.26 23700.21 72052.80 136522.50 ____________________________________________________________________
323
A SAS Frequency Report
A SAS frequency report produces simple statistics that count how often individual values occur within a SAS data set calculates statistics including frequency percent cumulative frequency cumulative percent.
324
Example of a SAS Frequency Report
The FREQ Procedure Cumulative Cumulative Category Frequency Percent Frequency Percent -----------------------------------------------------DOM 19 38.00 19 38.00 INT 31 62.00 50 100.00
325
Section 8.2
Planning to Create a Summary Report
Objectives
Plan to create a summary report based on a SAS data set.
327
Example of a SAS Summary Report
The MEANS Procedure N Category Obs Variable N Mean Std Dev Minimum Maximum ____________________________________________________________________ DOM 19 Salary 19 68405.79 11439.29 44980.00 79760.00 NewSalary 19 72550.12 12562.02 47229.00 85342.20 INT 31 Salary 31 99252.55 21019.22 67340.00 125250.00 NewSalary 31 107365.26 23700.21 72052.80 136522.50 _____________________________________________________________________
328
Requirements to Create a SAS Summary Report
1. Name the SAS data set to be referenced. 2. Determine the variables on which to calculate the statistics. 2. Determine the variable by which to group the data.
329
DATA Step Example
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; Bonus = Salary * 0.10; if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; else if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; else if JobCode = 'PILOT3' then NewSalary = Salary * 1.09; run;
330
Name the SAS Data Set to Be Referenced
Operating Environment Windows UNIX OS/390 SAS Data Set Name pilotdata pilotdata pilotdata
331
Determine the Variables on Which to Calculate the Statistics
SAS Variable Name EmployeeID FirstName LastName JobCode Salary Category Bonus NewSalary
332
... ...
Determine the Variable by Which to Group the Data
SAS Variable Name EmployeeID FirstName LastName JobCode Salary Category Bonus NewSalary
333
... ...
Exercises
This exercise reinforces the concepts discussed previously.
334
Planning to Create a Summary Report
Based on your program named CH8EX2 or the program named CH9EX, plan to create a summary report. The desired summary report is a comparison between the average current passenger fare and the average new passenger fare for the domestic and international flights. This is Report #2 of the exercise scenario.
335
Planning to Create a Summary Report
Name the SAS data set to be referenced.
ratedata Name the variables on which to calculate the descriptive statistics.
PassengerFare
NewPassengerFare
336
... ...
Planning to Create a Summary Report
Name the variable by which to group the data. Category
337
... ...
Section 8.3
Coding to Create a Summary Report
Objectives
Code to create a summary report based on a SAS data set.
339
Choosing the PROC MEANS Step
PROC Step(s) PRINT ...
MEANS
FREQ
340
The PROC MEANS Step
Start
proc means _________ ; _______________ ; . . . _______________ ; run;
End
341
... ...
The PROC MEANS Step
The PROC MEANS statement names the SAS data set displayed in the summary report.
General form of the PROC MEANS statement: PROC MEANS DATA=SAS_data_set_name;
The RUN statement signals the end of the step. General form of the RUN statement:
RUN;
342
The PROC MEANS Step
Based on the International Airlines lecture scenario, the code for the complete PROC MEANS step is proc means data=pilotdata; run;
343
The PROC MEANS Step
This demonstration illustrates using the PROC MEANS statement and the RUN statement.
344
The VAR Statement
The VAR statement can be added to the PROC MEANS step. The PROC MEANS step then consists of the following statements: the PROC MEANS statement the VAR statement the RUN statement.
345
The VAR Statement
The VAR statement names the variables on which to calculate the statistics. General form of the VAR statement:
VAR variable . . . ; Example:
var Salary NewSalary;
346
The VAR Statement
Based on the International Airlines lecture scenario, the code for the complete PROC MEANS step is proc means data=pilotdata; var Salary NewSalary; run;
347
The VAR Statement
This demonstration illustrates using the PROC MEANS statement, the VAR statement, and the RUN statement.
348
The CLASS Statement
The CLASS statement can be added to the PROC MEANS step. The PROC MEANS step then consists of the following statements: the PROC MEANS statement the VAR statement the CLASS statement the RUN statement.
349
The CLASS Statement
The CLASS statement names the variables by which to group the data. General form of the CLASS statement:
CLASS variable . . . ;
Example:
class Category;
350
The CLASS Statement
Based on the International Airlines lecture scenario, the code for the complete PROC MEANS step is
proc means data=pilotdata; var Salary NewSalary; class Category; run;
351
The CLASS Statement
This demonstration illustrates using the PROC MEANS statement, the VAR statement, the CLASS statement, and the RUN statement.
352
Exercises
This exercise reinforces the concepts discussed previously.
353
Coding to Create a Summary Report
Based on your previous Planning to Create a Summary Report exercise, write the 1. PROC MEANS statement 2. VAR statement 3. CLASS statement 4. RUN statement. Retrieve your program named CH8EX2 or the program named CH9EX. Add the PROC MEANS step to the bottom of the program. Save the code to a program file named CH9EX2.
354
Section 8.4
Planning to Create a Frequency Report
Objectives
Plan to create a frequency report based on a SAS data set.
356
Example of a SAS Frequency Report
The FREQ Procedure Cumulative Cumulative Category Frequency Percent Frequency Percent ______________________________________________________ DOM 19 38.00 19 38.00 INT 31 62.00 50 100.00
357
Requirements to Create a SAS Frequency Report
1. Name the SAS data set to be referenced. 2. Determine the variable on which to calculate the frequency statistics.
358
DATA Step Example
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; Bonus = Salary * 0.10; if JobCode = 'PILOT1' then NewSalary = Salary * 1.05; else if JobCode = 'PILOT2' then NewSalary = Salary * 1.07; else if JobCode = 'PILOT3' then NewSalary = Salary * 1.09; run;
359
Name the SAS Data Set to Be Referenced
Operating Environment Windows UNIX OS/390 SAS Data Set Name pilotdata pilotdata pilotdata
360
Determine the Variable on Which to Calculate the Frequency Statistics
SAS Variable Name EmployeeID FirstName LastName JobCode Category Salary Bonus NewSalary
361
... ...
Exercises
This exercise reinforces the concepts discussed previously.
362
Planning to Create a Frequency Report
Based on your program named CH8EX2 or the program named CH9EX, plan to create a frequency report. The desired frequency report is a breakdown of the percentage of domestic flights versus international flights. This is Report #3 of the exercise scenario.
363
Planning to Create a Frequency Report
Name the SAS data set to be referenced.
ratedata
Name the variable on which to calculate the frequency statistics.
Category
364
... ...
Section 8.5
Coding to Create a Frequency Report
Objectives
Code to create a frequency report based on a SAS data set.
366
Choosing the PROC FREQ Step
PROC Step(s) PRINT ...
MEANS
FREQ
367
The PROC FREQ Step
Start
End
proc freq __________ ; _______________ ; . . . _______________ ; run;
368
... ...
The PROC FREQ Step
The PROC FREQ statement names the SAS data set being displayed in the frequency report. General form of a PROC FREQ statement:
PROC FREQ DATA=SAS_data_set_name; The RUN statement signals the end of the step. General form of the RUN statement:
RUN;
369
The PROC FREQ Step
Based on the International Airlines lecture scenario, the code for the complete PROC FREQ step is proc freq data=pilotdata; run;
370
The PROC FREQ Step
This demonstration illustrates using the PROC FREQ statement and the RUN statement.
371
The TABLES Statement
The TABLES statement can be added to the PROC FREQ step. The PROC FREQ step then consists of the following statements: the PROC FREQ statement the TABLES statement the RUN statement.
372
The TABLES Statement
The TABLES statement names the variables on which to calculate the frequency statistics.
General form of the TABLES statement:
TABLES variable . . . ;
Example:
tables Category;
373
The TABLES Statement
Based on the International Airlines lecture scenario, the code for the complete PROC FREQ step is
proc freq data=pilotdata; tables Category; run;
374
The TABLES Statement
This demonstration illustrates using the PROC FREQ statement, the TABLES statement, and the RUN statement.
375
One-Way Frequency
A one-way frequency table is generated by a single variable name on the TABLES statement.
TABLES variable; Example:
tables Category;
376
One-Way Frequency
The FREQ Procedure Cumulative Cumulative Category Frequency Percent Frequency Percent ______________________________________________________ DOM 19 38.00 19 38.00 INT 31 62.00 50 100.00
377
Two-Way Frequency
A two-way frequency table can be generated by two variable names joined with an asterisk on the TABLES statement.
TABLES variable*variable; Example:
tables Category*JobCode;
378
Two-Way Frequency
Partial Output
The FREQ Procedure
Table of Category by JobCode
Category JobCode Frequency| Percent | Row Pct | Col Pct | PILOT1 |PILOT2 PILOT3 Total _________|________ |___________________ DOM | 10 | 9 0 19 | 20.00 | 18.00 0.00 38.00 | 52.63 | 47.37 0.00 |100.00 | 36.00 0.00
379
Two-Way Frequency
This demonstration illustrates using the PROC FREQ statement, the TABLES statement, and the RUN statement to create a two-way frequency report.
380
Exercises
This exercise reinforces the concepts discussed previously.
381
Coding to Create a Frequency Report
Based on your previous Planning to Create a Frequency Report exercise, write the 1. PROC FREQ statement 2. TABLES statement 3. RUN statement. Retrieve your program named CH8EX2 or the program named CH9EX. Add the PROC FREQ step to the bottom of the program. Save the code to a program file named CH9EX4.
382
Chapter 9
Additional Topics
Section 9.1
Creating a Permanent SAS Data Set
Objectives
Create a permanent SAS data set.
385
Why Create a Permanent SAS Data Set?
Permanent data sets are created in order to have a SAS data set that exists after ending the SAS session.
386
... ...
Creating a Temporary SAS Data Set
data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; run;
What is the name of the SAS data set being created?
387
... ...
Creating a Temporary SAS Data Set
pilotdata
(a temporary SAS data set) data pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; run;
388
Creating a Temporary SAS Data Set
If a SAS data set is temporary, the data set can be referenced with a one-level name. Example: pilotdata Actually, all SAS data sets have a two-level name. Example: work.pilotdata
Data Library Reference Data Set Name ... ...
389
SAS Data Libraries
A SAS data library can be thought of as a drawer in a filing cabinet and a SAS data set as one of the file folders in the drawer.
FILES
LIBRARIES
390
... ...
SAS Data Libraries
Each SAS data library is identified by assigning a libref.
LIBREF
391
... ...
SAS Data Libraries
When SAS is invoked, automatically there is access to a temporary and a permanent SAS data library.
WORK
SASUSER
WORK - temporary library SASUSER - permanent library
392
... ...
SAS Data Libraries
Additional permanent libraries can also be created.
WORK
SASUSER
IA - permanent library
IA
393
... ...
SAS Data Libraries
A SAS data library is organized differently in each operating environment.
Operating Environment Windows UNIX OS/390 SAS Data Library Organized As directory directory specially formatted operating system file
394
The LIBNAME Statement
The LIBNAME statement can be used to create additional SAS data libraries. General form of the LIBNAME statement:
LIBNAME libref 'SAS-data-library' ;
Name of SAS data library
Location of SAS data set(s)
395
... ...
The LIBNAME Statement
Windows Example:
libname ia 'c:\workshop\winsas\basic'; UNIX Example: libname ia '/users/edu01'; OS/390 Example: libname ia 'edu001.basic.sasdata';
396
Creating a Permanent SAS Data Set
data ia.pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; run;
What is the name of the SAS data set being created?
397
... ...
Creating a Permanent SAS Data Set
ia.pilotdata
data ia.pilotdata; infile 'input-raw-data-file'; input EmployeeID $ 1 - 6 FirstName $ 7 - 19 LastName $ 20 - 34 JobCode $ 35 - 41 Salary 42 - 47 Category $ 48 - 50; run;
(a permanent SAS data set)
398
The LIBNAME Statement
This demonstration illustrates using the LIBNAME statement.
399
Exercises
This exercise reinforces the concepts discussed previously.
400
Creating a Permanent SAS Data Set
Modify the program named CH10EX to create a permanent SAS data set. Also, add a PROC PRINT step to create a list report of the permanent SAS data set.
401
Section 9.2
Sorting the Observations in a SAS Data Set
Objectives
Sort the observations in a SAS data set.
403
Why Sort the Observations?
Sorting observations enables you to create a list report with the observations in a particular order.
404
... ...
Example of a List Report
Obs Employee ID FirstName LastName Job Code
1 E01046 DAVID CHAPMAN PILOT1 E01682 VICTOR TAILOR PILOT1 The2PRINT procedure displays the observations 3 E02659 CLIFTON WILDER PILOT1 in the order that they are G. stored in the SAS data set. 4 E04042 SAMUEL BENNETT PILOT1 5 E04732 CHRISTIAN EDMINSTON PILOT1 6 E03740 CRAIG N. SAWYER PILOT1 7 E03389 LOUISE STAINES PILOT1 8 E04688 JOHN D. PERRY PILOT1
405
... ...
Sorting Observations in a Data Set
2
EmployeeID E06745 E07884 E07902 FirstName CHUCK DONNA TINA
1
LastName SMITH BROWN SMITH JobCode PILOT2 PILOT2 PILOT2
EmployeeID E07884 E06745 E07902
FirstName DONNA CHUCK TINA
LastName BROWN SMITH SMITH
JobCode PILOT2 PILOT2 PILOT2
406
... ...
Sorting Observations in a Data Set
The SORT procedure rearranges the observations in a SAS data set can create a new SAS data set containing the rearranged observations can sort on multiple variables sorts in ascending order (by default) does not generate printed output.
407
Requirements for Sorting Observations in a Data Set
1. Name the SAS data set to be sorted. 2. Name the SAS data set to be created (optional). 2. Name the variable(s) and order by which to sort.
408
Name the SAS Data Set to be Sorted
Operating Environment Windows UNIX OS/390 SAS Data Set Name pilotdata pilotdata pilotdata
409
Name the SAS Data Set to be Created (Optional)
Operating Environment Windows UNIX
SAS Data Set Name
sortedpilotdata sortedpilotdata
OS/390
sortedpilotdata
410
Name the Variable(s) and Order by Which to Sort
SAS Variable Name EmployeeID
second
first
FirstName LastName JobCode Salary Category
411
... ...
The PROC SORT Step
The PROC SORT step consists of the following statements: the PROC SORT statement the BY statement the RUN statement.
412
The PROC SORT Statement
The PROC SORT statement names the SAS data set to be sorted. Optionally, the PROC SORT statement names the SAS data set to be created.
General form of the PROC SORT statement: PROC SORT DATA=SAS_data_set_name OUT=NEW_SAS_data_set_name;
413
The PROC SORT Statement
Example:
proc sort data=pilotdata out=sortedpilotdata;
414
The BY Statement
The BY statement determines the sort order of the SAS data set.
General form of the BY statement: BY variable(s);
Example:
by LastName FirstName;
415
The RUN Statement
The RUN statement signals the end of a step.
General form of the RUN statement: RUN;
416
The PROC SORT Step
Based on the scenario, the code for the complete PROC SORT step is
proc sort data=pilotdata out=sortedpilotdata; by LastName FirstName; run;
417
The PROC SORT Step
This demonstration illustrates using the PROC SORT statement, the BY statement, and the RUN statement.
418
Exercises
This exercise reinforces the concepts discussed previously.
419
Sorting Observations in a Data Set
Based on the program named CH10EX, plan and code to create a list report. The following is a partial example of the list report that needs to be created. The data is sorted by two variables.
Obs 1 2 . 97 98 99 100 . Origination AKL AMS . RDU RDU SEA SEA . Destination WLG LHR . SEA SFO RDU SFO . Range SHORT SHORT . MEDIUM MEDIUM MEDIUM SHORT . Category DOM INT . DOM DOM DOM DOM .
420
Sorting Observations in a Data Set
Name the SAS data set to be sorted.
ratedata
Name the SAS data set to be created.
sortedratedata
421
... ...
Sorting Observations in a Data Set
Name the variables by which to sort. Origination First sort variable: Second sort variable: Destination
422
... ...
Section 9.3
Enhancing a Report
Objectives
Add titles and footnotes to a list report. Display descriptive labels for variables in a list report. Format the values of a variable in a list report.
424
Program Code to Create a List Report
proc print data=pilotdata; var EmployeeID FirstName LastName Salary; run;
425
List Report
The SAS System Employee ID E01046 E01682 E02659 E04042 E04732
Add Titles
LastName Salary
72660 44980 53630 52870 76120
Obs
1 2 3 4 5
FirstName
DAVID CHAPMAN Display Labels VICTORDescriptive TAILOR CLIFTON G. WILDER SAMUEL BENNETT Format the Values CHRISTIAN EDMINSTON
Add Footnotes
426
... ...
Enhanced List Report
International Airlines Employee Information ID Number E01046 E01682 E02659 E04042 E04732 First Name DAVID VICTOR CLIFTON G. SAMUEL CHRISTIAN Last Name CHAPMAN TAILOR WILDER BENNETT EDMINSTON Annual Salary $72,660 $44,980 $53,630 $52,870 $76,120
Obs
1 2 3 4 5
Created by the Human Resource Department Confidential
427
Enhanced List Report
To enhance a list report, the following statements can be added to the PROC PRINT step: TITLE statement(s) FOOTNOTE statement(s) LABEL statement FORMAT statement.
428
The TITLE Statement(s)
The TITLE statement specifies a title line to be printed on each page of SAS output.
General form of the TITLE statement: TITLEn text ;
429
The TITLE Statement(s)
Titles appear at the top of the page. The default title is The SAS System. The value of n can be from 1 to 10. An unnumbered TITLE is equivalent to TITLE1. After a title is specified, it is used for all subsequent output until the title is changed, cancelled, or the SAS session is ended.
430
The TITLE Statement(s)
To change a title, a TITLE statement for a given line cancels the previous TITLE statement for that line and for all title lines with higher numbers. The following statement suppresses a title on line n and all lines after it: TITLEn;
431
The TITLE Statement(s)
proc print data=pilotdata; title1 'The First Line'; title2 'The Second Line'; title4 'The Fourth Line'; run; proc print data=pilotdata; run;
Four titles will appear on this output. Lines 1, 2, and 4 will have a value and line 3 will be blank. This output will have the same titles as the previous output. Two titles will appear on this output. Line 1 will be the same as the previous output.
proc print data=pilotdata; title2 'A New Second Line'; run;
432
... ...
The TITLE Statement(s)
Based on the scenario, the code for the PROC PRINT step is
proc print data=pilotdata; var EmployeeID FirstName LastName Salary; title1 'International Airlines'; title2 'Employee Information'; run;
433
The FOOTNOTE Statement(s)
The FOOTNOTE statement specifies a line of text to be printed on the bottom of each page of SAS output.
General form of the FOOTNOTE statement: FOOTNOTEn 'text';
434
The FOOTNOTE Statement(s)
Footnotes appear at the bottom of the page. There is no default footnote. The value of n can be from 1 to 10. An unnumbered FOOTNOTE is equivalent to FOOTNOTE1. After a footnote is specified, it is used for all subsequent output until the footnote is changed, cancelled, or the SAS session is ended.
435
The FOOTNOTE Statement(s)
To change a footnote, a FOOTNOTE statement for a given line cancels the previous FOOTNOTE statement for that line and for all footnote lines with higher numbers. The following statement suppresses a footnote on line n and all lines after it: FOOTNOTEn;
436
The FOOTNOTE Statement(s)
Based on the scenario, the code for the PROC PRINT step is
proc print data=pilotdata; var EmployeeID FirstName LastName Salary; footnote1 'Created by the Human Resource Department'; footnote2 'Confidential'; run;
437
The LABEL Statement
The LABEL statement associates descriptive labels with variables. Any number of variables can be associated with labels in a single LABEL statement.
General form of the LABEL statement: LABEL variable='label ' variable='label ';
438
The LABEL Statement
In addition to the LABEL statement, the LABEL option must be present on the PROC PRINT statement. General form of the PROC PRINT statement with the LABEL option:
PROC PRINT DATA=SAS_data_set_name LABEL;
439
The LABEL Statement
Based on the scenario, the code for the PROC PRINT step is
LABEL Option
proc print data=pilotdata label; var EmployeeID FirstName LastName Salary; label EmployeeID='ID Number' FirstName='First Name' LABEL LastName='Last Name' Statement Salary='Annual Salary'; run;
440
... ...
The LABEL Statement
When you use the LABEL option, PROC PRINT splits labels if necessary in order to conserve space. To control where the splits occur, the SPLIT= option can be used instead of the LABEL option.
General form of the PROC PRINT statement with the SPLIT= option: PROC PRINT DATA=SAS_data_set_name SPLIT='split-character';
441
The LABEL Statement
Based on the scenario, the code for the PROC PRINT step is
proc print data=pilotdata split='*'; var EmployeeID FirstName LastName Salary; label EmployeeID='ID*Number' FirstName='First*Name' LastName='Last*Name' Salary='Annual*Salary'; run;
442
The LABEL Statement
proc print data=pilotdata split='*'; label EmployeeID='ID*Number' FirstName='First*Name' LastName='Last*Name' Salary='Annual*Salary'; run; proc print data=pilotdata; run;
Labels will appear on this output.
No labels will appear on this output.
Labels associated with variables in a PROC step remain in effect only for that step. ... ...
443
The FORMAT Statement
The FORMAT statement associates formats with variables. A single FORMAT statement can associate the same format with several variables or different formats with different variables.
General form of the FORMAT statement: FORMAT variable(s) format(s);
444
The FORMAT Statement
Salary
(as stored in data set) 72660
Salary
(as needed in report) $72,660
Use a SAS format that adds a dollar sign and commas to a numeric value.
445
... ...
The FORMAT Statement
Selected formats for numeric values: w.d is standard numeric format. COMMAw.d adds commas to a numeric value. DOLLARw.d adds a dollar sign and commas to a numeric value.
w= d = specifies the width, in columns, of the field containing the numeric value. specifies the number of digits to the right of the decimal point (optional).
446
The FORMAT Statement
Salary
(as stored in data set) 72660
Salary
(as needed in report) $72,660
What numeric format is needed?
447
... ...
The FORMAT Statement
Salary
(as stored in data set)
Salary
(as needed in report)
72660
$72,660
DOLLARw.d
7 0
448
... ...
The FORMAT Statement
Salary
(as stored in data set) 72660
Salary
(as needed in report) $72,660
DOLLAR7.0
449
The FORMAT Statement
Based on the scenario, the code for the PROC PRINT step is
proc print data=pilotdata; var EmployeeID FirstName LastName Salary; format Salary dollar7.0; run;
450
The FORMAT Statement
proc print data=pilotdata; format Salary dollar7.; run; proc print data=pilotdata; run;
Salary will
be formatted for this output.
Salary will
not be formatted for this output.
Formats associated with variables in a PROC step remain in effect only for that step.
451
... ...
The PROC PRINT Step
This demonstration illustrates using the TITLE statement, the FOOTNOTE statement, the LABEL statement, and the FORMAT statement within a PROC PRINT step.
452
Exercises
This exercise reinforces the concepts discussed previously.
453
Enhancing a Report
Based on the program named CH10EX, code to create a list report. The following is a partial example of the list report that needs to be created.
International Airlines Flight Information Flight Start FRA CPT RDU SYD . Flight Stop CPT FRA FRA HKG . Cargo Rate $5.27 $5.27 $2.86 $4.15 . Passenger Fare $1,142.00 $1,142.00 $836.00 $898.00 .
Obs 1 2 3 4 .
Created for the Management Staff
454
Section 9.4
Displaying Selected Observations in a Report
Objectives
Display selected observations in a report.
456
Why Display Selected Observations?
Displaying selected observations creates a list report that contains only the observations that meet a specified condition.
457
... ...
Displaying Selected Observations
Scenarios: A list report that contains only the employees who make less than 70000 A list report of only the employees who have a job code of PILOT2
A list report of the employees who have a job code of PILOT2 and who make less than 70000.
458
Example of a List Report
Obs 1 2 3 4 5 6 7 8 FirstName LastName JobCode Salary 72660 44980 53630 52870 76120 62280 74390 67680 DAVID CHAPMAN PILOT1 VICTOR TAILOR PILOT1 The PRINT procedure, by default, CLIFTON G. WILDER PILOT1 SAMUEL BENNETT PILOT1 in displays all the observations CHRISTIAN PILOT1 aEDMINSTON SAS data set. CRAIG N. SAWYER PILOT1 LOUISE STAINES PILOT1 JOHN D. PERRY PILOT1
459
... ...
The WHERE Statement
The WHERE statement restricts observations shown in the report, based on specific variable values.
General form of the WHERE statement: WHERE where-expression;
460
The WHERE Statement
The where-expression is a valid arithmetic or logical expression that generally consists of a sequence of operands and operators. Operands include constants and values of variables obtained from the SAS data set. Operators are used in the expression to request a comparison, arithmetic calculation, or logical operation.
461
Comparison Operators
EQ NE GT LT GE LE IN = ^= > < >= <= equal to not equal to greater than less than greater than or equal to less than or equal to equal to one of a list
462
Comparison Operators
Examples:
where Salary ge 50000; where LastName='Smith';
Character values must be enclosed in quotes and match the case of the data.
463
... ...
Arithmetic Operators
* / + ** multiplication division addition subtraction exponentiation
464
Logical Operators
AND & If both expressions are true, then the compound expression is true. If either expression is true, then the compound expression is true. This operator can be combined with other operators to reverse the logic of a comparison.
OR
NOT
465
Logical Operators
Examples:
where LastName='Smith' and Salary ge 50000; where LastName not = 'Smith' and Salary not ge 50000;
466
Logical Operators
Examples:
where LastName='Smith' or LastName='Brown' or LastName='Jones'; Must type variable name three times.
where LastName in ('Smith', 'Brown', 'Jones');
Type variable name only one time.
467
... ...
The WHERE Statement
Based on the scenarios, the code for the needed WHERE statements is
where Salary lt 70000; where JobCode='PILOT2'; where JobCode='PILOT2' and Salary lt 70000; Each WHERE statement belongs in a separate PROC step.
468
... ...
The PROC PRINT Step
This demonstration illustrates using the WHERE statement within a PROC PRINT step.
469
Exercises
This exercise reinforces the concepts discussed previously.
470
Displaying Selected Observations
Based on the program named CH10EX, code to create three list reports. The three list reports should include the data representing origination, destination, cargo rate, and passenger fare. The first report should only show the flights leaving from RDU. The second report should show only the flights with passenger fares greater than 500. The third report should show only the flights leaving from RDU with passenger fares greater than 500.
471
Section 9.5
Reading Excel Spreadsheets
Objectives
Create a SAS data set from an Excel spreadsheet using the Import Wizard.
473
Business Task
The flight data for Dallas and Los Angeles are in an Excel spreadsheet. Read the data into a SAS data set. Excel Spreadsheet
SAS Data Set
Flight Date 439 921 114
474
SAS Data Set
LAX DFW LAX
Dest
FirstClass Economy 20 20 15 137 131 170
12/11/00 12/11/00 12/12/00
The Import Wizard
The Import Wizard is a point-and-click graphical interface that enables you to create a SAS data set from several types of external files including dBASE files (*.DBF) Excel spreadsheets (*.XLS) Microsoft Access tables (*.MDB) delimited files (*.*) comma-separated values (*.CSV).
475
Reading Raw Data with the Import Wizard
This demonstration illustrates using the Import Wizard to create a SAS data set from an Excel spreadsheet.
476
Chapter 10
Learning More
Section 10.1
Next Steps
Next Steps
Having completed this course, you now have a broad understanding of the capabilities of the base SAS product general knowledge of some of the commonly used programming features and procedures.
479
Next Steps
Refer to the SAS Training Web site at www.sas.com/training for more information on these classes and the broad curriculum of courses available from SAS Institute Inc.
480
Next Steps
Consider SAS e-Learning products to enhance and practice what you learn in class. SAS Online Tutor software - Web-based training for acquiring Version 8 SAS programming skills SAS Tutor software - a CD-ROM library of six lessons designed to teach Version 6 SAS programming skills.
481
Section 10.2
SAS Institute Resources
SAS Services
SAS Institute is a full service company that provides consulting short- or long-term consulting services to meet your business needs training instructor-based and on-line training options certification global certification program to assess knowledge of SAS software and earn industry-recognized credentials.
483
SAS Services
SAS Institute is a full-service company that provides online help a comprehensive online help system to address many of your information needs documentation extensive online and hardcopy reference information technical support specialists for all SAS software products and supported operating systems.
484
SAS Services
Access the SAS Web site at www.sas.com to learn more about the available software, support, and services and to take advantage of these offerings.
485
Online Help and Documentation
SAS features an extensive online help system. You can also access SAS OnlineDoc, which provides you with SAS System reference documentation.
486
SAS Documentation
SAS documentation is also available in hardcopy. Some useful references are Getting Started with the SAS System, Version 8 SAS Language Reference: Dictionary, Version 8 SAS Language Reference: Concepts, Version 8 SAS Procedures Guide, Version 8.
487
Bien plus que des documents.
Découvrez tout ce que Scribd a à offrir, dont les livres et les livres audio des principaux éditeurs.
Annulez à tout moment.