Professional Documents
Culture Documents
Tutorials
Paper 268-29
SUGI 29
Tutorials
SELECT *
FROM USSALES;
QUIT;
(see output #1 for results)
SUGI 29
Tutorials
QUIT;
(see output #5 for results)
All operators that IF uses (= , <, >, NOT, NE, AND, OR, IN, etc)
BETWEEN AND
CONTAINS or ?
IS NULL or IS MISSING
=*
LIKE
ANOTHER CASE
The CASE statement has much of the same functionality as an IF statement. Here is yet another variation on the CASE
expression:
PROC SQL;
SELECT STATE,
CASE
WHEN SALES > 20000 AND STORENO
IN (33281,31983) THEN CHECKIT
ELSE OKAY
SUGI 29
Tutorials
END AS SALESCAT
FROM USSALES;
QUIT;
(see output #8 for results)
REMERGING
Remerging occurs when a summary function is used without a GROUP BY. The result is a grand total shown on every line:
PROC SQL;
SELECT STATE, SUM(SALES) AS TOTSALES
FROM USSALES;
QUIT;
(see output #10 for results)
CALCULATING PERCENTAGE
Remerging can also be used to calculate percentages:
PROC SQL;
SELECT STATE, SALES,
(SALES/SUM(SALES)) AS PCTSALES
FORMAT=PERCENT7.2
FROM USSALES;
QUIT;
(see output #12 for results)
Check your output carefully when the remerging note appears in your log to determine if the results are what you expect.
SUGI 29
Tutorials
SUGI 29
Tutorials
PROC SQL;
SELECT STATE, STORE,
SUM(SALES) AS TOTSALES
FROM USSALES
GROUP BY STATE, STORE
WHERE TOTSALES > 500;
QUIT;
(see output #17 for results)
INNER JOIN
A Conventional or Inner Join combines datasets only if an observation is in both datasets. This type of join is similar to a
DATA step merge using the IN Data Set Option and IF logic requiring that the observations key is on both data sets (IF ONA
AND ONB).
PROC SQL;
SUGI 29
Tutorials
SELECT *
FROM GIRLS, BOYS
WHERE GIRLS.STATE=BOYS.STATE;
QUIT;
(see output #21 for results)
IN SUMMARY
PROC SQL is a powerful data analysis tool. It can perform many of the same operations as found in traditional SAS code, but
can often be more efficient because of its dense language structure.
PROC SQL can be an effective tool for joining data, particularly when doing associative, or three-way joins. For more
information regarding SQL joins, reference the papers noted in the bibliography.
CONTACT INFORMATION
Your comments and questions are valued and encouraged. Contact the author at:
Katie Minten Ronk
Systems Seminar Consultants
2997 Yarmouth Greenway Drive
Madison, WI 53713
Phone: (608) 278-9964
SUGI 29
Tutorials
Email: kronk@sys-seminar.com
Web: www.sys-seminar.com
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and
other countries. indicates USA registration.
Other brand and product names are trademarks of their respective companies.
SUGI 29
Tutorials
OUTPUT #1 (PARTIAL):
67$7(6$/(66725(12
&200(17
6725(1$0
:,
6$/(6:(5(6/2:%(&$86(2)&203(7,72566$/(
521
69$/8(5,7(6725(
:,
6$/(66/2:(57+$11250$/%(&$86(2)%$':($7+(5
35,&('60$57*52&(56
:,
$9(5$*(6$/(6$&7,9,7<5(3257('
9$/8(&,7<
OUTPUT #2 (PARTIAL):
67$7(7276$/(6
ddddddddddddddd
0,
,/
OUTPUT #3 (PARTIAL):
52:67$7(6$/(6
:,
:,
:,
OUTPUT #4 (PARTIAL):
5(*,216$/(67$;
OUTPUT #5 (PARTIAL):
STATE
TAX
REBATE
:,
:,
:,
0,
SUGI 29
Tutorials
OUTPUT #6 (PARTIAL):
5(32572)7+(866$/(6
$028172)
67$7(6$/(67$;
:,
:,
:,
0,
35(3$5('%<7+(0$5.(7,1*'(37
OUTPUT #7 (PARTIAL):
67$7(6$/(6&$7
:,$9*
:,/2:
:,+,*+
0,9(5<+,*+
OUTPUT #8 (PARTIAL):
67$7(6$/(6&$7
:,2.$<
:,2.$<
:,2.$<
0,&+(&.,7
OUTPUT #9:
67$7(7276$/(6
,/
0,
:,
OUTPUT #11:
67$7(7276$/(6
:,
:,
:,
0,
7276$/(6
10
SUGI 29
Tutorials
STATE
SALES PCTSALES
________________________
WI
10103.23
5.86%
WI
9103.23
5.28%
WI
15032.11
8.71%
MI
33209.23
19.2%
NOTE: The query requires remerging summary
Statistics back with the original data.
STATE
SALES
--------------IL
32083.22
IL
22223.12
IL
20338.12
IL
10332.11
MI
33209.23
OUTPUT #14 (PARTIAL):
REGION
TAX
---------------312
516.6055
313
1604.161
313
1111.156
319
1016.906
352&64/
6(/(&767$7(6$/(66$/(6
$67$;
)520866$/(6
:+(5(67$7(,1
2+
,1
,/
$1'7$;!
(55257+()2//2:,1*&2/8016:(5(127)281',17+(
&2175,%87,1*7$%/(67$;
127( 7KH6$66\VWHPVWRSSHGSURFHVVLQJWKLVVWHSEHFDXVH
RIHUURUV
67$7(6$/(67$;
:,
:,
:,
,/
11
SUGI 29
Tutorials
*5283%<67$7(6725(
:+(5(7276$/(6!
(5525([SHFWLQJRQHRIWKHIROORZLQJ
__ ! !! (4*(*7/(/7
1(A a $1'25_
+$9,1*25'(5
7KHVWDWHPHQWLVEHLQJLJQRUHG
(55257KHRSWLRQRUSDUDPHWHULVQRWUHFRJQL]HG
OUTPUT #19:
67$7(6725(127276$/(6
,/
,/
,/
,/
0,
67$7(6$/(6
,/
,/
,/
,/
OUTPUT #20(PARTIAL):
1$0(67$7(1$0(67$7(
dddddddddddddddddddddddddddddddddddddd
1$1&<:,1(':,
1$1&<:,*(1(1<
1$1&<:,$'$0&$
-($1011(':,
-($101*(1(1<
-($101$'$0&$
$0(/,$,/1(':,
$0(/,$,/*(1(1<
$0(/,$,/$'$0&$
12
SUGI 29
Tutorials
1$0(67$7(1$0(67$7(
dddddddddddddddddddddddddddddddddddddd
1$1&<:,1(':,
OUTPUT #22:
(03/2<(()(%6$/(6%(1(),76
2%6)1$0(/1$0(6725(122%667$7(6$/(66725(122%6)1$0(/1$0(&/$,06
$11%(&.(50,$11%(&.(5
&+5,6'2%6210,&+5,6'2%621
($5/),6+(5,/$//(13$5.
$//(13$5.,/%(77<-2+1621
%(77<-2+1621
.$5(1$'$06
)1$0(/1$0(&/$,066725(1267$7(
$11%(&.(50,
$//(13$5.,/
%(77<-2+1621,/
13