SAS Basics %28review%29 - big
SAS Basics %28review%29 - big
Application
Analytical Base SAS
Development
1
Submitting a SAS Program
When you execute a SAS program, the output generated
by SAS is divided into two major parts:
Raw SAS
Data Program
Files Files
SAS
DATA
Sets
2
Turning Data into Information
Raw DATA
Data Step
SAS PROC
Data Steps
Sets
Information
3
SAS Data Sets
• are files specific to SAS that
SAS
contain variables and
Data observations
Sets • can be created only by SAS
• can be read only by SAS
• consist of a descriptor
portion and a data portion
4
Components of a SAS Program
• A SAS program is a sequence of steps.
• There are only two kinds of steps:
– DATA steps A SAS Program
– PROC steps.
DATA
step(s)
PROC
step(s)
DATA Step(s)
• Typically, DATA steps read data, such as raw
data files, to create SAS data sets.
• In addition, DATA steps can modify existing
variables or create new variables as necessary.
SAS Data Set
Raw Data File
Descriptor
DATA
Step
10
...
5
PROC Step(s)
• PROC steps typically read SAS data sets
to create reports.
SAS Data Set
Descriptor
Report
PROC
Step
11
...
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.
12
6
Components of a Step
• A statement usually starts with a keyword
and always ends in a semicolon (;)
KEYWORD . . . ;
13
7
Components of a PROC Step
• A PROC step starts with a PROC statement
and ends with a RUN statement.
Step Boundaries
SAS steps begin with a
DATA statement
PROC statement.
8
Step Boundaries
data work.staff;
infile 'raw-data-file';
input LastName $ 1-20 FirstName $ 21-30
JobTitle $ 36-43 Salary 54-59;
run;
17
data work.staff;
infile 'raw-data-file';
input LastName $ 1-20 FirstName $ 21-30
JobTitle $ 36-43 Salary 54-59;
run;
9
SAS Syntax Rules
SAS statements are free-format.
One or more blanks or special characters can be
used to separate words.
They can begin and end in any column.
A single statement can span multiple lines.
Several statements can be on the same line.
data work.staff;
infile 'raw-data-file';
input LastName $ 1-20 FirstName $ 21-30
JobTitle $ 36-43 Salary 54-59;
run;
proc means data=work.staff;
class JobTitle; var Salary;run; 19
SAS Comments
Type /* to begin a comment.
Type your comment text.
Type */ to end the comment.
/* Create work.staff data set */
data work.staff;
infile 'raw-data-file';
input LastName $ 1-20 FirstName $ 21-30
JobTitle $ 36-43 Salary 54-59;
run;
20
10
Syntax Errors
Syntax errors include
misspelled keywords
invalid options.
daat work.staff;
infile 'raw-data-file';
input LastName $ 1-20 FirstName $ 21-30
JobTitle $ 36-43 Salary 54-59;
run;
proc print data=work.staff
run;
proc means data=work.staff average max;
class JobTitle;
var Salary;
run; 21
Lab Exercise: Part 2
Column
Variable
Field
Observation Row
Record
22
11
SAS Data Set and Variable
Names
•SAS names
– can be 32 characters long.
– can be uppercase, lowercase, or mixed-
case.
– must start with a letter or underscore.
Subsequent characters can be letters,
underscores, or numeric digits.
23
• Example:
proc print data=work.staff;
run;
24
12
SAS Data Libraries
You can think of a SAS data library as a drawer in a
filing cabinet and a SAS data set as one of the file
folders in the drawer.
FILES
LIBRARIES
25
Assigning a Libref
•Regardless of which
host operating system
you use, you identify
SAS data libraries by
assigning each a library
reference name (libref).
libref
26
13
SAS Data Libraries
When you invoke SAS, you automatically have
access to a temporary and a permanent SAS data
library.
• ia - permanent library
ia
Assigning a Libref
• You can use the LIBNAME statement to
assign a libref to a SAS data library.
• General form of the LIBNAME statement:
LIBNAME libref 'SAS-data-library' <options>;
14
Reading a SAS Data Set
General form of a DATA step:
DATA output-SAS-data-set;
SET input-SAS-data-set;
additional SAS statements
RUN;
29
Assignment Statements
An assignment statement
• evaluates an expression
• assigns the resulting value to a variable.
variable=expression;
30
15
SAS Expressions
An expression contains operands and operators that
form a set of instructions that produce a value.
Operands are Operators are
variable names symbols that request
constants. arithmetic calculations
SAS functions.
31
Using Operators
Selected operators for basic arithmetic
calculations in an assignment statement:
Operator Action Example Priority
+ Addition Sum=x+y; III
- Subtraction Diff=x-y; III
* Multiplication Mult=x*y; II
/ Division Divide=x/y; II
** Exponentiation Raise=x**y; I
- Negative prefix Negative=-x; I
32
16
Using SAS Functions
You can use functions in executable DATA step
statements anywhere that an expression can
appear.
data contrib;
set prog2.donate;
Total=sum(Qtr1,Qtr2,Qtr3,Qtr4);
run;
33
34
17
Using Date Functions
You can use SAS date functions to
• create SAS date values
• extract information from SAS date values.
35
18
What Is a SAS Format?
• A format is an instruction that SAS uses to write
data values.
• SAS formats have the following form:
<$>format<w>.<d>
Number of
Indicates a
decimal places
character
format
Required
Format name
delimiter
Total width (including
decimal places and
special characters)
37
SAS Formats
Selected SAS formats:
w.d standard numeric format
8.2 Width=8, 2 decimal places: 12234.21
$w. standard character format
$5. Width=5: KATHY
38
19
Formatting Data Values
• To apply a format to a specific SAS
variable, use the FORMAT statement.
• General form of the FORMAT statement:
• Example:
proc print data=ia.empdata;
format Salary dollar11.2;
run;
Defining Titles
• You use titles to enhance reports.
• General form of the TITLE statement:
Examples:
title1 'Flight Crew Employee Listing';
title2 'Employee Review';
40
20
Defining Titles
Features of titles:
• Titles appear at the top of the page.
• The default title is The SAS System.
System
• The value of n can be from 1 to 10.
• An unnumbered TITLE is equivalent to TITLE1.
• Titles remain in effect until they are changed,
cancelled, or you end your SAS session.
• The null TITLE statement, title;, cancels all
titles.
41
42
21
Using SAS System Options
Selected SAS system options:
NUMBER specifies that page numbers be printed
(default) on the first line of each page of output.
NONUMBER specifies that page numbers not be
printed.
PAGENO=n specifies a beginning page number (n)
for the next page of SAS output.
Example:
options nodate nonumber ls=72;
43
44
22
Generating HTML Files
• Output is directed to the specified HTML file until
you
– close the HTML destination
– specify another destination file.
HTML File
ods html file='…';
proc print… report
proc means…
proc freq… report
ods html close;
report
45
46
23
Additional Resources
• The Little SAS Book: A Primer, by
Delwiche and Slaughter (4th edition)
• SAS E-Learning free from Illinois
WebStore (webstore.illinois.edu)
– Programming 1
– Programming 2
– SQL 1: Essentials
47
24