Professional Documents
Culture Documents
Course Notes
Introduction Entering and Executing SAS Code Editing SAS Code Saving and Retrieving SAS Code Additional Topics (Optional) Chapter Summary Solutions to Exercises
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 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
Business Solutions
Web Enablement
10
... ...
Section 1.2
Explaining the Basic Functionality of SAS Software
Objectives
12
Basic Functionality
Access Manage
Data
Present
Analyze
13
... ...
Section 1.3
Identifying the File Types Used in SAS
Objectives
15
16
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
OS/390
18
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
20
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
22
Windows
UNIX
survey
survey
OS/390
survey
23
... ...
survey
survey
survey
24
... ...
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
26
OS/390
27
Exercises
This exercise reinforces the concepts discussed previously.
28
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
File File that thatcontains contains SAS SASprogram program code code
File that File contains that contains data specific SAS-specific to SAS data
30
True _______
_______ True
False _______
31
... ...
False _______
False _______
False _______
32
False _______
The naming convention for a SAS program file is the same regardless of the operating environment.
33
... ...
Descriptor Portion
Data Portion
34
... ...
Section 1.4
Identifying the Components of a SAS Program
Objectives
36
PROC step(s)
37
DATA Step(s)
Typically, DATA steps read data, such as raw data files, to create SAS data sets.
DATA Step
38
... ...
DATA Step(s)
In addition, DATA steps can modify existing variables or create new variables as necessary.
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.
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
Start
data _______________ ;
_______________ ;
. . .
_______________ ;
End
run;
... ...
44
Start
proc _______________ ;
_______________ ;
. . .
_______________ ;
End
run;
45
... ...
Exercises
This exercise reinforces the concepts discussed previously.
46
Circle the appropriate answer(s) for 2.a through 2.i. Fill in the blanks for 2.j.
47
48
... ...
49
... ...
50
... ...
51
... ...
52
... ...
53
... ...
54
... ...
55
... ...
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
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
67
... ...
Report #1
Report #2
Report #3
68
Report #1
69
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
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
73
74
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.
Job Code
Salary
Category
Bonus
New Salary
Create
79
... ...
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
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
... ...
Report #1
Report #2
Report #3
84
Report #1
85
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
88
Report #3
89
90
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
Create
95
... ...
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
100
... ...
DATA Step
101
... ...
Section 3.2
Planning to Read a Raw Data File
Objectives
103
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
105
106
107
... ...
108
109
110
Last Name
111
... ...
1 7 20 35 42 48
6 19 34 41 47 50
... ...
112
113
114
115
EmployeeID FirstName
LastName
JobCode Salary Category
... ...
116
117
118
119
120
121
122
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
125
126
... ...
OS/390
127
... ...
Origination
Destination
Origination
Destination Range CargoRate PassengerFare Category
Flight Range
Cargo Rate Passenger Fare Flight Category
128
... ...
1 6 12 20
8
17 23 34 40
28
38
129
... ...
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
133
Start
End
134
... ...
135
136
137
138
139
140
141
142
143
144
145
Exercises
This exercise reinforces the concepts discussed previously.
146
147
INFILE
Signals end of the DATA step. Names raw data file to be read.
2nd Statement
3rd Statement 4th Statement
RUN
DATA INPUT
148
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
155
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
159
The SAS windowing environment is made up of a collection of windows. There are three primary windows in the windowing environment.
1
... ...
160
161
... ...
162
... ...
163
... ...
Commands
Commands are used to navigate among the various windows of the SAS windowing environment and are used to execute a program.
continued...
164
... ...
165
... ...
166
Section 4.3
Editing SAS Code
Objectives
168
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
171
172
Results Verification
Program Testing
173
... ...
174
Exercises
This exercise reinforces the concepts discussed previously.
175
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
178
179
180
181
182
... ...
183
... ...
184
Exercises
This exercise reinforces the concepts discussed previously.
185
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
192
... ...
193
194
Report
PROC Step
195
... ...
Section 5.2
Planning to Create a List Report
Objectives
197
198
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
201
202
EmployeeID
FirstName
LastName
JobCode
203
... ...
Exercises
This exercise reinforces the concepts discussed previously.
204
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
ratedata
Name the variables to be displayed.
206
... ...
3rd variable:
4th variable:
207
... ...
Section 5.3
Coding to Create a List Report
Objectives
209
MEANS
FREQ
210
End
211
... ...
212
213
214
215
216
217
218
Exercises
This exercise reinforces the concepts discussed previously.
219
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
VAR
RUN
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
225
... ...
1 2 3 4 5 6 7
226
Obs 1 2 3 4 5 6 7
227
228
... ...
Section 6.2
Planning to Create a Variable
Objectives
230
231
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
234
value
ASSIGN
value
235
... ...
236
... ...
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
240
Character Constants
A character constant is a string of text enclosed in matching quotes. Wednesday 123 Main Street INSTRUCTOR % of revenue 7251
241
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
245
246
247
248
... ...
249
... ...
Exercises
This exercise reinforces the concepts discussed previously.
250
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
252
... ...
253
... ...
B A ____________ = ____________
254
... ...
255
... ...
256
... ...
999-99-9999
257
... ...
B __________ A __________
C __________
B __________
258
... ...
259
... ...
260
... ...
261
... ...
262
... ...
263
... ...
Section 6.3
Coding to Create a Variable
Objectives
265
266
267
... ...
269
... ...
Bonus
Salary
0.10;
EmployeeID
FirstName
Salary
Bonus
4268
ANGELA
...
52000
270
... ...
5200
EmployeeID FirstName
5200
Salary Bonus
4268
ANGELA
...
52000
5200
271
272
Exercises
This exercise reinforces the concepts discussed previously.
273
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
nglasse s su .
it is rain f I
erwise, h t
bre l l a . um
278
... ...
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
... ...
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
282
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
283
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
83738
79843 126996
122832
284
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
287
... ...
288
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
289
... ...
JobCode JobCode
= 'PILOT1' = 'PILOT2'
Character values must be enclosed in quotes and match the case of the data.
JobCode
= 'PILOT3'
290
... ...
291
... ...
292
CHAPMAN PILOT1 TAILOR PILOT1 . . . MCELROY PILOT2 CARTER PILOT2 . . . MASON PILOT3 LEE PILOT3
293
... ...
NewSalary NewSalary
NewSalary
= Salary * 1.09
294
... ...
Exercises
This exercise reinforces the concepts discussed previously.
295
296
297
... ...
Section 7.3
Coding to Perform Conditional Logic to Create a Variable
Objectives
299
300
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
... ...
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
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;
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;
312
... ...
... ...
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
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
320
... ...
321
322
323
324
325
Section 8.2
Planning to Create a Summary Report
Objectives
327
328
329
331
... ...
... ...
Exercises
This exercise reinforces the concepts discussed previously.
334
335
PassengerFare
NewPassengerFare
336
... ...
337
... ...
Section 8.3
Coding to Create a Summary Report
Objectives
339
MEANS
FREQ
340
End
341
... ...
The RUN statement signals the end of the step. General form of the RUN statement:
RUN;
342
343
344
345
346
347
348
349
CLASS variable . . . ;
Example:
class Category;
350
351
352
Exercises
This exercise reinforces the concepts discussed previously.
353
354
Section 8.4
Planning to Create a Frequency Report
Objectives
356
357
358
360
... ...
Exercises
This exercise reinforces the concepts discussed previously.
362
363
ratedata
Name the variable on which to calculate the frequency statistics.
Category
364
... ...
Section 8.5
Coding to Create a Frequency Report
Objectives
366
MEANS
FREQ
367
Start
End
368
... ...
RUN;
369
370
371
372
TABLES variable . . . ;
Example:
tables Category;
373
374
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
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
382
Chapter 9
Additional Topics
Section 9.1
Creating a Permanent SAS Data Set
Objectives
385
386
... ...
387
... ...
388
389
FILES
LIBRARIES
390
... ...
LIBREF
391
... ...
WORK
SASUSER
... ...
WORK
SASUSER
IA - permanent library
IA
393
... ...
394
395
... ...
396
397
... ...
398
399
Exercises
This exercise reinforces the concepts discussed previously.
400
401
Section 9.2
Sorting the Observations in a SAS Data Set
Objectives
403
404
... ...
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
... ...
1
LastName SMITH BROWN SMITH JobCode PILOT2 PILOT2 PILOT2
406
... ...
407
408
409
OS/390
sortedpilotdata
410
second
first
411
... ...
412
413
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
416
417
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
420
ratedata
Name the SAS data set to be created.
sortedratedata
421
... ...
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
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
... ...
Obs
1 2 3 4 5
427
428
429
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
431
432
... ...
433
434
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
436
437
438
439
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
... ...
441
442
Labels associated with variables in a PROC step remain in effect only for that step. ... ...
443
444
Salary
(as needed in report) $72,660
Use a SAS format that adds a dollar sign and commas to a numeric value.
445
... ...
446
Salary
(as needed in report) $72,660
447
... ...
Salary
(as stored in data set)
Salary
(as needed in report)
72660
$72,660
DOLLARw.d
7 0
448
... ...
Salary
(as stored in data set) 72660
Salary
(as needed in report) $72,660
DOLLAR7.0
449
450
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
... ...
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 .
Section 9.4
Displaying Selected Observations in a Report
Objectives
456
457
... ...
A list report of the employees who have a job code of PILOT2 and who make less than 70000.
458
459
... ...
460
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.
467
... ...
468
... ...
This demonstration illustrates using the WHERE statement within a PROC PRINT step.
469
Exercises
This exercise reinforces the concepts discussed previously.
470
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
Dest
475
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
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