You are on page 1of 45

Introduction to SAS Essentials

Mastering SAS for Data Analytics


Alan Elliott and Wayne Woodward

1 SAS Essentials - Elliott & Woodward


Available to be viewed online on
books.google.com.hk
} https://books.google.com.hk/books?id=bdVZCgAAQ
BAJ&printsec=frontcover&dq=Introduction+to+SAS+
Essentials+Mastering+SAS+for+Data+Analytics&hl=
en&sa=X&ved=2ahUKEwi_pLPbwKDuAhXaE4gKHb
cjC54Q6AEwAHoECAUQAg#v=onepage&q=Introdu
ction%20to%20SAS%20Essentials%20Mastering%2
0SAS%20for%20Data%20Analytics&f=true

2 SAS Essentials - Elliott & Woodward


Tools available

n SAS and SPSS are the most commonly-used software/tool that organisations use.
n Most organisations (or even universities) provide both of these two tools.
n However, before we proceed further, I think it is necessary for you all to
understand why one of them is often chosen over another.

3
Tools available

n SPSS is more user-friendly in general.


n In terms of the available analytical tools in SAS and SPSS, they are more or less
equally powerful. But, consultancy and business analytics generally prefer SAS for a
number of reasons.
n 1. It is common that we are dealing with high-volume datasets (i.e. we are
talking about datasets with millions or billions observations). SPSS is a lot
slower.
n 2. Another important reason is that unfortunately data is not “clean”.
4
Importance of Data Cleansing
n What do I really mean by data not being clean?
n One way to understand it is that data cannot by analysed in the way they are
received.
n Your clients usually provide you with messy data.
n Wrong entries
n Invalid data
n Data that is in separate files (i.e. this is very common. Data are coming from
different sources – business division). Without properly merging them
together in the right format, there is no way we can examine the pattern of
results that are of interest.

5
Accessing SAS

6 SAS Essentials - Elliott & Woodward


Three Methods
1) Register for SAS OnDemand for Academics ( free, web-
based )
1) https://www.sas.com/en_hk/software/on-demand-for-
academics.html
2) Choose Access Now, Choose ‘Don’t have a SAS profile’ to
register
2) Download SAS University ( Need to install virtualbox on your
PC and install SAS into your virtual environment )
1) https://www.sas.com/en_hk/software/university-
edition/download-software.html
3) Access e-lab ( full access to SAS enterprise )
1) http://booking.hsu.edu.hk
2) Choose Academic Department
3) Choose Department of Supply Chain & Information Management
4) Choose A211

7 SAS Essentials - Elliott & Woodward


Use first method – SAS Ondemand for
Academics for the beginning

8 SAS Essentials - Elliott & Woodward


First register SAS profile using your hsu
email id and then log on after receiving
instruction from SAS

9 SAS Essentials - Elliott & Woodward


Chapter 1: Getting Started

10 SAS Essentials - Elliott & Woodward


LEARNING OBJECTIVES
} To be able to use the SAS® software program in a
Windows environment.
} To understand the basic information about getting
data into SAS and running a SAS program.
} To be able to run a simple SAS program.

11 SAS Essentials - Elliott & Woodward


Introducing SAS
} SAS is a programming language that specializes in
data analytics.
} There are several ways to use SAS – this book
teaches you how to use SAS code, which provides
the most flexibility.
} We’ll be using SAS in a Windows environment,
although SAS is available on most platforms.
} A brief tutorial on the University Edition is in the book’s
appendix.
} Also, information on using the Citrix SAS (Apps) version
is included in the text.

12 SAS Essentials - Elliott & Woodward


Getting Started – Download Example
Files
} Getting the data needed for the course
} Data files are available at

http//:www.alanelliott.com/sas

Choose one of three options to download files:


} OPTION 1 - Download self-extracting file to copy
SAS files to C:\SASDATA) Recommended.
} OPTION 2 – Download a zipped version of data files.
} Option 3 – Download SAS files in text format – a
chapter at a time.

