PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.

Slides:



Advertisements
Similar presentations
Haas MFE SAS Workshop Lecture 3:
Advertisements

© Abdou Illia MIS Spring 2014
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Introduction to Structured Query Language (SQL)
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
Introduction to Structured Query Language (SQL)
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Database Systems More SQL Database Design -- More SQL1.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
Introduction to Structured Query Language (SQL)
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Introduction to SQL Structured Query Language Martin Egerhill.
Copyright 2007, Paradigm Publishing Inc. BACKNEXTEND 3-1 LINKS TO OBJECTIVES Save a Filter as a Query Save a Filter as a Query Parameter Query Inner, Left,
Chapter 3: Combining Tables Horizontally using PROC SQL 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 1Advanced Tables, Relationships, Queries, and Forms Chapter 3Advanced Query Techniques.
Concepts of Database Management, Fifth Edition
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
SAS SQL Part 2 Alan Elliott. Dealing with Missing Values Title "Dealing with Missing Values in SQL"; PROC SQL; select INC_KEY,GENDER, RACE, INJTYPE, case.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
Chapter 9 Joining Data from Multiple Tables
ITBIS373 Database Development
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
Using Special Operators (LIKE and IN)
Chapter 4 Multiple-Table Queries
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Tutorial 9 Using Action Queries and Advanced Table Relationships.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
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.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 12 Subqueries and Merge Statements
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
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.
Sorting and Joining.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
 CONACT UC:  Magnific training   
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Session 1 Retrieving Data From a Single Table
More SQL: Complex Queries,
Relational Database Design
Chapter 6: Set Operators
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
David M. Kroenke and David J
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Combining Data Sets in the DATA step.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
SQL set operators and modifiers.
UNION Operator keywords Displays all rows from both the tables
Manipulating Data Lesson 3.
Presentation transcript:

PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science in Predictive Analytics Program

Data Processing Terminologies Across Data Sciences…

Why PROC SQL or What Can It Do For Analysts? Generate reports Generate summary statistics Retrieve data from tables or views Combine data from tables or views Create tables, views, and indexes Update the data values in PROC SQL tables Update and retrieve data from database management system (DBMS) tables Modify a PROC SQL table by adding, modifying, or dropping columns PROC SQL can be used in an interactive SAS session or within batch programs, and it Can include global statements, such as TITLE and OPTIONS.

An Example of Extracting, Summarizing, and Printing Using Data Step title 'Large Countries Grouped by Continent'; proc summary data=sql.countries; where Population > ; class Continent; var Population; output out=sumPop sum=TotPop; run; proc sort data=SumPop; by totPop; run; proc print data=SumPop noobs; var Continent TotPop; format TotPop comma15.; where _type_=1; run; /* Extracting and summarizing */ /* Sorting to arrange the output */ /* Printing */

Creating The Same Using PROC SQL proc sql; title 'Population of Large Countries Grouped by Continent'; select Continent, sum(Population) as TotPop format=comma15. from sql.countries where Population gt group by Continent order by TotPop; quit;

Countries Table

WordCityCoords Table

USCityCoords Table

UnitedStates Table

PostalCodes Table

Worldtemps Table

Oilprod Table

OILRSRVS Table

CONTINENTS Table

FEATURES Table

SELECT statement

Three Important Aspects – Describe, Print, Quit /* Helps understand the structure of the table */ PROC SQL; Describe table sql.unitedstates; Quit;

SELECT means PRINTING is Included Unless SELECT * /* all columns */ SELECT city, state /* specific columns */ SELECT distinct continent /* specific columns but avoid dup */ So it is possible to run this

The output is…

Suppress column headings…

Calculated columns and alias name…

Retrieving Data From Multiple Tables Means we are JOINING tables If there is no JOIN statement, it means (1) Cartesian product of records [no subset condition ] or (2) inner joins [ we need some subset condition] Alias names can be used for tables too; it helps simplify calling specific columns of a table

SELECT … FROM table1, table2; A Cartesian Product

Order the output from INNER JOIN INNER JOIN can be used explicitly

INNER JOIN with comparison values on another column…

Effect of Null Values on JOINS

NOT MISSING option

Multicolumn JOINS

Columns are directly comparable between two tables… Capitals FROM sql.unitedstates City FROM sql.uscitycoord Postalcodes FROM sql.postalcodes

Is it possible to do SELFJOIN?

Two Types of OUTERJOIN – LEFTJOIN and RIGHTJOIN

FULLJOIN …

SPECIALTY JOINS

NATURAL is applicable for both LEFT and RIGHT JOIN. The purpose is to reduce verbose to match on multiple common columns… Gives the same output; Non matching rows have missing values

Use COALESCE to combine multiple columns to create new matching variables

Using SUB QUERY or NESTED QUERY – SINGLE VALUE =

Correlated SUBQUERY = NESTED QUERY

Where “EXISTS” option

Multiple NESTED QUERY

Combine a JOIN with a SUBQUERY

QUERY strategies…

UNION is ROWWISE (PROC APPEND), while JOIN is COLUMNWISE (MERGE by) Keep the dups

OUTER UNION = KEEP ONLY FROM – Key word EXCEPT

To overlay data better: keyword CORRESPONDING