Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proc Sql Kelly Hardman Aug. 9, 2007 BMTRY 789. What is Proc Sql? SQL: Structured Query Language It is a procedure that combines the data and proc steps.

Similar presentations


Presentation on theme: "Proc Sql Kelly Hardman Aug. 9, 2007 BMTRY 789. What is Proc Sql? SQL: Structured Query Language It is a procedure that combines the data and proc steps."— Presentation transcript:

1 Proc Sql Kelly Hardman Aug. 9, 2007 BMTRY 789

2 What is Proc Sql? SQL: Structured Query Language It is a procedure that combines the data and proc steps of traditional SAS code It can “sort, summarize, join, and concatenate datasets, create new variables, and print the results or create a new table to view all in one step!” It can perform these functions quicker and with fewer steps than traditional SAS code

3 Proc Sql: The Basics Syntax: Title ‘ ‘; Proc Sql; Select columns From table-name Where expression Group By columns Having expression Order By columns; Quit;

4 Example Data Set Data People; length Name$ 9; length City$ 9; length Occupation$ 12; input Name$ Age Gender$ City$ State$ Occupation$; datalines; Robert 55 M Annapolis MD accountant Susan 24 F Charlotte NC nurse Gary 43 M Denver CO construction Elizabeth 15 F Miami FL student Trey 33 M Seattle WA doctor Hazel 72 F Madison WI retired Amanda 41 F Cleveland OH teacher Victoria 36 F Tucson AZ secretary Dan 61 M Rochester NY fireman Cynthia 52 F Savannah GA housewife Doug 49 M Gulfport MS lawyer Charles 37 M Detroit MI banker ; run;

5 Selecting All of the Data With traditional code, we would use proc print In proc sql, we use: proc sql; select * from People; quit; Notice the * It is used when selecting all of the columns People is the data set name. If we were using a permanent data set, we would use libname.People

6 All of the Data Proc Sql – All of the Data NameCityOccupationAgeGenderState RobertAnnapolisaccountant55MMD SusanCharlottenurse24FNC GaryDenverconstruction43MCO ElizabethMiamistudent15FFL TreySeattledoctor33MWA HazelMadisonretired72FWI AmandaClevelandteacher41FOH VictoriaTucsonsecretary36FAZ DanRochesterfireman61MNY CynthiaSavannahhousewife52FGA DougGulfportlawyer49MMS CharlesDetroitbanker37MMI

7 Selecting Some of the Data To select only certain columns or variables, just write the column names after the select statements Here we just want names, cities, and states: proc sql; select name, city, state from people; quit; Notice the commas between the column names

8 Some of the Data Proc Sql - Selecting Certain Variables NameCityState RobertAnnapolisMD SusanCharlotteNC GaryDenverCO ElizabethMiamiFL TreySeattleWA HazelMadisonWI AmandaClevelandOH VictoriaTucsonAZ DanRochesterNY CynthiaSavannahGA DougGulfportMS CharlesDetroitMI

9 Sorting Data With traditional code, we would use a proc sort followed by a proc print In proc sql, we use: proc sql; select name, age, gender from people order by gender, age asc; quit;

10 Sorted Data Proc Sql - Sorting Data NameAgeGender Elizabeth15F Susan24F Victoria36F Amanda41F Cynthia52F Hazel72F Trey33M Charles37M Gary43M Doug49M Robert55M Dan61M

11 Analyzing a Subset of Data The where statement is used in proc sql the same way it is used in traditional SAS code Here, we only want the names and occupations of the females: proc sql; select name, occupation from people where gender in ('F'); quit;

12 Subset of Data Proc Sql - Subset of Data NameOccupation Susannurse Elizabethstudent Hazelretired Amandateacher Victoriasecretary Cynthiahousewife

13 Creating a New Variable Traditionally, to create a new variable, we would have to use a series of if-then statements in the data step With proc sql: proc sql; select name, age, case when age le 20 then 'young' when age le 54 then 'middle-aged' else 'old' end as Category from people order by Age asc, Category asc; quit;

14 New Variable Proc Sql - Creating a New, Conditional Variable NameAgeCategory Elizabeth15young Susan24middle-aged Trey33middle-aged Victoria36middle-aged Charles37middle-aged Amanda41middle-aged Gary43middle-aged Doug49middle-aged Cynthia52middle-aged Robert55old Dan61old Hazel72old

15 Creating a New Table Equivalent of creating a new data set from the old data set Here, we create the table EastCoast from the table People proc sql; create table EastCoast as select name, occupation, state from people where state in ('NC','MD','FL','NY','GA'); select * from EastCoast; quit;

16 New Table Proc Sql - Creating a New Table NameOccupationState RobertaccountantMD SusannurseNC ElizabethstudentFL DanfiremanNY CynthiahousewifeGA

17 References Ronk, Katie Minten, Steve First, David Beam. “An Introduction to Proc SQL” SUGI Paper 191-27 http://www2.sas.com/proceedings/sugi27/p 191-27.pdf Vecchione, Phil “Proc SQL” http://www.google.com/search?q=cache:w E8FcgdygFIJ:www.cognigencorp.com/pers pective/tipsNtricks.pub/1/PROC%2520SQL %2520Talk_12_.ppt+proc+sql&hl=en&ct=cl nk&cd=8&gl=us&client=firefox-a

18 Questions?


Download ppt "Proc Sql Kelly Hardman Aug. 9, 2007 BMTRY 789. What is Proc Sql? SQL: Structured Query Language It is a procedure that combines the data and proc steps."

Similar presentations


Ads by Google