St 555 Homework2 Coding 1 Page
The correct bibliographic citation for this manual is as follows: Blum, James and Jonathan
Duggins. 2019. Fundamentals of Programming in SAS®: A Case Studies Approach. Cary, NC: SAS Institute Inc.
Fundamentals of Programming in SAS®: A Case Studies Approach
Copyright © 2019, SAS Institute Inc., Cary, NC, USA
9781642952285 (Hardcover) 9781635266726 (Paperback) 9781635266719 (Web PDF) 9781635266696 (epub) 9781635266702 (mobi)
All Rights Reserved. Produced in the United States of America.
For a hardcopy book: No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, or otherwise, without the prior written permission of the publisher, SAS Institute Inc.
For a web download or ebook: Your use of this publication shall be governed by the terms established by the vendor at the time you acquire this publication.
The scanning, uploading, and distribution of this book via the Internet or any other means without the permission of the publisher is illegal and punishable by law. Please purchase only authorized electronic editions and do not participate in or encourage electronic piracy of copyrighted materials. Your support of others’ rights is appreciated.
U.S. Government License Rights; Restricted Rights: The Software and its documentation is commercial computer software developed at private expense and is provided with RESTRICTED RIGHTS to the United States Government. Use, duplication, or disclosure of the Software by the United States Government is subject to the license terms of this Agreement pursuant to, as applicable, FAR 12.212, DFAR 227.7202 1(a), DFAR 227.72023(a), and DFAR 227.72024, and, to the extent required under U.S. federal law, the minimum restricted rights as set out in FAR 52.22719 (DEC 2007). If FAR
Playlists
History
Topics
Learning Paths
Offers & Deals
Highlights
Settings
Support
Sign Out
52.22719 is applicable, this provision serves as notice under clause (c) thereof and no other notice is required to be affixed to the Software or documentation. The Government’s rights in Software and documentation shall be only those set forth in this Agreement.
SAS Institute Inc., SAS Campus Drive, Cary, NC 275132414
July 2019
SAS® and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.
Other brand and product names are trademarks of their respective companies.
SAS software may be provided with certain thirdparty software, including but not limited to opensource software, which is licensed under its applicable thirdparty software license agreement. For license information about thirdparty software distributed with SAS software, refer to http://support.sas.com/thirdpartylicenses.
Chapter 1: Introduction to SAS
1.1 Introduction
1.2 Learning Objectives
1.3 SAS Environments
1.3.1 The SAS Windowing Environment
1.3.2 SAS Studio and SAS University Edition
1.4 SAS Fundamentals
1.4.1 SAS Language Basics
1.4.2 SAS DATA and PROC Steps
1.4.3 SAS Libraries and Data Sets
1.4.4 The SAS Log
1.5 Output Delivery System
1.6 SAS Language Basics
1.6.1 SAS Language Structure
1.6.2 SAS Naming Conventions
1.7 Chapter Notes
1.8 Exercises
1.1 Introduction
This chapter introduces basic concepts about SAS that are necessary to use it effectively. This
chapter begins with an introduction to some of the available SAS environments and describes
Playlists
History
Topics
Learning Paths
Offers & Deals
Highlights
Settings
Support
Sign Out
the basic functionality of each. Essentials of coding in SAS are also introduced through some
pre‐constructed sample programs. These programs rely on several data sets, some provided
with SAS, others are provided separately with the textbook, including those that form the basis
for the case study used throughout Chapters 2 through 7. Therefore, this chapter also
introduces SAS data sets and libraries. In addition, an introduction to debugging code is
included, which includes a discussion of the SAS log where notes, warnings, and error
messages are provided for any code submitted.
1.2 Learning Objectives
This chapter provides a basis for working in SAS, which is a necessary first step for successful
mastery of the material contained in the remainder of this book. In detail, it is expected upon
completion of this chapter that the following concepts are understood within the chosen SAS
environment:
Demonstrate the ability to open, edit, save, and submit a SAS program
Apply the LIBNAME statement to create a user‐defined library—including the BookData
library that contains all files for this text, downloadable from the Author Page
Demonstrate the ability to navigate through libraries and view data sets
Think critically about all messages SAS places in the log to determine their cause and
severity
Apply ODS statements to manage output and output destinations
Explain the basic rules and structure of the SAS language
Demonstrate the ability to apply a template to customize output
Use the concepts of this chapter to solve the problems in the wrap‐up activity. Additional
exercises and case‐studies are also available to test these concepts.
1.3 SAS Environments
Interacting with SAS is possible in a variety of environments, including SAS from the command
line, the SAS windowing environment, SAS Enterprise Guide, SAS Studio, and SAS University
Edition; with most of these being available on multiple operating systems. This chapter
introduces the SAS windowing environment, SAS Studio, and SAS University Edition on the
Microsoft Windows operating system and points out key differences between those SAS
environments. For further specifics on differences across SAS environments and operating
systems, consult the appropriate SAS Documentation. In nearly all examples in this book, code
is given outside of any specific environment and output is shown in generic RTF‐style tables or
standard image formats. Output may vary somewhat from the default styles across SAS
environments on various operating systems, and examples later in this chapter demonstrate
some of these differences. Later chapters give information about how to duplicate the table
styles.
1.3.1 The SAS Windowing Environment
The SAS windowing environment is shown in Figure 1.3.1 with three windows visible: Log,
Explorer, and Editor (commonly referred to as the Enhanced Program Editor). The Results and
Output windows are two other windows commonly available by default, but are typically
obfuscated by other windows at launch. When code that generates output is executed, these
windows (and possibly others) become relevant.
Figure 1.3.1: SAS Windowing Environment on Microsoft Windows
In the Microsoft Windows operating system, the menu and toolbars in the SAS windowing
environment have a similar look and feel compared to other programs running on Windows.
Exploring the menus reveals standard options under the File, Edit, and Help menus (such as
Open, Save, Clear, Find). The View, Tools, Solutions, and Window menus have specialized
options related to windows and utilities that are specific to SAS. The Run menu is dedicated to
submissions of SAS code, including submissions to a remote session. As is typical in most
applications, toolbar buttons in SAS provide quick access to common menu functions and vary
depending on which window is active in the session. Some menu and toolbar options are
reviewed below during the execution of the supplied sample code given in Program 1.3.1. This
sample code is available from the author web pages for this book.
Program 1.3.1: Demonstration Code
options ps=100 ls=90 number pageno=1 nodate;
data work.cars;
set sashelp.cars;
mpg_combo=0.6*mpg_city+0.4*mpg_highway;
select(type);
when(‘Sedan’,’Wagon’) typeB=’Sedan/Wagon’;
when(‘SUV’,’Truck’) typeB=’SUV/Truck’;
otherwise typeB=type;
end;
label mpg_combo=’Combined MPG’ typeB=’Simplified Type’;
run;
title ‘Combined MPG Means’;
proc sgplot data=work.cars;
hbar typeB / response=mpg_combo stat=mean limits=upper;
where typeB ne ‘Hybrid’;
run;
title ‘MPG FiveNumber Summary’;
title2 ‘Across Types’;
proc means data=cars min q1 median q3 max maxdec=1;
class typeB;
var mpg:;
run;
After downloading the code to a known directory, there are multiple ways to navigate to and
open this code. Figure 1.3.2 shows two methods to open the file, each requiring the Editor
window to be active.
Figure 1.3.2: Methods for Opening SAS Code Files in the SAS Windowing Environment
Either of these choices launches a standard Microsoft Windows file selection window, which is
used to navigate to and select the file of interest. Upon successful selection of the code, it
appears in the Editor window, and is displayed with some color coding as shown in Figure 1.3.3
(assuming the Enhanced Program Editor is in use, the Program Editor window provides
different color coding). It is not important to understand the specific syntax or how the code
works at this point, for now it is used simply to provide an executable program to introduce
some SAS fundamentals.
Code submission can also occur in multiple ways, two of which are shown in Figure 1.3.3, again
each method requires the Editor window to be the active window in the session. If multiple
Editor windows are open, only code from the active window is submitted.
Figure 1.3.3: Submitting SAS Code in the SAS Windowing Environment
Typically, after any code submission the Results window activates and displays an index of links
to various entities produced by the program, including output tables. While not all SAS code
generates output, Program 1.3.1 does, and it may be routed to different destinations (and
possibly more than one destination simultaneously) depending on the version of SAS in use
and current option settings.
In SAS 9.4, the default settings route output to an HTML file which is displayed in the Results
Viewer, a viewing window internal to the SAS session. Previous versions of SAS rely on the
Output window for tables, an option which remains available for use in the SAS 9.4 windowing
environment, and other specialized destinations for graphics. Default output options can be
set by navigating to the Tools menu, selecting Options, followed by Preferences from that sub‐
menu, and choosing the Results tab in the window that appears, as shown in Figure 1.3.4.
Figure 1.3.4: Managing Output for Program Submissions
Among other options, Figure 1.3.4 shows the option for Create HTML checked and Create
Listing unchecked. For tables, the listing destination is the Output window, so when Create
Listing is checked, tables also appear in the Output window in what appears as a plain text
form. It is possible to check both boxes, and it is also possible to check neither, whichever is
preferred.
In the remainder of this book, output tables are shown in an RTF form embedded inside the
book text, outside of any SAS Results window. Appearance of output tables and graphs in the
book is similar to what is produced by a SAS session, but is not necessarily identical when
default session options are in place. Later in this chapter, the ability to use SAS code to control
delivery of output to each of these destinations is demonstrated, along with use of the listing
destination as an output destination for graphics files.
1.3.2 SAS Studio and SAS University Edition
SAS Studio and SAS University Edition (which are, for the remainder of this text, singularly
referred to as SAS University Edition) interface with SAS through a web browser. Typically, the
browser used is the default browser for the machine hosting the SAS University Edition
session, but this is not a requirement. Figure 1.3.5 shows a typical result of launching SAS
University Edition (in this case using the Firefox browser on Microsoft Windows), launching in
visual programmer mode by default. A closer match to the structure of the SAS windowing
environment is provided by selecting SAS Programmer from the toolbar as shown.
Figure 1.3.5: SAS University Edition
Opening a program is accomplished via the Open icon on the toolbar, as illustrated in Figure
1.3.6, and the opened code is displayed in a manner very similar to the that of the Enhanced
Program Editor display shown in Section 1.3.1.
Figure 1.3.6: Opening a Program in SAS University Edition
Though in a different position, the toolbar icon for submission is the same as in the SAS
windowing environment, and selecting it produces output in the Results tab as shown in Figure
1.3.7.
Figure 1.3.7: Execution of a Program in SAS University Edition
A few important differences to note in SAS University Edition: first, the output is displayed
starting at the top, rather than at the bottom of the page as in the SAS windowing
environment. Second, there is an additional tab for Output Data in this session. In Section
1.4.3, libraries, data sets, and navigation to each are discussed; however, SAS University
Edition also includes a special tab whenever a program generates new data sets, which aids in
directly viewing those results. Finally, note that the Code, Log, Results, and Output Data tabs
are contained within the Program 1.3.1 tab, and each program opened is given its own set of
tabs. In contrast, the SAS windowing environment supports multiple Editor windows in a single
session, but they all share a common Log window, Output window, and (under default
conditions) output HTML file. As discussed in other examples and in Chapter Notes 1 and 2 in
Section 1.7, submissions from any and all Editor windows in the SAS windowing environment
are cumulative in the Log and Output windows; therefore, managing results in each
environment is quite different.
1.4 SAS Fundamentals
To build an initial understanding of how to work with programs in SAS, Program 1.3.1 is used
repeatedly in this section to introduce various SAS language elements and concepts. For both
SAS windowing environment and SAS University Edition, the features of each environment and
navigation within them are discussed in conjunction with the language elements that relate to
them.
1.4.1 SAS Language Basics
Program 1.4.1 is a duplicate of Program 1.3.1 with certain elements noted numerically
throughout the code, followed by notes on the specific code in the indicated position.
Throughout this book, this style is used to detail important features found in sample code.
Program 1.4.1: Program 1.3.1, Revisited
options ps=100 ls=90 number pageno=1 nodate;
data work.cars;
set sashelp.cars;
MPG_Combo=0.6*mpg_city+0.4*mpg_highway;
select(type);
when(‘Sedan’,’Wagon’) TypeB=’Sedan/Wagon’;
when(‘SUV’,’Truck’) TypeB=’SUV/Truck’;
otherwise TypeB=type;
end;
label mpg_combo=’Combined MPG’ typeB=’Simplified Type’;
run;
title ‘Combined MPG Means’;
proc sgplot data=work.cars;
hbar typeB / response=mpg_combo stat=mean limits=upper;
where typeB ne ‘Hybrid’;
run;
title ‘MPG FiveNumber Summary’;
title2 ‘Across Types’;
proc means data=work.cars min q1 median q3 max maxdec=1;
class typeB;
var mpg:;
run;
SAS code is written in statements, each of which ends in a semicolon. The statements
indicated here (OPTIONS and TITLE) are examples of global statements. Global statements are
statements that take effect as soon as SAS compiles those statements. Typically, the effects
remain in place during the SAS session until another statement is submitted that alters those
effects.
The SAS DATA step has a variety of uses; however, it is primarily a tool for creation or
manipulation of data sets. A DATA step is generally comprised of several statements forming a
block of code, ending with the RUN statement, the role of which is described in .
Procedures in SAS are used for a variety of tasks and, like the DATA step, are generally
comprised of several statements. These are generically referred to as PROC steps.
The PROC MEANS result includes the variables MPG_City, MPG_Highway, and MPG_Combo
even though none of these are explicitly written in the procedure code. The colon (:) at the end
of a variable name acts as a wildcard indicating that any variable name starting with the prefix
given is part of the designated set, this shortcut is known in SAS as a name prefix list. For other
types of variable lists, see Chapter Note 3 in Section 1.7.