0% found this document useful (0 votes)
8 views24 pages

SAS Basics %28review%29 - big

SAS is a comprehensive software suite used for data management, analysis, and reporting, consisting of various components such as data access, reporting, and analytical development. It utilizes a structured programming approach with DATA and PROC steps to manipulate and analyze data, generating outputs like reports and logs. The document also covers file types, syntax rules, and options for formatting and generating reports in SAS.

Uploaded by

newtondr7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

SAS Basics %28review%29 - big

SAS is a comprehensive software suite used for data management, analysis, and reporting, consisting of various components such as data access, reporting, and analytical development. It utilizes a structured programming approach with DATA and PROC steps to manipulate and analyze data, generating outputs like reports and logs. The document also covers file types, syntax rules, and options for formatting and generating reports in SAS.

Uploaded by

newtondr7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

What Is SAS?

SAS is a collection of components that enable you


to manage, manipulate, and examine your data.
Reporting Data Access
User
and and
Interfaces
Graphics Management

Application
Analytical Base SAS
Development

Visualization Business Web


and Discovery Solutions Enablement

SAS Windowing Environment


Interactive windows enable you to interface with SAS.

1
Submitting a SAS Program
When you execute a SAS program, the output generated
by SAS is divided into two major parts:

SAS log contains information about the processing of


the SAS program, including any warning and
error messages.
SAS output contains reports generated by SAS
procedures and DATA steps.

Lab Exercise: Part 1

Types of Files Used with SAS

Raw SAS
Data Program
Files Files
SAS
DATA
Sets

2
Turning Data into Information
Raw DATA
Data Step

SAS PROC
Data Steps
Sets

Information

Raw Data Files


• are nonsoftware-specific files
that contain records and fields
Raw • can be created by a variety of
Data software products
Files
• 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.
6

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

SAS Program Files


• contain SAS program
SAS code
Program
Files • can be saved and re-
used
• file extension is .sas

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

Components of a DATA Step


• A DATA step starts with a DATA statement
and ends with a RUN statement.
Start data _______________ ;
_______________ ;
. . .
_______________ ;
End run;
14
...

7
Components of a PROC Step
• A PROC step starts with a PROC statement
and ends with a RUN statement.

Start proc _______________ ;


_______________ ;
. . .
_______________ ;
End run;
15
...

Step Boundaries
SAS steps begin with a
 DATA statement
 PROC statement.

SAS detects the end of a step when it encounters


 a RUN statement (for most steps)
 a QUIT statement (for some procedures)
 the beginning of another step (DATA statement or
PROC statement).
16

8
Step Boundaries
data 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;

proc means data=work.staff;


class JobTitle;
var Salary;
run;

17

SAS Syntax Rules


SAS statements
 usually begin with an identifying keyword

 always end with a semicolon.

data 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;


class JobTitle;
var Salary;
run;
18

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;

/* Produce listing report of work.staff */


proc print data=work.staff;
run;

20

10
Syntax Errors
Syntax errors include
 misspelled keywords

 missing or invalid punctuation

 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

SAS vs. Other Terminology


Data Library Folder
Directory

Data Set File

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

Browsing the Data Portion


• General form of the PRINT procedure:
• PROC PRINT DATA=SAS-data-set;
RUN;

• 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.

• work - temporary library


work

•sasuser - permanent library sasuser

• ia - permanent library
ia

You can create and access your own


permanent libraries.
27

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>;

Rules for naming a libref:


 must be 8 characters or less

 must begin with a letter or underscore

 remaining characters are letters, numbers, or


underscores.
28
Lab Exercise: Part 3 & 4

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;

By default, the SET statement reads all of the


• observations from the input SAS data set
• variables from the input SAS data set.

29

Assignment Statements
An assignment statement
• evaluates an expression
• assigns the resulting value to a variable.

General form of an assignment statement:

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;

proc print data=contrib noobs;


run;

33

Functions That Compute Statistics


Selected functions that compute sample
statistics based on a group of values
include
• SUM function (total of values)
• MEAN function (average of values)
• MIN function (lowest value)
• MAX function (highest value).

34

17
Using Date Functions
You can use SAS date functions to
• create SAS date values
• extract information from SAS date values.

35

Date Functions: Extracting Information


YEAR(SAS-date) extracts the year from a SAS date
and returns a four-digit value for
year.
QTR(SAS-date) extracts the quarter from a SAS
date and returns a number from 1
to 4.
MONTH(SAS-date) extracts the month from a SAS
date and returns a number from 1
to 12.
WEEKDAY(SAS-date) extracts the day of the week from
a SAS date and returns a number
from 1 to 7, where 1 represents
Sunday, and so on.
36

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

COMMAw.d commas in a number


COMMA9.2 Width=9, 2 decimal places: 12,234.21
DOLLARw.d dollar signs and commas in a number
DOLLAR10. Width=10, 2 decimal places: $12,234.21

38

19
Formatting Data Values
• To apply a format to a specific SAS
variable, use the FORMAT statement.
• General form of the FORMAT statement:

FORMAT variable(s) format;

• Example:
proc print data=ia.empdata;
format Salary dollar11.2;
run;

Lab Exercise: Part 5 39

Defining Titles
• You use titles to enhance reports.
• General form of the TITLE statement:

TITLEn 'text ';

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

Using SAS System Options


Selected SAS system options:
DATE prints the date and time the SAS
(default) session began at the top of each page
of the SAS output.
NODATE does not print the date and time the
SAS session began.
LINESIZE=width specifies the line size for the SAS log
LS=width and SAS output.
PAGESIZE=n specifies the number of lines (n) that
PS=n can be printed per page of SAS output.

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

Generating HTML Files


• The ODS HTML statement opens, closes, and
manages the HTML destination.
• General form of the ODS HTML statement:

ODS HTML FILE='HTML-file-specification' <options>;


SAS code that generates output
ODS HTML CLOSE;

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

Creating an HTML Report


1. Open an HTML destination for the listing report.
2. Generate the report.
3. Close the HTML destination.
ods html file=‘C:\salary report.html';
proc print data=st.empdata label noobs;
label Salary='Annual Salary';
format Salary money. Jobcode $codefmt.;
title1 'Salary Report';
run;
ods html close;

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

You might also like