Professional Documents
Culture Documents
Procsql PDF
Procsql PDF
Coders' Corner
Paper 70-27
An Introduction to SAS
PROC SQL
Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois
Abstract
This paper introduces SAS users with at least a
basic understanding of SAS data sets to the SAS
SQL Procedure. The subjects discussed are (1)
extracting specific data items and observations from
data sets and views (2) creating and organizing new
tables and views (3) creating and grouping summary
statistics (4) joining two or more data tables (5)
assigning data to SAS Macro variables (6) comparing
and contrasting PROC SQL with the SAS DATA Step
and other SAS Procedures.
1
2
3
4
5
6
7
8
9
10
11
12
13
101
101
101
102
102
102
103
103
103
103
104
104
104
25MAY01
01JUN01
08JUN01
30JUL01
06AUG01
13AUG01
24JUL01
31JUL01
07AUG01
14AUG01
22AUG01
29AUG01
05SEP01
PROC SQL;
CREATE TABLE BP AS
SELECT PATIENT, DATE, BPS, BPD
FROM VITALS;
QUIT;
PULSE TEMP
72
75
74
81
77
78
77
77
78
75
72
69
71
98.5
98.6
98.5
99.0
98.7
98.7
98.3
98.5
98.6
99.2
98.8
99.1
98.9
BPS BPD
130
133
136
141
144
142
137
133
140
147
128
131
127
88
92
90
93
97
93
79
74
80
89
83
86
82
Page 1 of 6
SUGI 27
Coders' Corner
PROC SQL;
CREATE TABLE VISITS AS
SELECT PATIENT, DATE
FROM VITALS
ORDER BY PATIENT, DATE DESCENDING;
QUIT;
DATE FORMAT=DATE7.
PATIENT DATE
101
101
101
103
103
103
103
08JUN01
01JUN01
25MAY01
14AUG01
07AUG01
31JUL01
24JUL01
BPS
BPD
136
133
130
147
140
133
137
90
92
88
89
80
74
79
TEMPC
36.944444444
37
36.944444444
37.333333333
37
36.944444444
36.833333333
PROC SQL;
CREATE TABLE MEANBP AS
SELECT COUNT(*) AS N, ROUND(MEAN(BPS),
0.01) FORMAT=6.2 AS BPSMEAN,
ROUND(MEAN(BPD),0.01) FORMAT=6.2 AS
BPDMEAN
FROM VITALS;
QUIT;
BPSMEAN
BPDMEAN
13
136.08
86.62
Page 2 of 6
SUGI 27
Coders' Corner
PATIENT
DATE
101
101
101
102
102
102
103
103
103
103
104
104
104
3
3
3
3
3
3
4
4
4
4
3
3
3
08JUN01
25MAY01
01JUN01
30JUL01
06AUG01
13AUG01
31JUL01
07AUG01
24JUL01
14AUG01
05SEP01
29AUG01
22AUG01
BPD
BPDHIGH BPDPCT
90
88
92
93
97
93
74
80
79
89
82
86
83
92
92
92
97
97
97
89
89
89
89
86
86
86
97.83
95.65
100.00
95.88
100.00
95.88
83.15
89.89
88.76
100.00
95.35
100.00
96.51
Joining Tables
PATIENT
DATE
103
101
102
4
3
3
14AUG01
01JUN01
06AUG01
BPDHIGH
89
92
97
Page 3 of 6
SUGI 27
Coders' Corner
MED
1
2
3
4
5
6
7
8
9
10
11
12
Med
Med
Med
Med
Med
Med
Med
Med
Med
Med
Med
Med
102
102
102
103
103
103
104
104
104
105
105
105
30JUL01
06AUG01
13AUG01
24JUL01
31JUL01
08AUG01
22AUG01
29AUG01
05SEP01
18JUN01
25JUN01
02JUL01
A
A
A
B
B
B
A
A
A
B
B
B
2
3
2
3
3
3
2
2
2
1
2
1
1.2
1.0
1.2
3.5
3.5
3.5
1.5
1.5
1.5
4.5
3.0
5.0
mg
mg
mg
mg
mg
mg
mg
mg
mg
mg
mg
mg
The two main points to note are the FROM and the
ON clauses. The FROM clause contains both source
tables and corresponding aliases, A for VITALS, and
B for DOSING. The keywords INNER JOIN specify
this type of join. The ON statement specifies one or
more key columns (Akin to the BY variables in a
DATA step Merge) and any related logical operators
(In this case = and AND). Column attributes remain
unchanged unless they are altered in the SELECT
statement. If an attempt is made to output columns
with the same name from more than one table an
error results. In the SELECT clause the aliases
denote which source table the data item is read from.
This is the resulting table named BOTH:
30JUL01
06AUG01
13AUG01
24JUL01
31JUL01
22AUG01
29AUG01
05SEP01
PULSE MED
81
77
78
77
77
72
69
71
102
102
102
102
102
102
102
102
102
Med
Med
Med
Med
Med
Med
Med
Med
A
A
A
B
B
A
A
A
DOSES
AMT
2
3
2
3
3
2
2
2
1.2
1.0
1.2
3.5
3.5
1.5
1.5
1.5
30JUL01
30JUL01
30JUL01
06AUG01
06AUG01
06AUG01
13AUG01
13AUG01
13AUG01
PULSE MED
81
81
81
77
77
77
78
78
78
Med
Med
Med
Med
Med
Med
Med
Med
Med
DOSES
AMT
3
2
2
2
2
3
3
2
2
1.0
1.2
1.2
1.2
1.2
1.0
1.0
1.2
1.2
A
A
A
A
A
A
A
A
A
PROC SQL;
CREATE TABLE BOTH AS
SELECT A.PATIENT,
A.DATE FORMAT=DATE7. AS DATE, A.PULSE,
B.MED, B.DOSES, B.AMT FORMAT=4.1
FROM VITALS A INNER JOIN DOSING B
ON (A.PATIENT = B.PATIENT) AND
(A.DATE = B.DATE)
ORDER BY PATIENT, DATE;
QUIT;
102
102
102
103
103
104
104
104
PATIENT DATE
PATIENT DATE
DATE
25MAY01
01JUN01
08JUN01
30JUL01
06AUG01
13AUG01
24JUL01
31JUL01
07AUG01
14AUG01
22AUG01
29AUG01
05SEP01
PULSE
72
75
74
81
77
78
77
77
78
75
72
69
71
MED
Med
Med
Med
Med
Med
A
A
A
B
B
Med A
Med A
Med A
DOSES
AMT
.
.
.
2
3
2
3
3
.
.
2
2
2
.
.
.
1.2
1.0
1.2
3.5
3.5
.
.
1.5
1.5
1.5
SUGI 27
Coders' Corner
102
102
103
103
103
103
104
104
104
.
.
.
.
30JUL01
06AUG01
13AUG01
24JUL01
31JUL01
22AUG01
29AUG01
05SEP01
PULSE MED
.
.
.
.
81
77
78
77
77
72
69
71
Med
Med
Med
Med
Med
Med
Med
Med
Med
Med
Med
Med
DOSES AMT
B
B
B
B
A
A
A
B
B
A
A
A
3
1
2
1
2
3
2
3
3
2
2
2
3.5
4.5
3.0
5.0
1.2
1.0
1.2
3.5
3.5
1.5
1.5
1.5
06AUG01
13AUG01
24JUL01
31JUL01
07AUG01
14AUG01
22AUG01
29AUG01
05SEP01
77
78
77
77
78
75
72
69
71
Med
Med
Med
Med
A
A
B
B
Med A
Med A
Med A
3
2
3
3
.
.
2
2
2
1.0
1.2
3.5
3.5
.
.
1.5
1.5
1.5
PROC SQL;
CREATE TABLE FULL AS
SELECT A.PATIENT,
A.DATE FORMAT=DATE7. AS DATE, A.PULSE,
B.MED, B.DOSES, B.AMT FORMAT=4.1
FROM VITALS A FULL JOIN DOSING B
ON (A.PATIENT = B.PATIENT) AND
(A.DATE = B.DATE)
ORDER BY PATIENT, DATE;
QUIT;
.
.
.
.
25MAY01
01JUN01
08JUN01
30JUL01
PULSE MED
.
.
.
.
72
75
74
81
Med
Med
Med
Med
DOSES AMT
B
B
B
B
Med A
3
1
1
2
.
.
.
2
3.5
5.0
4.5
3.0
.
.
.
1.2
Page 5 of 6
SUGI 27
Coders' Corner
Editing Observations
90);
INSERT INTO VITALS
Concluding Summary
One of the main advantages of PROC SQL over a
DATA step is faster execution speed. Run time
performance is improved still further by using
indexes. PROC SQL is also very convenient for
performing table joins compared to a DATA step
merge. SQL calls can be nested and can be used to
access external databases such as Oracle or DB2.
SAS OPTIONS settings are valid within a PROC SQL
QUIT block. A DATA Step is better suited for
sequential observation-by -observation processing.
Further information about PROC SQL is available
from the SAS HELP menus and from the Guide to
SAS PROC SQL manual.
SAS is a registered trademark of the SAS Institute
Inc. In the USA and other countries. indicates USA
registration.
Page 6 of 6