13 SAS Essentials - Elliott & Woodward


1.1 Using SAS in A Windows
Environment ( Note: We shall not use
Windows environment during online
sessions, this is for your information
only )
} SAS runs on a number of computer platforms
(operating systems) including mainframes and
personal computers whose operating systems are
UNIX, Mac OS X, Linux, or Windows.
} This book is based on using SAS in a Windows
environment where you have the software installed
on your local computer. The vast majority of the
content in this book will apply to any SAS computer
environment.
14 SAS Essentials - Elliott & Woodward
Creating a Folder for Storing Your
SAS Files
} Install the data and SAS programming files in your
(Windows) C:\SASDATA folder.
We’ll be accessing SAS files from
Your Hard Disk
the C:\SASDATA folder on your
computer

C:\SASDAT SAS Program


A

If you are using the University edition, consult Appendix E regarding the
location of the data files. For other versions of SAS, see Table 1.1 on page 4 for
information on accessing files.

15 SAS Essentials - Elliott & Woodward


Using the ‘APPS’ version on campus
} At some universities there is a network version of
SAS available. For example at SMU it is:

} apps.smu.edu

} When using apps version, instead of C:\ use

} \\CLIENT\C$\

16 SAS Essentials - Elliott & Woodward


Understanding the SAS Interface

LOG Window

Explorer
&
Results
Program Editor

NOTE TABS

17 SAS Essentials - Elliott & Woodward


SAS Editor
} Also called the Enhanced Editor or Windows
Programming Editor (WPGM), this is the area where
you write SAS code. It is like a simple word
processor. When you open a previously saved SAS
program, its contents will appear in this window.
} SAS code is stored in plain ASCII text, so files saved
in the ASCII format from any other editor or word
processor may be easily opened in this editor.
} You can also copy (or cut) text from another editor or
word processor and paste it into the Editor window.

18 SAS Essentials - Elliott & Woodward


SAS Log
When you run a SAS program, a report detailing how
(and if) the program ran, appears in the LOG
window. Typically, when you run a SAS program, you
will first look at the contents of the LOG window to see
if there were any errors in the program. Errors appear
in red. You should also look for warnings and other
notes in the LOG Window to that tell you that some
part of your program may not have run correctly.

19 SAS Essentials - Elliott & Woodward


SAS Explorer/Results Window
This window appears at the left of the screen and
contains two tabs: The Results tab displays a tree
listing of your output, making it easy to quickly scroll to
any place in your output listing. The Explorer tab
displays the available SAS libraries – these libraries
are where SAS data files are stored.

20 SAS Essentials - Elliott & Woodward


Graph Window
If your SAS program creates graphic output, SAS will
display a Graph window that contains the SAS graph.
It is usually automatically displayed. If it does not
appear click on the Graph tab to display this window.

21 SAS Essentials - Elliott & Woodward


Results Viewer
Beginning with SAS 9.3, SAS displays output from
your SAS analysis in HTML format in the Results
Viewer. We’ll discuss more about how to control this
output in the section on Output Delivery System (ODS)

22 SAS Essentials - Elliott & Woodward


Oops…
} What if you close one of these Windows?
} Reopen it by going to the View pull-down menu and
select the windows you want to reopen

23 SAS Essentials - Elliott & Woodward


1.2 Your First SAS Analysis
} In SAS, choose File/Open Program… and open the
program named C:\SASDATA\First.SAS

24 SAS Essentials - Elliott & Woodward


Note Sections in Code

This section
defines the data
for the analysis

This section defines


an analysis

25 SAS Essentials - Elliott & Woodward


Defining a Data Set

We’ll discuss this more in detail


later, but notice that this code
defines data set named
EMPLOYEES that consists of
two variables GENDER and
AGE.

26 SAS Essentials - Elliott & Woodward


Requesting an Analysis

The second section of the


code requests an analysis.
In this case, the requested
analysis is called “MEANS”
(which indicates simple
statistics) and the CLASS
(classification or grouping) of
the data is request by
GENDER.

