(Statistical Analysis System) : By: Kirtikrushna
(Statistical Analysis System) : By: Kirtikrushna
(Statistical Analysis System) : By: Kirtikrushna
By:
Kirtikrushna
SAS
(Statistical Analysis System)
CLINICAL
BANKING
INSURANCE
INTRODUCTION TO THE SAS SYSTEM :
1.Editor window
2.Output window.
3.Log window
4.Result window
5.Explorer window
1.Editor window :
Editor window contains the list of programs which has
an extension of .SAS
We can type any no of programs in editor window
We can execute all programs at a time or individually
2.OUT PUT window
Results of program will be displayed in output window
which has an extension of .LIST
3.LOG WINDOW
Suppose if there are any errors or warnings in the program
those messages will be displayed in log window
It displays the licensed agreement of SAS Version no of
variables , no of observations
4.Result window:
It displays result of all the programs in editor window
No extension for result window
5.Explorer window
Contains Libraries and Mycomputer
SAS LANGUAGE :
DATA steps
PROC steps
DATA STEP:
A DATA step consists of a group of statements in
the SAS language that can
INPUT <var1><var2>….<varn>;
CARDS;
Data values
;
RUN;
EG:
data temp;
input name $ no;
datalines;
hari 102
ravi 104
ganesh 105
kiran 109
;
run;
SAS DATA SETS:
A SAS data set consists of the following:
-descriptor information
-data values.
Syntax:
Proc <Procedure name>;
Statement 1
Statement 2
.
.
.
Statement n
;
Run;
EG:
proc print data=temp;
Run;
1)Numerical Data(0-9)
2)Character Data (A-Z)
Best db storage:
Text
Excel
Access
DB2
Oracle
Tera Data
LIBRARIES:
There are 2 ways of creating libraries.
1.Menu driven
2.Programming coding
1.Menu driven
Explorer
|
Right click
|
New
2.Through programming
Editor window
LIBNAME <Name of library> <path>;
LIBNAME Hari “D:\Ganesh”;
Example:
To delete Library:
Libname Guru clear;
RULES FOR SAS STATEMENTS:
There are only a few rules for writing SAS statements:
SAS names are used for SAS data set names, variable
names, and other items. The following rules apply:
INPUT BUFFER
PROGRAM DATA VECTOR:
INPUT BUFFER:
Input buffer is a logical area in memory into which SAS reads
each record of raw data when SAS executes an INPUT
statement.
Input stack
Data temp;
Input name $ no;
Cards;
Hari 101
;
%let list=name;
proc print data=temp;
var &list;
run;
Word scanner
compiler
Data temp
Input stack
;
Input name $ no;
Cards;
Hari 101
;
%let list=name;
proc print data=temp;
var &list;
run;
“The process that SAS uses to extract words and
symbols from the input stack to word scanner is
called tokenization.”
Tokenization is performed by a component of SAS called
the word scanner.
The word scanner starts at the first character in the
input stack and examines each character in turn.
Literal
a string of characters enclosed in quotation marks.
Number
digits, date values, time values, and hexadecimal
numbers.
Name
a string of characters beginning with an underscore or
letter.
Special
any character or group of characters that have special
meaning to SAS. Examples of special characters include:
What Are the SAS Language Elements?
Renaming variables
Selecting only the first or last n observations for
processing
Dropping variables from processing or from the
output data set
Specifying a password for a data set.
Syntax for Data Set Options
(option-1=value-1<...option-n=value-n>)
data temp;
amount=1145.32;
put amount dollar10.2;
run;
$1,145.32
Informats
De.nition of an Informat
An informat is an instruction that SAS uses to read data
values into a variable.
For example, the following value contains a dollar sign and
commas:
$1,000,000
To remove the dollar sign ($) and commas (,) before storing
the numeric value 1000000 in a variable, read this value with
the COMMA11. informat.
Syntax of an Informat
De.nition of Functions
A SAS function performs a computation or system
manipulation on arguments and returns a value.
Syntax of Functions:
function-name (argument-1<...,argument-n>)
x=max (cash,credit);
x=sqrt(1500);
Statements
Definition of Statements
A SAS statement is a series of items that may include
keywords, SAS names, special characters, and operators.
All SAS statements end with a semicolon.
INPUT, List
PUT , DATALINES
DO Iterative
DO Until
DO While
SELECT, DROP
MERGE ,SET
FILE , LENGTH
Sum, END
OUTPUT, KEEP, DATA, RETAIN
SAS System Options
data dates;
input name $ Bdate: ddmmyy8. ;
format Bdate: ddmmyy8.;
cards;
hari 21-10-84
ravi 22-11-86
;
run;
Date Informats:
_ name
_ type
_ length
_ informat
_ format
_ label
DIFFERENT WAYS TO READ DATA:
You can place data directly in the job stream with the
programming statements that make up the DATA step.
datalines;
1023 David Shaw red 189 165
1049 Amelia Serrano yellow 145 124
1219 Alan Nance red 210 192
1246 Ravi Sinha yellow 194 177
1078 Ashley McKnight red 127 118
;
3.DATA IN A SAS DATA SET
You can also use data that is already stored in a SAS data set
as input to a new data set.
_ SET statement
_ MERGE statement
Data Temp;
Set weight_club;
Run;
2.DATA IN AN EXTERNAL FILE:
data employees;
set dblib.employees;
run;
DATA SET OPTIONS:
Data set options specify actions that apply only to the SAS
data set with which they appear. They enable you to perform
operations such as these:
KEEP:
This example uses the KEEP= data set option in the SET
statement to read only the variables that represent the in
Set Statement:
Data samp;
Set weight_club (Keep= IdNumber Team);
Run;
DROP:
Use the DROP= option to create a subset of a larger data set
when you want to specify which variables are being excluded
rather than which ones are being included. The following
DATA step reads all of the variables from the data set
weight_club except for those that are specified with the
DROP= option, and then creates a data set named A1.
Data A1;
Set weight_club (Drop= IdNumber Name);
Run;
OBS= :
data s1 ;
set weight_club(obs=3);
run;
Firstobs=:
data s1 ;
set weight_club(obs=4 firstobs=2);
run;
RENAME=:
PW= :
Data tmp;
set weight_club (where=(Name ="David Shaw"));
run;
IN=:
Data statement:
Begins a DATA step and provides names for any output
SAS data sets.
data example1 ;
set weight_club;
run;
When Not Creating a Data Set
data _NULL_;
set weight_club;
put Name ;
run;
CARDS Statement:
Indicates that data lines follow
FORMAT Statement:
Associates formats with variables
INFORMAT Statement:
Associates informats with variables
data two1;
input ename $ eid hiredate ;
informat hiredate mmddyy8.;
datalines;
hari 101 12/01/05
ravi 102 11/03/06
;
run;
DATALINES4 Statement: or Cards4:
dm log ‘clear’;
KEEP Statement:
data average;
set weight_club;
keep name team;
run;
LABEL Statement:
data rtest;
set weight_club;
label name=teamname;
run;
LENGTH Statement:
data testlength;
input firstname$ lastname$ n1 n2;
length name $25 ;
datalines;
Alexander Robinson 35 11
;
INPUT Statement:
data scores;
data test;
input name $ age @@;
datalines;
John 13 Monica 12 Sue 15 Stephen 10
Marc 22 Lily 17
;
The INPUT statement in this DATA step uses the & format
modifier with list input to read character values that contain
embedded blanks.
DATA AMPERS;
INPUT NAME & $25. AGE GENDER : $1.;
DATALINES;
RASPUTIN 45 M
BETSY ROSS 62 F
ROBERT LOUIS STEVENSON 75 M
;
PROC PRINT DATA=AMPERS;
TITLE 'Example 4';
RUN;
BY Statement:
Controls the operation of a SET, MERGE, MODIFY, or
UPDATE statement in the DATA step and sets up special
grouping variables.
Processing BY-Groups:
BY statement.
In all other cases, LAST.variable has a value of 0.
Grouping Observations by City, State, Zip Code, and Street
data cnt;
set demo3;
by eid;
if first.eid then ct=0;
ct+1;
if last.eid;
run;
FILE Statement:
PUT Statement:
data response(drop=time1-time3);
set sulfa;
time=time1;
output;
time=time2;
output;
time=time3;
output;
run;
IF Statement:
if sex=’F’;
IF-THEN/ELSE Statement:
LIBNAME Statement:
Associates or disassociates a SAS data library with a libref
(a shortcut name);
retain c1 c2;
if patname='ramu' then
c1=patid;
run;
INFILE Statement:
data cnt;
set demo3;
by eid;
if first.eid then ct=0;
ct+1;
if last.eid;
run;
END Statement:
Ends a DO group
do;
statements
end;
DO Statement, Iterative:
data tc;
do i=1 to 10;
i+1;
end;
run;
DO UNTIL Statement:
Data temp;
n=0;
do until(n>=8);
put n=;
n+1;
end;
run;
DO WHILE Statement:
n=0;
do while(n<5);
put n=;
n+1;
end;
MERGE Statement:
data benefits.qtr1;
merge benefits.jan benefits.feb;
run;
Example 2: Match-Merging This example shows how to
combine observations from two data sets into a single
observation in a new data set according to the values of
a variable that is specified in the BY statement:
data inventry;
merge stock orders;
by partnum;
run;
FUNCTIONS:
Defenition of Functions:
Syntax of Functions:
The syntax of a function is
function-name (argument-1<, ...argument-n>)
Scan Function:
data n;
sd='hari kris ram ganesh';
cs=scan(sd,4);
run;
INDEX Funtion:
Data samp;
gd='ganesh hari';
sc=index(gd,'r');
Run;
LENGTH Function :
Example:
In the below example it returns the length of ‘ganesh hari’
into a new variable.
data temp3;
fg='ganesh hari';
ds=length(fg);
run;
SUB STRING Function:
Data samp1;
sg='ganesh';
dp=substr(sg,1,5);
Run;
UPCASE Function:
DATA temp1;
ps='ganesh';
tp=upcase(ps);
Run;
LOWCASE Function:
Data temp2;
rr='GANESH';
ss=lowcase(rr);
Run;
LEFT Function:
Data demo1;
a=’ DUE DATE’;
b=left(a);
put b;
Run;
MAX Function:
Data acnt;
x=max(8,3);
Run;
MEAN Function:
Data demo2;
x1=mean(2,.,.,6);
Run;
Data demo3;
x2=mean(1,2,3,2);
Run;
2
MIN Function:
x=min(7,4);
4
MOD Function
x1=mod(10,3);
put x1 ;
1
INPUT Function:
Prints the date and time that the SAS session was initialized