SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 26 By Tasha Chapman, Oregon Health Authority.

Slides:



Advertisements
Similar presentations
Haas MFE SAS Workshop Lecture 3:
Advertisements

Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
SAS PROCs ISYS 650. PROC Statement Syntax PROC name options; Statements statement options; … RUN;
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
MySQL and PHP By Trevor Adams.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
Introduction to Structured Query Language (SQL)
Basic And Advanced SAS Programming
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Data Preparation for Analytics Using SAS Gerhard Svolba, Ph.D. Reviewed by Madera Ebby, Ph.D.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Introduction to SQL Structured Query Language Martin Egerhill.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
SAS SQL SAS Seminar Series
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 3 Single-Table Queries
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
PROC SQL Phil Vecchione. 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.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
1 Efficient SAS Coding with Proc SQL When Proc SQL is Easier than Traditional SAS Approaches Mike Atkinson, May 4, 2005.
Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Chapter 12 Subqueries and Merge Statements
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Online Programming| Online Training| Real Time Projects | Certifications |Online Classes| Corporate Training |Jobs| CONTACT US: STANSYS SOFTWARE SOLUTIONS.
 CONACT UC:  Magnific training   
PROC SQL Double the Syntax, Double the Fun By Tasha Chapman.
Using Structured Query Language (SQL) NCCS Applications –MS Access queries (“show SQL”) –SAS (PROC SQL) –MySQL (the new dataserver) –Visual Foxpro Other.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 8, 13, & 24 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 14 & 19 By Tasha Chapman, Oregon Health Authority.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 16 & 17 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 5 & 6 By Ravi Mandal.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.
SAS and Other Packages SAS can interact with other packages in a variety of different ways. We will briefly discuss SPSSX (PASW) SUDAAN IML SQL will be.
Session 1 Retrieving Data From a Single Table
Chapter 10: Accessing Relational Databases (Self-Study)
Chapter 5 Introduction to SQL.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Database Systems: Design, Implementation, and Management Tenth Edition
JDBC.
David M. Kroenke and David J
Combining Data Sets in the DATA step.
Contents Preface I Introduction Lesson Objectives I-2
Database Systems: Design, Implementation, and Management Tenth Edition
Presentation transcript:

SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 26 By Tasha Chapman, Oregon Health Authority

Topics covered…  Basic syntax of PROC SQL  Select  From  Where  Order By  Creating tables  Joining tables  Group functions  CASE / WHEN logic

What is SQL? SQL stands for Structured Query Language Used for modifying and querying databases Found in many different platforms Oracle MS Access SPSS SQL Server SAS

What can you do with SQL? Data step Proc Print Data step merge Proc Freq, Proc Means, Proc Report, Proc Tabulate, etc. Proc Sort

Basic SQL Syntax

Basic SQL Query Proc SQL; Select Title, Author, ISBN From Books; Quit;

Basic SQL Query Proc SQL; Select Title, Author, ISBN From Books; Quit;

TitleAuthorISBN The Little SAS BookDelwiche SAS Survival HandbookWiseman SAS for DummiesMcDaniel Learning SAS by ExampleCody Output Delivery SystemHaworth X SAS Functions by ExampleCody Annotate: Simply the BasicsCarpenter SAS Programming ShortcutsAster Survival Analysis Using SASAllison X Longitudinal Data and SASCody SAS Macro ProgrammingBurlew

Where Clause Proc SQL; Select Title, Author, ISBN From Books Quit; Where Author = ‘Cody’;

TitleAuthorISBN The Little SAS BookDelwiche SAS Survival HandbookWiseman SAS for DummiesMcDaniel Learning SAS by ExampleCody Output Delivery SystemHaworth X SAS Functions by ExampleCody Annotate: Simply the BasicsCarpenter SAS Programming ShortcutsAster Survival Analysis Using SASAllison X Longitudinal Data and SASCody SAS Macro ProgrammingBurlew

TitleAuthorISBN Learning SAS by ExampleCody SAS Functions by ExampleCody Longitudinal Data and SASCody

Proc SQL; Select Title, Author, ISBN From Books Quit; Where Author = ‘Cody’; Select the variables (columns) you want to keep Identify the table(s) the data comes from Indicate which observations (rows) you want to select Basic SQL Query