27 SAS Essentials - Elliott & Woodward


Run the code

To tell SAS to run


(Submit) this code,
click on the “Running
Man” icon or select
Run/Submit

28 SAS Essentials - Elliott & Woodward


Output Created, Results Window

The output created by this simple program displays simple statistics for the
numeric variables (AGE) in the data set – By Gender. This type output is
HTML.

29 SAS Essentials - Elliott & Woodward


What to do with output
} You can print from the output window
} Save the output
} Copy and paste the results in Word, PowerPoint, etc.
} Later, we’ll see how to automatically output the
analyses to .DOC, .PDF, etc

30 SAS Essentials - Elliott & Woodward


Do Hands-On Example p 9
} Make sure you are in the Editor window.
} Open Second.SAS.
} Add a line to the program.
} Run the program and observe the output.

31 SAS Essentials - Elliott & Woodward


Results

32 SAS Essentials - Elliott & Woodward


1.3 How SAS Works
1. Define a data set in SAS using the DATA step (which begins
with the key word DATA.) In this case, the data values are a
part of the code (although it is not always the case.) The data
values to be used in this analysis follow the keyword
DATALINES.

2. Once you have a data set of values, you can tell SAS what
analysis to perform using a procedure (PROC) statement In this
case, the keywords PROC MEANS initiate the "MEANS"
procedure.

3. Run the program and observe the output (in the Results
Viewer).

33 SAS Essentials - Elliott & Woodward


1.4 Tips and Tricks for Running SAS
} Within a SAS program, each statement begins with
an identifying keyword (DATA, PROC, INPUT,
DATALINES, RUN, etc.) and ends with a semicolon
“;”. For example:

DATA TEMP;
PROC PRINT DATA=TEMP;
RUN;
All three lines start with a SAS keyword…

34 SAS Essentials - Elliott & Woodward


SAS Tips continued…

} Statements can begin anywhere and end anywhere.


} Statements can continue over several lines, ends with
semi-colon.
} Several statements may be on the same line.
} Blanks, as many as you want but at least one, separating
the components (words) in a SAS program statement.
} Case, (lower and UPPER) doesn’t matter in most SAS
statements.
} Case does make a difference in data and quoted
information. (such as M or m for “MALE” or “male”).

35 SAS Essentials - Elliott & Woodward


SAS Tips Continued…
} The most common error in SAS programming is a
misplaced (or missing) semicolon.
} A second common error is a missing RUN; statement.
} A third common error in a SAS program is the presence
of unbalanced quotation marks.
} Look for errors in a program log from the top down.
} If program errors cause problems that result in SAS
“freezing up” or not completing the steps in your program,
a way to stop SAS from continuing to run is to press
CTRL-Break and to select the “Cancel Submitted
Statements” option.
} If you cannot resolve a problem within SAS, save your
files, exit the SAS program, and restart.
} Make the structure of your SAS programs easy to read.

36 SAS Essentials - Elliott & Woodward


Enhanced Editor
} Green - Comments appear in green.
} Dark Blue - Major SAS commands (also called
“step-boundaries”) begin with the keyword in dark
blue.
} Blue - Keywords that have special meaning as SAS
commands appear in blue.
} Yellow highlight - Data are highlighted in yellow.
} Boundary Line - A section boundary line separates
each step.

37 SAS Essentials - Elliott & Woodward


