You are on page 1of 18

PROC SQL

Phil Vecchione

• Today the SQL parser that is used by most databases are bases on SQL92 standards. . based on different databases. • In 1986 the first unified SQL standard (SQL-86) was created.SQL • Structured Query Language • Developed by IBM in the early 1970‟s • From the 70‟s to the late 80‟s there were different types of SQL.

Proc SQL • Added to the Base SAS package in version 6 • Implemented to allow people familiar with database to use SQL features within SAS • A “language within a language” .

gender from work.demographics where gender=„M‟ group by race. quit. age. select study. patient. .Anatomy of A PROC SQL Statement proc SQL. race.

But The SAS Data Step Already Does That…. • • • • • • • SAS SQL Create dataset • Create tables • Update values Update values • Delete Records Delete Records Append new records • Insert New Records • Create New Create New Variables variables • Sort Data Sort data • Join tables Merge datasets So what’s so cool about proc SQL? .

as a single object • Because of this difference SQL can easily do a few things that are more difficult to do in SAS .The Power Of SQL • SQL looks at datasets differently from SAS – SAS looks at a dataset one record at a time. using an implied loop that moves from the first record to the last – SQL looks at all the records.

Power of SQL: SQL Functions • There are a number of built in functions in SQL that can be used in a select statement • Because of how SQL handles a dataset. these functions work over the entire dataset • Functions: – – – – – Count: Counts Values Sum: Sums Values Max: Identifies the largest value Min: Identifies the smallest value Mean: Averages the values .

SQL Functions: Example 12 13 14 15 proc sql.pat_survey1 quit. select count(*) as Records from orcl. RECORDS -------19 .

Power of SQL: Group By • Similar to the BY parameter used in SAS • Groups the SQL observations by the variable defined • When used with the SQL functions allows summary information on groupings rather then the entire dataset .

quit.Group By: Example 21 22 23 24 25 proc sql. SITE_N RECORDS ---------------107 1 998 1 2310 2 2344 1 . select site_n.pat_survey1 group by site_n. count(*) as Records from orcl.

Loading Macro Variables • A great feature of Proc SQL is that you can load a value or values from a SQL statement into a macro variable • Can put a specific value into a macro variable for use throughout your program • Coupled with the SQL functions. you can load calculated values into a macro variable .

35294 The mean age is: 33. %put The mean age is: &meanage.Loading Macro Variables: Example 43 44 45 46 47 48 50 proc sql. AVG -----------33. select mean(rhin_age) into: meanage from orcl.35294 .pat_survey1 where rhin_age is not null. quit.

allowing for joins to occur between values .Power of SQL: Merging Between Two Values • A merge using a SAS data step requires that the variable described in the BY parameter have an EXACT match • SQL joins can contain NON-EXACT parameters for a join • Thus.

Merging Between Two Values: Example Drug Dosing Drug Concentrations Patient Visit Date Conc 1 4/14/2003 12 2 4/10/2003 3 3 4/4/2003 99 Patient Start Drug End Drug 1 4/13/2003 4/15/2003 1 4/19/2003 4/21/2003 2 3/22/2003 3/25/2003 2 4/9/2003 4/11/2003 3 3/1/2003 3/3/2003 3 5/9/2003 5/11/2003 .

visit_date.patient=d. Patient Start Date End Date 1 4/13/2003 4/15/2003 1 4/19/2003 4/21/2003 2 3/22/2003 3/25/2003 2 4/9/2003 4/11/2003 3 3/1/2003 3/3/2003 3 5/9/2003 5/11/2003 .start_date. c.Merging Between Two Values: Example Drug Dosing Drug Concentrations Patient Visit Date Conc 1 4/14/2003 12 2 4/10/2003 3 3 4/4/2003 99 proc sql. drug_dosing d where c.d. quit.patient.end_date from drug_conc c. d.conc. c. select c.patient.

start_date. drug_dosing d where c.d.conc.patient=d.patient and (d.end_date from drug_conc c.start_date le c.end_date). c. Drug Dosing Patient Start Date End Date 1 4/13/2003 4/15/2003 1 4/19/2003 4/21/2003 2 3/22/2003 3/25/2003 2 4/9/2003 4/11/2003 3 3/1/2003 3/3/2003 3 5/9/2003 5/11/2003 . c.Merging Between Two Values: Example Drug Concentrations Patient Visit Date Conc 1 4/14/2003 12 2 4/10/2003 3 3 4/4/2003 99 select c.visit_date le d.patient. d.visit_date.

References • Books – SAS Guide to the SQL Procedure – SQL for Dummies • Papers – SQL for People Who Don‟t Think They Need SQL: Erin Christen (PharmaSUG 2003) .

Thank You Any Questions? .