Proc SQL; Select Title, Author, ISBN From Books Quit; Where Author = ‘Cody’; Separate variables with commas Only one semi-colon at the end of the query Basic SQL Query End with QUIT; not RUN;

Select *  Use an asterisk to select all the available columns in a table Proc SQL; Select * From Books Quit;

Renaming variables  Use AS to rename a variable or name a newly created variable Proc SQL; Select ISBN as Book_ID From Books Quit;

Renaming variables  Use AS to rename a variable or name a newly created variable Proc SQL; Select ISBN as Book_ID, Price*0.8 as Sale_Price From Books Quit;

Labels and Formats  Labels and Formats can be applied to variables in the SELECT clause Proc SQL; Select Customer label='Ordered by:', Order_Date format=mmddyy10. From Books Quit;

Functions  SAS functions and other similar manipulations can be implemented in PROC SQL Proc SQL; Select Customer label='Ordered by:', year(Order_Date) as Order_Yr From Books Quit;

Order By Clause Proc SQL; Select Title, Author, ISBN From Books Quit; Order by Title; Where Author = ‘Cody’

TitleAuthorISBN Learning SAS by ExampleCody SAS Functions by ExampleCody Longitudinal Data and SASCody

TitleAuthorISBN Learning SAS by ExampleCody Longitudinal Data and SASCody SAS Functions by ExampleCody

Creating Tables

Creating Datasets Proc SQL; Create Table Newdata as Select Title, Author, ISBN From Books Quit; Where Author = ‘Cody’;

Referencing Libraries Proc SQL; Create Table Newdata as Select Title, Author, ISBN From in.Books Quit; Where Author = ‘Cody’; Libname in ‘C:\SAS\Chapmantl\’;

Referencing Libraries Libname oralib oracle user = sas_user password = my_password path = “dev.cbs.or.us” connection = unique; Libname in ‘C:\SAS\Chapmantl\’;

Joining Tables

TitleAuthorISBN The Little SAS BookDelwiche SAS Survival HandbookWiseman SAS for DummiesMcDaniel Learning SAS by ExampleCody Output Delivery SystemHaworth X SAS Functions by ExampleCody Annotate: Simply the BasicsCarpenter SAS Programming ShortcutsAster Survival Analysis Using SASAllison X Longitudinal Data and SASCody SAS Macro ProgrammingBurlew ISBNOrder Date /23/ X07/07/ /09/ /24/ X11/05/ /30/ /25/2009

Joining Tables Proc SQL; Create Table Newdata as Select books.Title, books.Author, books.ISBN, orders.order_date From books join orders on books.ISBN = orders.ISBN; Quit;

TitleAuthorISBNOrder Date SAS for DummiesMcDaniel /25/2009 Learning SAS by ExampleCody /23/2009 Output Delivery SystemHaworth X07/07/2007 SAS Functions by ExampleCody /09/2008 SAS Programming ShortcutsAster /24/2008 Survival Analysis Using SASAllison X11/05/2007 Longitudinal Data and SASCody /30/2008

Joining Tables - Inner Joins OrdersBooks Books that have been Ordered

Joining Tables Proc SQL; Create Table Newdata as Select books.Title, books.Author, books.ISBN, orders.order_date From books join orders on books.ISBN = orders.ISBN; Quit;

Joining Tables Proc SQL; Create Table Newdata as Select b.Title, b.Author, b.ISBN, o.order_date From books as b join orders as o on b.ISBN = o.ISBN; Quit;

TitleAuthorISBN The Little SAS BookDelwiche SAS Survival HandbookWiseman SAS for DummiesMcDaniel Learning SAS by ExampleCody Output Delivery SystemHaworth X SAS Functions by ExampleCody Annotate: Simply the BasicsCarpenter SAS Programming ShortcutsAster Survival Analysis Using SASAllison X Longitudinal Data and SASCody SAS Macro ProgrammingBurlew ISBNOrder Date /23/ X07/07/ /09/ /24/ X11/05/ /30/ /25/2009 BookNum

Joining Tables Proc Sort data = Orders; by ISBN; run; Proc Sort data = Books; by ISBN; run; Proc Datasets library = work; modify Orders; rename BookNum = ISBN; run; quit; Data NewDat; Merge Books Orders; by ISBN; run;