SAS Function Keys (Defined)
Function SAS command Result
Key
F2 RESHOW reshows window interrupted by system
command
F3 END; /*GSUBMIT submits SAS statements in clipboard
BUFFER=DEFAUL
T*/
F4 RECALL recalls current SAS code to editor
F5 PROGRAM (PGM) display SAS program editor window
F6 LOG displays SAS log window
F7 OUTPUT displays SAS output window
F8 ZOOM submits (runs) the current SAS program
OFF;SUBMIT
F9 KEYS displays keys window
F10 NOT DEFINED
F11 COMMAND BAR moves cursor to command bar
F12
38
NOT DEFINED
SAS Essentials - Elliott & Woodward
Define a new F12 Key (page 13)
} Step 1: Select Tools, Options, and Keys.
} Step 2: Next to the blank F12 option enter this code:
CLEAR LOG; ODSRESULTS; CLEAR; WPGM;
} (Press the Enter key to lock in the new command.)
} Step 3: Exit the Key Window and try out this function
key by re-running one of the previous examples.
With the output displayed on the screen, press F12.
The output will be cleared, the log file information will
be cleared and the editor window will be displayed
still containing the current program code. Thus, this
command allows you to quickly clear the log and
output windows while keeping the program code.

39 SAS Essentials - Elliott & Woodward


SAS Menus (Change depending on
Window)
} File Menu - Used for opening and saving files, and for printing.
} Edit Menu - Used to copy, cut and paste text, as well as to find and replace
text.
} View Menu - Allows you to go back and forth between viewing SAS editor,
log and output windows.
} Tools - Allows you to open programs for graphic, image, and text editing,
along with other options available to customize the program to your
preferences.
} Run - Allows you to run (submit) a SAS program and also contains options
for accessing remote SAS options.
} Solutions - Contains links to SAS programs that allow you to interactively
design, select, and perform analyses.
} Window - Contains options found in most Windows program that allow you
to choose display strategies for opened windows such as tiled, cascade, etc.
Also allows you to select a particular window to open such as Log, Output,
etc.
} Help - Contains options for the SAS Help System as well as online
documentation.
40 SAS Essentials - Elliott & Woodward
Common File Extensions
} SAS Code File (filename.sas): This is an ACSII text file
and may be edited using the SAS Editor, Notepad, or any
text editor that can read an ASCII file.
} SAS Log File (filename.log): This ASCII text file contains
information such as errors, warnings, and data set details
for a submitted SAS program.
} SAS Results File (filename.mht or filename.html): This
file contains web-formatted output such as that displayed
in the Results Viewer. HTML stands for Hyper-Text
Markup Language and is the common language of
Internet web files. MHT is short for MHTML and stands
for Microsoft (or MIME) Hypertext Archive file.

41 SAS Essentials - Elliott & Woodward


More extensions
} SAS Data File (filename.sas7bdat): This file contains a
SAS data set that includes variable names, labels, and
the results of calculations you may have performed in a
DATA step. You cannot open or read this file except in
SAS or in a few other programs that can read SAS data
files.
} Raw Data Files (filename.dat or filename.txt or
filename.csv): These ASCII (text) files contain raw data
that can be imported into SAS or edited in an editor such
as Notepad.
} Excel File (filename.xls or filename.xlsx): The data in a
Microsoft Excel file (when properly formatted into a table
of columnar data) can be imported into SAS. (We’ll
discuss data file types that can be imported into SAS in
Chapter 3.)

42 SAS Essentials - Elliott & Woodward


Help -> SAS Help and Documentation

43 SAS Essentials - Elliott & Woodward


1.5 Summary, Chapter 1
} This chapter provided an overview of SAS and
examples of how to run an existing SAS program. In
the following chapters, we will discuss the
components of a SAS program, including how to
enter data, how to request analyses, and how to
format and read output.
} Continue to Chapter 2: Getting Data Into SAS.

44 SAS Essentials - Elliott & Woodward


These slides are based on the book:

Introduction to SAS Essentials


Mastering SAS for Data Analytics, 2nd
Edition

By Alan C, Elliott and Wayne A.


Woodward

Paperback: 512 pages


Publisher: Wiley; 2 edition (August 3,
2015)
Language: English
ISBN-10: 111904216X
These slides are provided for you to use978-1119042167
ISBN-13: to teach SAS using this book. Feel
free to modify them for your own needs. Please send comments about errors
in the slides (or suggestions for improvements) to acelliott@smu.edu.
Thanks.

45 SAS ESSENTIALS -- Elliott & Woodward

You might also like