sas basics-1-1

27
7/21/2019 SAS Basics-1-1 http://slidepdf.com/reader/full/sas-basics-1-1 1/27 SAS Basics-1 1 May 2009 Infosys Technologies Ltd, Pune

Upload: prem

Post on 05-Mar-2016

8 views

Category:

Documents


0 download

DESCRIPTION

SAS Basics

TRANSCRIPT

Page 1: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 1/27

SAS Basics-1

1

May 2009

Infosys Technologies Ltd,

Pune

Page 2: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 2/27

► Introduction to SAS.

► Advantages of using SAS 

► SAS process overview, compilation and execution of SAS 

► 2 Basic components of SAS 

 DATA step

 PR! step

► DATA Step in detail 

Topics covered

2

Page 3: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 3/27

►What is SAS?What is SAS?

Statistical Anal"sis S"stem is a powerful language w#ic# mainl" deals ininformation deliver" products w#ic# convert raw data into a meaningfulinformation t#at is used in decision ma$ing.

►What is the SAS system?What is the SAS system?

 An integrated s"stem of software products t#at ena%les to perform

► Data entr", retrieval, and management 

► Report writing and &rap#ics

►'ualit" improvement 

► Applications development 

Introduction

3

Page 4: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 4/27

SAS is a powerful and eas"(to(use #ig# level programming s"stem. It provides

• extensive data management tools

• file #andling tools

• wide ranging statistical procedures

• compre#ensive report writing features

•cross(platform processing, data access, and data transfer capa%ilities

 SAS is an interpreted language. T#ere is no compilation or lin$ing from t#e

developer)s end. T#e s"ntax c#ec$ing, compilation and lin$ing are done at run

time.

Contd..

4

Page 5: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 5/27

SAS runs on man" platforms * +nix , -S , indows etc /. 0owever for an" SAS

 product to run , t#e Base SAS software must %e installed in t#e s"stem

 Base SAS includes t#e SAS language , DATA step programming tools and

 processing procedures. Additional SAS products ma" %e present for specific

 functionalit".

Some common additional products are

• SAS1!3!T 4 for client server capa%ilities.

• SAS1A!!3SS 4 for accessing s"%ase data.

• SAS1Intret 4 for execution of SAS from we%.

• SAS1A5 4 for full screen o%6ect oriented programming.

SAS Products

5

Page 6: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 6/27

SAS Advantages

6

• SAS runs on I. ainframes computers

 II. ini computers

 III. icro computers

 I-. or$stations

• The Multi Vendor Architecture (MVA) of SAS provides the same look

and functionality across

 I. perating s"stems

 II. T"pes of #ardware

These explain the growing popularity of SAS.

Page 7: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 7/27

Process Overview….

7

Initial input Creating theSAS data set

Analyzingthe Data

Procedureoutput

DATA step

External Files:  SAS LO  !eports  External DataFiles

P!OC step

SAS Data sets:  SAS Data "les  SAS Data #ie$s:  P!OC S%L #ie$s &'ati(e)  DATA Step #ie$s &'ati(e)

  SAS*ACCESS #ie$s&Inter+ace)

!A, Data:  External- Files  Instrea. Data!e.ote AccessThrough:  Catalog  FTP  TCP*IP Soc/et

  0!L

SASDataSet

SASDataSet

SAS LO

!eport

Page 8: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 8/27

Compilation and Eecution P!ase

8

Page 9: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 9/27

 nput !uffer" 7ogical area in memor" into w#ic# SAS reads eac# record of raw data w#en

SAS executes an IP+T statement. It created onl" w#en t#e DATA step reads raw data. *#en

t#e DATA step reads a SAS data set, SAS reads t#e data directl" into t#e program data vector./

 #rogram $ata Vector (#$V)" !reates executing SAS language statements. T#e data values are

assigned to t#e appropriate varia%les in t#e program data vector. 5rom #ere, SAS writes t#e

values to a SAS data set as a single o%servation.

 Along wit# data set varia%les and computed varia%les, t#e PD- contains two automatic

varia%les, 88 and 83RRR8.

 $escriptor nformation"  It contains, for example, t#e name of t#e data set and its mem%er

t"pe, t#e date and time t#at t#e data set was created, and t#e num%er, names and data t"pes

*c#aracter or numeric/ of t#e varia%les. 

 %xecution" T#e DATA step %egins wit# a DATA statement. 3ac# time t#e DATA statement

executes, a new iteration of t#e DATA step %egins, and t#e 88 automatic varia%le is

incremented %" 9. T#e DATA step terminates w#en SAS encounters t#e end(of(file in a SAS

data set or a raw data file.

