Anda di halaman 1dari 24

Chapter 1

Introdution to Base Sas

The Goal

SAS Base is a very versatile and flexible programming environment. Our goal is to
learn SAS at a level that we can conduct Survival Analysis.
Also, to provide the students with a crash course for the basic understanding of SAS
Base environment

What is Base SAS?

It is a software suit for advanced analytics, business intelligence, data management


and predictive analytics.

SAS is short for Statistical Analysis System. The company dropped the longer name
once the abbreviation, SAS, became more common than the original one.

When someone is talking about SAS, she may be referencing


SAS Institute
SAS software
SAS language

Section 1

SAS LANGUAGE

SAS programs

Many software applications are either menu driven, or command driven (enter a
command - see the result)
SAS is a statement-driven language. You use a list of statements as instructions to
write SAS programs.

Definition: A SAS Program is a sequence of statements executed in order to


accomplish a certain task.

SAS Enterprise Guide or SAS JMP are point-and-click environments for writing SAS
programs but they don't offer the flexibility of Base SAS.

An Everyday Analogy to SAS Programs


I would like to make a withdrawal
My account number is 1234
I would like $200.
Give me five 20s and two 50s.

I would like to make a withdrawal


I would like $200.
Give me five 20s and two 50s.
My account number is 1234

You first say what you want to do, then give all the information teller needs.
The order of the details may change, but what you want to do must be always the
first.

A SAS Program Example


*Readanimalsweightsfromfile;-SAS Comment
DATAanimals;
INFILEc:\MyRawData\Zoo.dat;-SAS Statement
INPUTLionsTigers;
-SAS Statement
PROCPRINTDATA=animals;-SAS Statement /*Printtheresults*/
-SAS Comment
RUN;-SAS Statement

Very important:
Every SAS statement ends with a semicolon (;).

SAS Programming Rules

Firstruleistherearentmanysyntaxrules.Theonlyrealrule
iseverystatementendswithasemicolon.
Thesyntaxandformatareveryflexible.
Itisnotcasesensitive.
Statementscancontinueonthenextline.Also,therecanbe
morethanonestatementononeline.
Statementscanstartonanycolumnofthetexteditor.
SinceSASisthisflexible,itiseasytowriteSAScodethat
cannotberecognizedevenbythecoder.Weneedtobetidyand
organized:
Useindention
Usenamingconvention
Usecasingconvention(partofnamingconvention)

Some Naming Examples

Use the same rules in the same code, even in your career if possible.
numPrice, numPrice_Toys, numPrice_Hotel_Stay
charName, charName_City, charName_Baseball_Team
name, nameCity, nameBaseballTeam
calories_sugar, fiber_sugar, protein_sugar

a = b * c, instead write
discountAmount = priceItem * discountPercentage

(If m=civic then b=Honda), instead write


If model_bought = civic then make_bought = Honda

Section 2

SAS DATA SETS

Variables and Observations


Variable (also called columns)

Observation
(also called rows)

ID

Name

Height

Weight

53

Susie

42

41

54

Charlie

46

55

55

Calvin

40

35

56

Lucy

46

52

57

Dennis

44

43

50

58

Data point (or cell) Calvin shows the value for the 3rd observation and second
variable Name.

Data Types
ID

Name

Height

Weight

53

Susie

42

41

54

Charlie

46

55

55

Calvin

40

35

56

Lucy

46

52

57

Dennis

44

43

50

58

Two data types:


Numeric
Character
Null value for character is shown with a blank cell
Null value for numeric is shown with a dot.

Rules for SAS Names

Names must be 32 characters or fewer.


Names must start with a letter or an underscore (_) .
Names can contain upper- and lowercase letters.
Names can contain only letters, numbers or underscore. No %$!*&#@, please.

Using naming conventions can make your life easier if a need for debugging or
returning back to your code arises.

Section 3

TWO PARTS OF A SAS PROGRAM

An Example
DATA Step

PROC Step

DATA distance;
Miles = 26.22;
Kilometers = 1.61 * Miles;
RUN;
PROC PRINT DATA = distance;
RUN;

Output of the Example

Common Properties of
SAS Programs

They are made up of statements.


Some statements work in DATA and not PROC step or vice versa.
Remember this to avoid falling into a common pitfall of using the wrong statement for
the wrong step.
DATA steps read, modify and combine data
PROC steps analyze data, perform utility functions, or print reports.
A step ends when:
There is a new step keyword (i.e. DATA or PROC)
There is a RUN statement

Different Characteristics
DATA Steps

PROC Steps

begin with DATA statements

begin with PROC statements

Read, modify and combine data

perform specific analysis or functions

ceate a SAS data set

produce results or report

can include IF-THEN, DO statements

Not very common to include IF-THEN, DO


statements

Reminder: Keep in mind that, the above table is only a simplification.


Because SAS is so flexible, the differences between DATA and PROC
steps are more blurry.

DATA Steps Built-in Loop

DATA steps execute line by line and then observation by observation.

You dont need to tell SAS to execute this loop. It is handled automatically as
opposed to many coding languages. It is like T-SQL in that sense more than C#.

Section 4

SAS DATA LBRARES

Libraries

Before you can use a SAS data set, you have to tell SAS where to find it by setting up
a SAS library.
A SAS library is simply a location where SAS data sets are stored. It is like an
address or a reference for a physical storage unit.
Ways to set up a library:
Using LIBNAME statement
Using New Library window
You can navigate libraries using the Explorer window.

Active Libraries Window

Libraries on Start-up
SASHELP
SASUSER
WORK:
It is the default library
It is the temporary storage location for SAS datasets.
All data in this library is deleted once you end you SAS session.

Creating a New Library

By New Library window


To open it: (In Base SAS but not in SAS University)
Left click on the Active Libraries window (to make it active), choose New from the file
menu -OR Right click in the Active Libraries window and choose New from the pop-up menu.
In the New Library window,
Type the name of the library you want to create.
To the Path field, enter the complete path or directory of where you want to store the data sets.
By libname statement:
LIBNAME <library-name> <library-path>;
Can be tricky sometimes.
The name can be at most 8 characters and can contain letters, numerals or underscore, it cant
start with numerals.

Other Functions

Viewing data sets in the viewtable window


Contents window
Viewtable window
Changing Column headings
Column options
Viewing the properties of data sets with SAS Explorer
Opening the Properties window
General tab
Columns tab

Anda mungkin juga menyukai