Joining Tables Proc SQL; Create Table Newdata as Select b.Title, b.Author, b.ISBN, o.order_date From books as b join orders as o on b.ISBN = o.BookNum; Quit;

TitleAuthorISBN The Little SAS BookDelwiche SAS Survival HandbookWiseman SAS for DummiesMcDaniel Learning SAS by ExampleCody Output Delivery SystemHaworth X SAS Functions by ExampleCody Annotate: Simply the BasicsCarpenter SAS Programming ShortcutsAster Survival Analysis Using SASAllison X Longitudinal Data and SASCody SAS Macro ProgrammingBurlew ISBNOrder Date /23/ X07/07/ /09/ /24/ X11/05/ /30/ /25/ /05/2009

TitleAuthorISBNOrder Date SAS for DummiesMcDaniel /25/2009 SAS for DummiesMcDaniel /05/2009 Learning SAS by ExampleCody /23/2009 Output Delivery SystemHaworth X07/07/2007 SAS Functions by ExampleCody /09/2008 SAS Programming ShortcutsAster /24/2008 Survival Analysis Using SASAllison X11/05/2007 Longitudinal Data and SASCody /30/2008

Joining Tables Proc SQL; Create Table Newdata as Select b.Title, b.Author, b.ISBN, o.order_date From books as b join orders as o on b.ISBN = o.BookNum; Quit;

Joining Tables Proc SQL; Create Table Newdata as From Books as b, Orders as o Quit; Where b.ISBN = o.ISBN; Select b.Title, b.Author, b.ISBN, o.order_date

Joining Tables – Inner Joins OrdersBooks Books that have been Ordered

Joining Tables – Left Joins OrdersBooks Books that have been Ordered

Joining Tables – Left Joins Proc SQL; Create Table Newdata as Select b.Title, b.Author, b.ISBN, o.order_date From books as b left join orders as o on b.ISBN = o.ISBN; Quit;

TitleAuthorISBNOrder Date The Little SAS BookDelwiche SAS Survival HandbookWiseman SAS for DummiesMcDaniel /25/2009 Learning SAS by ExampleCody /23/2009 Output Delivery SystemHaworth X07/07/2007 SAS Functions by ExampleCody /09/2008 Annotate: Simply the BasicsCarpenter SAS Programming ShortcutsAster /24/2008 Survival Analysis Using SASAllison X11/05/2007 Longitudinal Data and SASCody /30/2008 SAS Macro ProgrammingBurlew

Joining Tables – Right Joins OrdersBooks Books that have been Ordered

Joining Tables – Full Joins OrdersBooks Books that have been Ordered

Group Functions

 One of the most basic ways to summarize data  Group functions return one result per group of rows processed  Examples include basic statistics like  SUM  MEAN  COUNT  MIN/MAX  Use the GROUP BY clause to indicate how to group the records

Tests Table DateStudent_IDSubjectSessionScore 01/25/10A12MathA96 01/25/10B34MathA92 01/25/10C56MathA68 01/25/10D75MathA79 03/26/10B34ScienceA96 03/26/10C56ScienceA82 04/23/10A12ReadingA84 04/23/10B34ReadingA94 04/23/10C56ReadingA78 04/23/10D75ReadingA81 05/22/10A12MathB92 05/22/10B34MathB94 05/22/10C56MathB72 05/22/10D75MathB81 04/15/10B34ScienceB94 04/15/10C56ScienceB84 06/01/10A12ReadingB88 06/01/10B34ReadingB96 06/01/10C56ReadingB82 06/01/10D75ReadingB79 48

Proc SQL; Select Student_ID, mean(Score) as Avg_Score From Tests Group By Student_ID; Quit; 49 Group Functions

Student_IDAvg_Score A12 90 B C D Group Functions

 Can group by more than one variable  Every field in the SELECT statement that is not being grouped, must also be listed in the GROUP BY clause  Even if the field does not result in additional groupings

Proc SQL; Select Student_ID, Session, mean(Score) as Avg_Score From Tests Group By Student_ID, Session; Quit; 52 Group Functions

