An Introduction
to
SAS
Programming
Last Updated : 29 June, 2004
Center of
Excellence
Agenda
Working with SAS Environment
SAS Programming Basics
Editing and Debugging SAS Programs
Setting up the SAS Session
Creating Basic Reports and Statistics
Objectives
Learn to
Work with and manage your SAS windows
Create SAS libraries
Explore and manage SAS files
Enter and submit SAS programs
Create and use file shortcuts.
Explore Window
User to
Create new libraries and SAS files
Open any SAS file Consolidation of
disparate information sources
Perform most file management tasks such
as moving, copying, and deleting files
Faster time-to-market for products and
services
Create shortcuts to non-SAS files.
Features of Program Editor
Drag and drop SAS Programs on this window
Specify the number of lines to submit at a time
Recall submitted statements
Save contents automatically
Clear contents
Turn line numbers on and off
Use the command line or menus
save your program.
Other Windows’ Features
Log
view messages about your SAS session and
any SAS programs you submit
Output
browse output from SAS programs
Result
navigate and manage output from SAS
programs view, save, and print individual
items of output
Working in SAS
Objectives
The structure and components of SAS
programs
The steps involved in processing SAS programs
The structure and components of SAS data
sets
The two types of SAS data sets
SAS libraries and the types of SAS files that
they contain
Temporary and permanent SAS libraries.
Layout of SAS Programs
SAS Programs used to access, analyze,
manage and present data
SAS Statements
Case insensitive
Free Format
Ends with a semicolon
Styles for Comment - *statement; or /* …*/
Parts of SAS Programs
Data Step
TO CREATE DATA SETS
put your data into a SAS data set
compute the values for new variables
check for and correct errors in your data
produce new SAS data sets by subsetting, merging, and updating
existing data sets.
Proc Step
PROCESS DATA SETS
print a report
produce descriptive statistics
create a tabular report
produce plots and charts.
SAS NAMES (VARIABLES & FILE
NAMES)
NAMES
1 TO 32 CHARACTERS IN LENGTH
START WITH LETTER (A THRU Z) AND _
CAN BE A COMBINATION OF NUMBERS/LETTERS
VARIABLES
CHARACTER
NUMERIC
FILENAMES
2 LEVEL
LIB - WORK/PERMANENT
Editing SAS Programs
the Program Editor window
the Enhanced Editor
the SAS Notepad window
the host editor of your choice
Processing SAS Programs
When you submit a SAS program, SAS software
reads the statements and checks them for errors.
When it encounters a DATA, PROC, or RUN
statement, SAS software stops reading statements
and executes the current step in the program. In
our sample program, each step ends with a RUN
statement.
Example:-
Data [Link] datasetname;
Set [Link] datasetname;
run;
Overview of SAS Data Sets
Conceptually, a SAS data set is a file consisting
of two parts: a descriptor portion and a data
portion. Some SAS data sets also contain an
index, which enables SAS software to locate
records in the data set.
Descriptor Portion
name of the data set
date and time the data set was created
number of observations
number of variables.
each variable's name, type, length, format, informat,
and label.
Data Portion
Data Portion
Tabular Arrangement of data
Contains Observations and Variables
Variables can be either Character or numeric
Character variables can be up to 32K long
Numeric Values stored as floating point
variables have a default length of 8 bytes
SAS can handle up to 32,767 variables
Naming Rules
SAS data set names must
be 1 to 32 characters in length
begin with a letter (A-Z, including mixed case
characters) or an underscore (_)
continue with any combination of numbers,
letters, or underscores.
Example:-
Payroll
Budget1995_1997
_Students
READING RAW DATA
DATA
START
OUTPUT DATA SET
INFILE
POINTER TO EXT FILE
INPUT
HOW TO READ
INPUT @1 DATE $9. @10 BOARDED 3.;
FLAT FILE VARIATIONS (DELIMITERS, LINE
POINTER, LINE HOLD SPECIFIERS)
Variable Formats
SAS Format & Informat
Data Value
SAS Data Value
Formatted Value
SAS Files
Table
View
Catalog
MDDB
SAS files are stored in a library.
Creating SAS Data Sets
Schedule
Creating Data Sets From Raw Files
Subsetting Data
SAS Functions
Reading Fixed Fields, Free Format files
Reading from files with multiple rows in a single line
Reading from files with a single row in multiple lines
Understanding Data Step Processing
Reading Hierarchical Files
Reading Variable Length Records
Base SAS
data access
management
analysis
presentation
Example
With SAS, we can join Oracle data on a
mainframe computer with an existing SAS
data set, create new variables (columns),
and produce an interactive graph on your
PC.
Types of Files We Can Access
SAS/ACCESS provides access to these types of files:
Relational databases
DB2 under OS/390 Informix SYBASE
Oracle Rdb DB2 under VM ODBC
ORACLE MS SQL Server DB2 under UNIX or
PC
CA-OpenIngres Teradata OLE DB
Contd…
Non-relational databases and other data
sources
ADABAS PC File Formats CA-IDMS
IMS-DL/I SYSTEM 2000 CA-DATACOM/DB
software
Contd…
Enterprise Resource Planning (ERP)
systems
Baan PeopleSoft
R/3 SAP BW
Managing Data
After we have accessed relevant data, we
can use the SAS programming language to
manipulate it any way we choose.
For example, we can
format the data
create variables (columns)
use operators to evaluate data values
use functions to create and recode data values
subset data
perform conditional processing
merge a wide range of data sources
create, retrieve, and update database information.
Analyzing Data and Presenting
Information
Once our data is in shape, we can use SAS
to analyze data and produce reports. Our
. SAS output can range from a simple listing
of a data set to customized reports of
complex relationships.
Analysis
Base SAS provides powerful data analysis tools.
For example, we can
produce tables, frequency counts, and cross-tabulation tables
create a variety of charts and plots
compute a variety of descriptive statistics, including the
mean, sum, variance, standard deviation and more
compute correlations and other measures of association, as
well as multi-way cross-tabulations and inferential statistics.
Presentation
For reporting and displaying analytical results, SAS
gives us an almost limitless number of visually
appealing output formats, such as
an array of markup languages including HTML4 and XML
output that is formatted for a high-resolution printer,
such PostScript, PDF, and PCL files
color graphs that you can make interactive using ActiveX
controls or Java applets.
RTF
Explore the SAS workspace
SAS is designed to be easy to use. It
provides windows for accomplishing all
the basic SAS tasks we need to do. When
you first start SAS, the five main SAS
windows open: the Explorer, Results,
Program Editor or Editor, Log, and Output
windows.
View the Explorer window
In the Explorer window, we can view and manage our SAS files and create shortcuts to files
that are not formatted by SAS. Use this window to
create new SAS libraries and SAS files
open any SAS file
perform most file management tasks such as moving, copying, and deleting files
create file shortcuts.
View the Editor window
We can use either of these windows to enter, edit, and
submit SAS programs:
the Program Editor window, which is available on all SAS platforms
the Editor window, which is available only in the Windows operating environment.
View the Log window
The Log window displays messages about our SAS session
and any SAS programs that we submit.
View the Output window
The Output window displays the output from SAS programs
that we submit. It automatically opens or moves to the front
of our display when we create output.
Contd…..
If we create HTML output, we can view it in the
Results Viewer window, which is the internal
browser for SAS.
View the Results window
The Results window helps us to navigate and manage output from SAS
programs that we submit. We can view, save, and print individual items of
output.
The Results window is empty until we submit a SAS program that
creates output. Then it opens or moves to the front of our display.
View the Solutions and Tools
menus
Along with windows for working with our SAS files and SAS programs,
SAS provides a set of ready-to-use solutions, applications, and tools. We
can access many of these tools by using the Solutions and Tools menus.
Create a library
You can create SAS libraries using a point-and-click
interface.
Click View Explorer.
Click File New.
In the New Library window, specify information for the new library. If you want the
library to be created at the beginning of each SAS session, click Enable at startup.
Click OK.
Contd…
Assigning a libref to reference
SAS files
/*************************************/
/* define SAS library */
/*************************************/
libname sales 'c:\Test';
/*************************************/
/* create new data set from raw data */
/*************************************/
data sales.quarter1;
length Department $ 7 Site $ 8;
input Department Site Quarter Sales;
datalines;
Parts Sydney 1 4043.97
Parts Atlanta 1 6225.26
Parts Paris 1 3543.97
Repairs Sydney 1 5592.82
Repairs Atlanta 1 9210.21
Repairs Paris 1 8591.98
Tools Sydney 1 1775.74
Tools Atlanta 1 2424.19
Tools Paris 1 5914.25
;
run;
/*************************************/
/* print new data set */
/*************************************/
proc print data=sales.quarter1;
run;
Editor
Log
Output
Library
Sales
Library & Data Sets
Assigning a libref to reference
DBMS data
/*************************************/
/* define SAS library for Oracle */
/*************************************/
libname myorlib oracle user=scott password=tiger
path="blunzer:v7" schema=hrdept;
/*************************************/
/* define SAS library for DB2 */
/*************************************/
libname mydblib db2 noprompt="user=testuser;
password=testpass;database=testdb";
/*************************************/
/* print Oracle table */
/*************************************/
proc print data=myorlib.all_employees;
where state='CA';
run;
/*************************************/
/* print DB2 table */
/*************************************/
proc print data=[Link];
where state='CA';
run;
/*************************************/
/* clear librefs */
/*************************************/
libname myorlib clear;
libname mydblib clear;
Read PC database files
If you have PC database files such as Microsoft Excel spreadsheets, Lotus spreadsheets, or Microsoft Access files, you
can use SAS to import these files and create SAS data sets. Once you have the data in SAS data sets, you can process
them as needed in SAS.
To read PC database files, you use the IMPORT procedure. PROC IMPORT reads the input file and writes the data to a SAS
data set, with the SAS variables defined based on the input records.
Example
/*************************************/
/* import the Excel file */
/*************************************/
proc import datafile="c:\test\[Link]" out=[Link]
sheet="Prices";
getnames=no;
run;
/*************************************/
/* print part of the new data set */
/*************************************/
proc print data=[Link](obs=10);
run;
/*************************************/
/* import the Access file */
/*************************************/
proc import table="customers" out=[Link] dbms=access; uid="userid";
pwd="mypassword"; database="c:\test\[Link]";
wgdb="c:\winnt\system32\[Link]";
run; /*************************************/
/* print part of the new data set */
/*************************************/
proc print data=[Link](obs=5);
run;
Read SAS data sets
DATA steps that read existing data sets using a SET
statement.
combine two or more input data sets to create one output data set
merge data from two or more input data sets that share a common
variable
update a master file based on transaction records.
Work with SAS data sets
Before we can work with our data in SAS, it must be in a special form called a SAS
data set. Conceptually, a SAS data set (also called a table) is a file containing
descriptor information and related data values. The file is organized as a table of
observations (rows) and variables (columns) that SAS can process. Some SAS
data sets also contain an index, which enables SAS to locate records in the
data set.
Contd…
To work with SAS data sets, you also need to understand how they are stored. All SAS files
are stored in a SAS library, which is a collection of files such as SAS data sets and catalogs.
In the Windows and Unix environments, a SAS library is typically a group of SAS files in the
same folder or directory.
In some operating environments, a SAS library is a physical collection of files. In others, the
files are only logically related.
Questions