Continued..

9

Page 10: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 10/27

Observation

VariableData Set

"nderstanding terms contd..

DataData

Values.Values.

Descriptor inforation.

Page 11: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 11/27

11

SAS Datasets and Data Tyes

•SAS can process data onl" in specific format and structure.

•!onverting t#e mainframe file into SAS dataset is t#e pre re:uisite for an" SAS processing.

•SAS dataset #as t#e format and structure w#ic# SAS can process.

•T"pes of Datasets

• Permanent 

•Temporar"

• Data T"pes used in SAS 

• umeric

•!#aracter1String 

• DAT3 

Page 12: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 12/27

 A SAS program is a series of SAS statements executed in order . A SAS program is %uilt on

two %asic components4

• DATA step

• PR! step

 $ATA and #&' steps are made up of SAS statements. 

 A step ma" #ave as few as one or as man" as #undreds of statement. 3ac#

 statement gives information or instructions to SAS.

enerally* the $ATA step manipulates data* and the #&' step analy+es*

 produces reports

Basic components o# SAS program

12

Page 13: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 13/27

 $ATA step" A data step can %e used for t#e following 

• Data can %e read from input files , in(stream, permanent1temporar"

SAS datasets

• Data processing to satisf" specific %usiness need can %e done on t#e

data read in t#e data step.

• Se:uential utput files 1 SAS file ma" %e written

• !ustomi;ed reports ma" %e produced.

T#e DATA step is an implied loop. T#is means t#at if a file is read in t#e

 data step, an automatic loop is executed till end of t#e file is reac#ed. An

 automatic s"stem generated varia%le 88 is incremented eac# time t#e loop starts.

$ATA Step

13

Page 14: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 14/27

14

%ules #or SAS names

9. Begin wit# an identif"ing $e"word *li$e DATA or PR!/ and alwa"s ends wit# a S3I!7.

2. Statements can %e in lower case or upper case.

<. SAS statements are free( format means t#e" can %egin or end in an" column.

=. SAS detects t#e %eginning of a step w#en it encounters a DATA or PR! statement.

>. SAS detects t#e end of a statement w#en it encounters a DATA or a PR! or a R+ ?. statement.

@. 7engt# of varia%les s#ould %e 9 to c#aracters.

. -aria%les s#ould start wit# a letter or 8*underscore/.

. !omments are li$e 1C C1 or C *for single line comments /.

9E. ne or more %lan$s s#ould %e placed %etween items in SAS statements. If t#e items are special

c#aracters suc# as FGF, FHF, FF, t#en %lan$s are not necessar".

Page 15: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 15/27

$ATA Step &continued'

 %xample" Read in selected fields from an input file

D!"! #$%&'!D(

  $)#$%' !*+$S"(

  $),-"  1 V!&1 3.

  1/ V!&3 04.

  19 V!&9 08.(

&-)(  ,ote "

• SAS will %uild a temporar" SAS data set called RJ.5I7R3AD

containing all records of input file

• nl" < fields as identified %" K position on file are read in. T#eir

attri%utes are as specified. T#e specifies a c#aracter data t"pe , PD stands for pac$ed

decimal. umeric format is 6ust a num%er followed %" L.) sign.• A&0IST is t#e logical file name

e.g.

 --AST  DD DSG59-IPB.SAS.TRAII& ,DISPGS0R

15

Page 16: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 16/27

$ATA Step &continued'

 %xample" Two wa"s of reading fields from input file.'1.

D!"! $)DD(

$)#$%' $)#$%'1(

$),-" 

1 *! 0+!&1.

3 D! 0+!&3. 7 !*" 0+!&5.(

'2

D!"! $)DD(

$)#$%' $)#$%'1(

$),-" *! 0 1

D! 0 35

!*" 0 711(

16

Page 17: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 17/27

$ATA Step &continued'

 %xample" Read in input file and appl"ing certain restrictions w#ile reading.

