Overview
Basic Structure
Verifying Statements
Specifying Columns
Specifying Rows
Introduction
SQL is a modular
language that uses
statements and clauses.
Basic structure of PROC SQL:
PROC SQL;
statement (select)
clauses (from, where, group by,
having, order by);
QUIT;
Statements
select - specifies the columns to be selected
procsql; procsql;
validate validate
selecttimemile,restpulse, selecttimemile,restpulse,
maxpulse maxpulse,
fromproject.fitness fromproject.fitness
wheretimemilegt7;
wheretimemilegt7;
Overview
Basic Structure
Verifying Statements
Specifying Columns
Specifying Rows
Specifying Columns
Objectives
-Displaying columns directly from a table
1017498.513690
102819914193
1027798.714497
1027898.714293
1037798.313779
PROCSQL; 1037798.513374
1037898.614080
1037599.214789
1047298.812883
SELECT* 1046999.113186
FROMVITALS;
QUIT;
Printing Specify Columns
If you do not want to print out all columns in a table in
the order that they were stored, you can specify the
columns to be printed in the order that you want them
in the SELECT statement or CASE EXPRESSION in the
select statement . PATIENTDOSEGRP
PROCSQL;
CREATETABLETESTMEDAS 101MedB
SELECTPATIENT, 101MedB
CASE((PATIENT/2= 101MedB
102MedA
INT(PATIENT/2))+
102MedA
(PATIENT=.))
102MedA
WHEN1THEN'MedA'
103MedB
WHEN0THEN'MedB'
103MedB
ELSE'Error'
103MedB
ENDASDOSEGRP
103MedB
LENGTH=5 104MedA
FROMVITALS 104MedA
ORDERBYPATIENT; 104MedA
QUIT;
Calculating Columns
We can calculate a new column by using data in an
existing column and then naming the new column
using the as function.
CalculatetheproportionofUnitsformeachcountry
CODE:
OUTPUT:
Calculated columns using SAS
Dates
OUTPUT:
Creating new columns
The use of CASE expression can be used to create a
new column
OUTPUT:
CODE:
Creating a table
To create and populate a table with the rows from
an SQL query, use create table.
State_ObsCodeState_Name
99UTUtah
procsql;
100VTVermont
create tablestatesas
101VAVirginia
selectstate_code,
102WAWashington
state_name
103WVWestVirginia
fromd2data.state;
104WIWisconsin
quit;
105WYWyoming
106N/A
Overview
Basic Structure
Verifying Statements
Specifying Columns
Specifying Rows
Specifying Rows in a table
Objectives
CODE:
OUTPUT:
OUTPUT:
Escape Clause
The escape clause allows you to designate a single
character that will indicate how proc sql will interpret
LIKE wildcards when SAS is searching within a
character string.
Example: Select observationsfromastringvariable
containinganunderscore('_').
CODE:
OUTPUT:
Subsetting calculated values
Lackofprofit
procsql; ProductIDprofit
title'Lackofprofit'; 2301005000450.7
selectProduct_ID, 2301005000680.9
2401001004331.85
((total_retail_price/quantity)- 2407002000042
costprice_per_Unit)asprofit 2402001000211.5
fromd2data.order_item 2401001000312.4
2407002000072.9
wherecalculated profit<3; 2401001002321.9
quit; 2301005000041.85
title; 2301005000041.85
240700100017-1.41
Summary
Basic Structure
PROC SQL;
statement (select)
clauses (from, where, group by, having, order by);
QUIT;
Verifying Statements
validate - used to check the select statement syntax
noexec - checks for invalid syntax in all types of SQL
statements
Specifying Columns
Displaying columns directly from a table
Displaying columns calculated from other columns
Calculating columns using a CASE expression
Specifying Rows
Selecting a subset of rows
Removing duplicate rows
Subsetting using where clauses, escape clauses, and
calculated values