SAS Accessing Data
SAS Accessing Data
Center of
Excellence
Agenda
Creating datasets using DATA step.
Infile statement
Different input styles.
Combining datasets using DATA step
SAS procedures
software tools for data analysis and reporting.
Macro facility
a tool for extending and customizing SAS software
programs and for reducing text in your programs.
Base SAS
data access
management
analysis
presentation
SAS catalog
Many different kinds of information that are used in a SAS job
use.
Descriptor
Portion
Data
Portion
NAME
1351
Farr, Sue
161
212
Moore, Ron
2512
Ruth, G H
...
...
...
5151
Coxe, Susan
WAGECAT
WAGERATE
S
S
S
S
3392.50
5093.75
.
1572.50
3163.00
reads raw data or existing SAS data sets to create a SAS data
set.
PROC step
A group of procedure statements used to analyze data in SAS
your SAS programs, and for reducing the amount of code that
you must enter to do common tasks. Macros are SAS files that
contain compiled macro program statements and stored text.
Descriptor
DATA Step
DATA Step
DATA Step
Sale
Sale
Amt
Amt
Mth2Dte
Mth2Dte
01APR2001
01APR2001
02APR2001
02APR2001
03APR2001
03APR2001
04APR2001
04APR2001
05APR2001
05APR2001
498.49
498.49
946.50
946.50
994.97
994.97
564.59
564.59
783.01
783.01
498.49
498.49
1444.99
1444.99
2439.96
2439.96
3004.55
3004.55
3787.56
3787.56
BirthDay
4253
SAS Function
Age
30
Salary
Salary
42000
42000
34000
34000
27000
27000
20000
20000
19000
19000
19000
19000
Div
Div
HUMRES
HUMRES
FINACE
FINACE
FLTOPS
FLTOPS
FINACE
FINACE
FINACE
FINACE
FLTOPS
FLTOPS
DATA Step
Div
Div
DivSal
DivSal
FINACE
FINACE
FLTOPS
FLTOPS
HUMRES
HUMRES
42000
42000
46000
46000
73000
73000
variables
SAS data sets
formats or informats
SAS procedures
options
arrays
statement labels
SAS macros or macro variables
SAS catalog entries
librefs or filerefs.
SAS Dates
SAS dates are special numeric values
representing the number of days between
January 1, 1960 and a specified date.
1jan1959
1jan1960
1jan1961
1jan2000
DATE9. Informat
-365
0
SAS Date
Values
366
14610
SAS Date
Values
MMDDYY10. Format
01
01
01
01
/01
/01
/01
/01
/19
/19
/19
/20
59
60
61
00
Standard Data
The term standard data refers to character and
numeric data that SAS recognizes automatically.
Some examples of standard numeric data
include
35469.93
3E5 (exponential notation)
-46859
Nonstandard Data
The term nonstandard data refers to
character and numeric data that SAS does
not recognize automatically.
Examples of nonstandard numeric data
include
12/12/2012
29FEB2000
4,242
$89,000
15OCT1999
01JUN1997
26OCT1993
16SEP1992
26NOV1996
27FEB1997
10MAY1987
06JAN2000
20AUG1991
raw
data
file
61065 A
contains
employee
91688
32639
information
for
28305
the level 1 flight
40440
attendants. Use the
39461
raw data file to
41463
create
the
40650
work.fltat1 SAS data
40950
set.
Desired Output
Obs
EmpID
Hire
Date
1
2
3
4
5
6
7
8
9
E1232
E2341
E3452
E6781
E8321
E1052
E1062
E8172
E1091
14532
13666
12352
11947
13479
13572
9991
14615
11554
Salary
Bonus
61065
91688
32639
28305
40440
39461
41463
40650
40950
3053.25
4584.40
1631.95
1415.25
2022.00
1973.05
2073.15
2032.50
2047.50
DATA
DATASAS-data-set;
SAS-data-set;
INFILE
INFILE 'raw-data-file'
'raw-data-file' <options>;
<options>;
INPUT
INPUTvariable-specification
variable-specification;
;
Formatted Input
The input style tells SAS where to find the
fields and how to read them into SAS.
INPUT
INPUT@n
@nvariable-name
variable-nameinformat.
informat....;
...;
variable-name=expression;
variable-name=expression;
descriptor information
is information that SAS creates and maintains about
DATA step
Reading External File Data
DATA step
An informat is an instruction that SAS uses to
read data values into a variable.
The INPUT statement with an informat after a
variable name is the simplest way to read values
into a variable.
$w.
DATEw.
or
ddmmmyyyy
MMDDYYw.-
mmddyyyy
w.d
COMMAw.d
Score2
Score3
Team Div
46
"Green Hornets, AtlantaAAA
19
25
"High Volts, Portland
54
"Vulcans, Las Vegas
AA
AAA
530
Option
Non-blank delimiters
DLM='delimiter(s)'
MISSOVER
DSD
Desired Output
The SAS data set should have one
observation per employee.
LName
FName
Farr
Anderson
Tennenbaum
Sue
Kay B.
Mary Ann
City
Anaheim
Chicago
Jefferson
State
CA
IL
MO
Phone
869-7008
483-3321
589-9030
Location
Sale
Date
Amount
USA
EUR
USA
USA
EUR
USA
14264
14274
14274
14280
14281
14320
3295.50
1876.30
2938.00
2908.74
3145.60
2789.10
INPUT
INPUTvar1
var1var2
var2var3
var3
@;
@;
Desired Output
EmpID
Contrib
E00973
E09872
E73150
E45671
E34805
1400
2003
2400
4500
1980
Read for
Obs. 2
Read for
Obs. 3
Process
Other
Statements
Process
Other
Statements
Process
Other
Statements
Output
Output
Output
data work.retire;
length EmpID $ 6;
infile 'raw-data-file';
input EmpID $ Contrib @@;
run;
Hold until end of
record.
Effect
Holds raw data record until
1) an INPUT statement with no trailing @
2) the bottom of the DATA step.
Holds raw data records in input buffer until
SAS reads past end of line.
Detail
Detail
Header
Header
Detail
Header
Detail
Detail
You can read a hierarchical file into a SAS data set by creating
observation per detail record and storing the header informatio
of each observation.
Hierarchical File
Header 1
Detail 1
Detail 2
Detail 3
Header 2
Detail 1
Header 3
Detail 1
Detail 2
Header
Variables
Detail
Variables
Header 1
Header 1
Header 1
Header 2
Header 3
Header 3
Detail 1
Detail 2
Detail 3
Detail 1
Detail 1
Detail 2
EmpLName
Adams
Adams
Porter
Lewis
Nicholls
Slaydon
EmpFName
Susan
Susan
David
Dorian D.
James
Marla
DepName
Michael
Lindsay
Susan
Richard
Roberta
John
Relation
C
C
S
C
C
S
RETAIN
RETAIN variable-name
variable-name<initial-value>;
<initial-value>;
Compile
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
ID
$
5
PDV
Compile
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
ID
$
5
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
...
Execute
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
ID
$
5
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
.
...
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
5 0 0 0 1
ID
$
5
4 f e b 1 9 8 9
1 3 2
5 3 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
.
...
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
5 0 0 0 1
ID
$
5
50001
4 f eb 1 9 8 9
1 3 2
5 3 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10627
.
.
132
530
.
...
Implicit return
Input Buffer
5 0 0 0 1
ID
$
5
50001
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
4 f eb 1 9 8 9
1 3 2
Implicit
output
5 3 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10627
.
530
.
Write out observation to airplanes.
.
132
...
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Implicit
output
Input Buffer
5 0 0 0 1
ID
$
5
50001
4 f eb 1 9 8 9
1 3 2
5 3 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10627
.
530
.
Write out observation to airplanes.
.
132
...
Implicit return
Input Buffer
5 0 0 0 1
ID
$
5
50001
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
4 f eb 1 9 8 9
1 3 2
5 3 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10627
.
.
132
530
.
...
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
5 0 0 0 1
ID
$
5
4 f eb 1 9 8 9
1 3 2
5 3 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
.
...
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
5 0 0 0 2
ID
$
5
50002
1 1 n o v 1 98 9
1 5 2
5 4 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10907
.
.
152
540
.
...
Implicit return
Input Buffer
5 0 0 0 2
ID
$
5
50002
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
1 1 n o v 1 98 9
1 5 2
Implicit
output
5 4 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10907
.
540
.
Write out observation to airplanes.
.
152
...
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Implicit
output
Input Buffer
5 0 0 0 2
ID
$
5
50002
1 1 n o v 1 98 9
1 5 2
5 4 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10907
.
.
152
540
.
...
Implicit return
Input Buffer
5 0 0 0 2
ID
$
5
50002
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
1 1 n o v 1 98 9
1 5 2
5 4 0
PDV
INSERVICE PASSCAP CARGOCAP
N
N
N
8
8
8
10907
.
.
152
540
.
...
data airplanes;
length ID $ 5;
infile 'raw-data-file';
input ID $
InService : date9.
PassCap CargoCap;
run;
Input Buffer
5 0 0 0 2
ID
$
5
Output of Dataset
proc print data=airplanes noobs;
run;
ID
50001
50002
50003
50004
50005
50006
In
Service
Pass
Cap
Cargo
Cap
10627
10907
11617
12088
12228
12772
132
152
168
172
170
180
530
540
530
550
510
520
Concatenating
Concatenating the data
sets appends the
observations from one
data set to another data
set. The DATA step reads
DATA1 sequentially until
all observations have
been processed, and
then reads DATA2. Data
set COMBINED contains
the results of the
concatenation.
SAS-data-set(IN=variable)
SAS-data-set(IN=variable)
where variable is any valid SAS variable name.
Variable is a temporary numeric variable with a value of:
0 to indicate false; the data set did not contribute to the
current
observation
1 to indicate true; the data set did contribute to the current
observation
Trans
D
C
C
D
C
Amnt
126.32
560
235
14.56
371.69
Branch
Num
111
112
114
115
116
Branch
M.G.Road
Sivaji Nagar
Madiwala
Koramangala
BTM
Trans
D
C
C
D
C
Amnt
126.32
560
235
14.56
371.69
Num
111
112
114
115
116
Branch
M.G.Road
Sivaji Nagar
Madiwala
Koramangala
BTM
Branch
data Transact
newtrans noactiv (drop=Trans Amnt) noacct
(drop=Branch);
merge prog2.transact(in=InTrans) prog2.branch(in=InBanks);
by ActNum;
if InTrans and InBanks then output newtrans;
else if InBanks and not InTrans then output noactiv;
else if InTrans and not InBanks then output noacct;
run;
D
C
D
C
M.G.Road
M.G.Road
Madiwala
BTM
126.32
560
14.56
371.69
Sivaji Nagar
Koramangala
235
Questions