D!"! #$%&'!D(

  $)#$%' !*+$S"(

  $),-"  1 V!&1 3.

  1/ V!&2 04.

  19 V!&3 08.(  $#  V!&1 5// !)D V!&3 S!%SD!"! (

&-)(

 ,ote "

• T#e SAS temporar" file RJ.5I7R3AD will now contain onl"

  records w#ere -AR9 is greater t#an or e:ual to >EE and -AR< containsLSA7SDATA) 

17

Page 18: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 18/27

$ATA Step &continued'

 %xample" Including %usiness logic in file read data step reading.

D!"! #$%&'!D(

  $)#$%' !*+$S"(

  $),-"  1 V!&1 3.

  1/ V!&2 04.

  19 V!&3 08.(  $#  V!&1 5//

"+')  V!&4 V!&1 1.1 ( : $ncrease V!&1 b; 1/ < :

  '%S'  V!&4 V!&1 1./5( : $ncrease V!&1 b; 5 < :

&-)( 

 ,ote " All records from input file will %e read to %uild RJ.5I7R3AD.

 Based on t#e condition specified a new varia%le -AR= will %e computed. 

18

Page 19: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 19/27

$ATA Step &continued'

 %xample" Reading ISTR3A data.

D!"! Displa;=Saple(

$),-" #)!>' 0 18 S'? 0 11 !*' 1314 +" 1617 @" 2/22(

D!"!%$)'S(

,!-% > 27 72 14/A'))$#'& # 28 64 135

&')'' # 35 54 128

>!)-'% > 35 6/ 125

"O)B > 32 68 13/

(

19

Page 20: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 20/27

$ATA Step &continued'

 %xample" Reading data from comma delimited file.

DATA "ep(

I!"IL# oaCt DSD$

I!P%T

  SS) 0+!&1/.

SS)2 0+!&1/.,"ED!"' >>DDBB1/. (

2/

 %xample" Reading data from Ta% delimited file.

DATA neF2( I!"IL# tFo DSD D#LIMIT#& 'G/9G( : use HeiCecial coCe for tab Celiiters

:

I!P%T var1var1/ (

&%!$

Page 21: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 21/27

$ATA Step &continued'

 %xample" +sing t#e S3T command 

D!"! )'@(

  S'" #$%&'!D(

&-)(

 ,ote " ne record at a time is read in from RJ.5I7R3AD t#at wascreated %" some previous data step and RJ.3 is %uilt.

21

Page 22: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 22/27

$ATA Step &continued'

 %xample" riting se:uential output file

D!"!  =)-%%=(

  S'" )'@(

  #$%' O-"#$%'(

  ,-" 1 V!&1 3.

  2/ V!&2 04.  35 V!&3 08.(

&-)(

 ,ote " ne record at a time is read in from RJ.3 and is written to t#e output file

identified %" t#e Lddname) +T5I73 in -S or logical filename in +nix or P!.

 DATA 8+778 signifies t#at no temporar" RJ SAS dataset is created.t#er places in t#e file are filled wit# %lan$s

22

Page 23: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 23/27

$ATA Step &continued'

 %xample" riting permanent SAS file

D!"! O-"#$%'.#$%'1(

S'" )'@(

&-)(

 ,ote " ne record at a time is read from RJ.3 and is written to t#e output SASdata mem%er +T5I739. 5I739 data mem%er will reside in t#e SAS li%rar" as identified %"

t#e ddname +T5I73 in -S or logical li%rar" name +T5I73 in +IM or P! 

23

Page 24: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 24/27

$ATA Step &continued'

 %xample" Illustrating J33P , D373T3 and DRP statements.

D!"! )'@ I J'',  V!&1 V!&4K (

S'" #$%&'!D(

  $# V!&3 "'S" "+') D'%'"'(

&-)(

 ,ote " 

• T#e J33P statement retains onl" t#e specified varia%les in t#e

RJ.3 file. RJ.5I7R3AD is used to %uild RJ.3.

• But onl" t#ose records are retained in RJ.3 t#at did not #ave

-AR< G LT3ST) in t#e original RJ.5I7R3AD.

• Alternativel" we could #ave used DRP G -AR2 -AR<

24

Page 25: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 25/27

$ATA Step &continued'

 %xample" Illustrating R3A3 statement 

D!"! )'@ I &')!>' V!&1 V!&)'@K (

S'" #$%&'!D(

&-)(

 ,ote " •  All records from RJ.5I7R3AD will %e used to %uild RJ.3.

 0owever t#ere will %e no varia%le called -AR9 in RJ.3. Instead

it needs to %e referenced as -AR3.

25

Page 26: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 26/27

$ATA Step &continued'

oncatenating datasets 

 %xample

D!"! !(

  S'" !1 !2(

&-)(

Two SAS datasets RJ.A9 and RJ.A2 t#at #ad previousl" %een created areconcatenated to create a new concatenated dataset RJ.A

 ,ote"

• T#e total num%er of o%servations in RJ.A is t#e sum of t#e num%er of o%servations in

RJ.A9 and RJ.A2.

• If t#e datasets do not contain similar varia%les , values will %e set to missing in t#econcatenated dataset 

26

Page 27: SAS Basics-1-1

7/21/2019 SAS Basics-1-1

http://slidepdf.com/reader/full/sas-basics-1-1 27/27

27

Thank /ou000