Student_IDSessionAvg_Score A12 A90 A12 B90 B34 A94 B34 B94.67 C56 A76 C56 B79.33 D75 A80 D75B80 53 Group Functions

Proc SQL; Select Student_ID, Session, mean(Score) as Avg_Score From Tests Group By Student_ID, Session Having Avg_Score lt 80; Quit; 54 Having Clause

Student_IDSessionAvg_Score C56 A76 C56B Having Clause

CASE / WHEN Logic

Tests Table DateStudent_IDSubjectSessionScore 01/25/10A12MathA96 01/25/10B34MathA92 01/25/10C56MathA68 01/25/10D75MathA79 03/26/10B34ScienceA96 03/26/10C56ScienceA82 04/23/10A12ReadingA84 04/23/10B34ReadingA94 04/23/10C56ReadingA78 04/23/10D75ReadingA81 05/22/10A12MathB92 05/22/10B34MathB94 05/22/10C56MathB72 05/22/10D75MathB81 04/15/10B34ScienceB94 04/15/10C56ScienceB84 06/01/10A12ReadingB88 06/01/10B34ReadingB96 06/01/10C56ReadingB82 06/01/10D75ReadingB79 57 Convert “Scores” to letter grades

data NewGrade; set Tests; length Test_Grade $ 4; if 70 le Score le 79 then Test_Grade = 'C'; else if 80 le Score le 89 then Test_Grade = 'B'; else if 90 le Score le 100 then Test_Grade = 'A'; else Test_Grade = 'Fail'; run; CASE/WHEN

Case when 70 le Score le 79 then 'C' when 80 le Score le 89 then 'B' when 90 le Score le 100 then 'A' else 'Fail' end as Test_Grade CASE/WHEN

Case when 70 le Score le 79 then 'C' when 80 le Score le 89 then 'B' when 90 le Score le 100 then 'A' else 'Fail' end as Test_Grade WHEN works like “ELSE IF” CASE/WHEN

Case when 70 le Score le 79 then 'C' when 80 le Score le 89 then 'B' when 90 le Score le 100 then 'A' else 'Fail' end as Test_Grade Conclude with an “END” expression CASE/WHEN

Proc SQL; Select Date, Student_ID, Subject, Session, Score, Case when 70 le Score le 79 then 'C' when 80 le Score le 89 then 'B' when 90 le Score le 100 then 'A' else 'Fail' end as Test_Grade From Tests; Quit; 62 CASE/WHEN

Results DateStudent_IDSubjectSessionScoreTest_Grade 01/25/10A12MathA96 A 01/25/10B34MathA92 A 01/25/10C56MathA68 Fail 01/25/10D75MathA79 C 03/26/10B34ScienceA96 A 03/26/10C56ScienceA82 B 04/23/10A12ReadingA84 B 04/23/10B34ReadingA94 A 04/23/10C56ReadingA78 C 04/23/10D75ReadingA81 B 05/22/10A12MathB92 A 05/22/10B34MathB94 A 05/22/10C56MathB72 C 05/22/10D75MathB81 B 04/15/10B34ScienceB94 A 04/15/10C56ScienceB84 B 06/01/10A12ReadingB88 B 06/01/10B34ReadingB96 A 06/01/10C56ReadingB82 B 06/01/10D75ReadingB79C 63

Additional Reading PaperSummary Introduction to Proc SQL (Chapman)Introduction to the basic syntax of Proc SQL An Introduction to SQL in SAS (Lund)Another introductory paper on Proc SQL A Hands-on Tour Inside the World of Proc SQL (Lafler) Yet another introductory paper on Proc SQL Kirk’s Ten Best Proc SQL Tips and Techniques Neat features in SQL Outer Joins and WHERE ClausesDiscusses the proper way to use a WHERE clause with an outer join (left, right, or full) to prevent unintentionally excluding desired rows The Many Uses of SQL SubqueriesTwo-part series that introduces subqueries (a nested query within another query) in SQL SAS with Oracle; Writing Efficient and Accurate SQL Discusses how to write efficient and accurate SQL that will properly communicate with a non-SAS database management system (DBMS) What’s New in SAS/ACCESSPerformance boosters to speed up your queries

Read chapters 8, 13, & 24 For next week…