Combining Data Sets in the DATA step.

Slides:



Advertisements
Similar presentations
Haas MFE SAS Workshop Lecture 3:
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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 Database Operators
SAS Programming:File Merging and Manipulation. Reading External Files (review) data barf; * create the dataset BARF; infile ’s:\mysas\Table7.1'; * open.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
1 Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
1 Combining (with SQL) HRP223 – 2010 October 27, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation.
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
Hash vs Join A case study evaluating the use of the data step hash object to replace a SQL join Geoff Ness Sep 2014.
Basic And Advanced SAS Programming
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 3: Combining Tables Horizontally using PROC SQL 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
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.
Chapter 15: Combining Data Horizontally 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
HRP Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected by copyright law and.
1 Lab 2 and Merging Data (with SQL) HRP223 – 2009 October 19, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
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.
While you are waiting for class to start... (1)Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab4.sql”. It is located on the.
SQL Select Statement IST359.
Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
INFANL01-3 ANALYSE 3 WEEK 3 March 2015 Institute voor Communication, Media en Informatietechnology.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 26 By Tasha Chapman, Oregon Health Authority.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL - Training Rajesh Charles. Agenda (Complete Course) Introduction Testing Methodologies Manual Testing Practical Workshop Automation Testing Practical.
Android Online Training AcuteSoft: India: , Land Line: +91 (0) USA: , UK : +44.
SQL – join.
Chapter 6: Set Operators
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Putting tables together
Database Systems: Design, Implementation, and Management Tenth Edition
LINQ to DATABASE-2.
Theory behind the relational engine
An Introduction to SQL.
Chapter 18: Modifying SAS Data Sets and Tracking Changes
CHAPTER 7: ADVANCED SQL.
Match-Merge in the Data Step
Creating the Example Data
JOINS (Joinining multiple tables)
LINQ to DATABASE-2.
Combining (with SQL) HRP223 – 2013 October 30, 2013
Outer Joins Inner joins returned only matching rows. When you join tables, you might want to include nonmatching rows as well as matching rows.
Inner Joins.
5 The EXCEPT Operator Unique rows from the first result set that are not found in the second result set are selected.
Lab 3 and HRP259 Lab and Combining (with SQL)
Lab 2 and Merging Data (with SQL)
Combining (with SQL) HRP223 – 2012 November 05, 2011
The INTERSECT Operator
Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Contents Preface I Introduction Lesson Objectives I-2
Appending and Concatenating Files
Subqueries.
Database Systems: Design, Implementation, and Management Tenth Edition
SQL set operators and modifiers.
UNION Operator keywords Displays all rows from both the tables
Displaying Data from Multiple Tables
JOINS (Joinining multiple tables)
Hans Baumgartner Penn State University
Relational Database Operators
Presentation transcript:

Combining Data Sets in the DATA step. Vertically –SET statement (Proc Append) Horizontally – MERGE statement

Combining Tables in SQL Vertically -- Set operators Horizontally -- Joins

Generate two data sets data tmp1 tmp2; call streaminit(54321); do id=1 to 12; chol=int(rand("Normal",240,40)); sbp=int(rand("Normal",120,20)); if id<6 then output tmp1; else output tmp2; end; run;

title "tmp1"; proc print data=tmp1 noobs;run; title "tmp2"; proc print data=tmp2 noobs;run; title "tmp1 inner join tmp2";

Combine Vertically

SQL uses set operators to combine tables vertically. proc sql; select * from tmp1 union select * from tmp2 ; quit; This produces results that can be compared to a DATA step concatenation. We will cover set operators after Joins.

Data Step uses the set statement to combine tables vertically title "Concatenation, data step"; data tot1; set tmp1 tmp2; run; proc print data=tot1 noobs;run; title;

SQL Joins combine data from multiple tables horizontally . 2 Introduction to SQL Joins SQL Joins combine data from multiple tables horizontally . inner and outer SQL joins. Compare SQL joins to DATA step merges.

Inner joins Return only matching rows Maximum of 256 tables can be joined at the same time.

Example Data data tmp1(keep=id chol sbp) tmp2(keep=id weight height); call streaminit(54321); do id=1,7,4,2,6; chol=int(rand("Normal",240,40)); sbp=int(rand("Normal",120,20)); output tmp1; end; do id=2,1,5,7,3; height=round(rand("Normal",69,5),.25); weight=round(rand("Normal",160,10),.5); output tmp2; run; title "tmp1"; proc print data=tmp1 noobs;run; title "tmp2"; proc print data=tmp2 noobs;run; title "tmp1 inner join tmp2";

Combining Data from Multiple Tables SQL uses joins to combine tables horizontally. The tables are not sorted on id, the primary key This produces results that can be compared to a DATA step merge. id appears twice, columns are not automatically overlayed title "tmp1 inner join tmp2"; proc sql; select * from tmp1,tmp2 where tmp1.id=tmp2.id ; quit;

Combining Data from Multiple Tables SQL uses joins to combine tables horizontally. The tables are not required to be sorted on the key proc sql; create table tmp3 as select * from tmp1,tmp2 where tmp1.id=tmp2.id ; select * from tmp3 quit; This produces results that can be compared to a DATA step merge.

Combining Data from Multiple Tables Simple data set merge gives a different result The tables are not sorted on id, the primary key This produces results that can be compared to a DATA step merge. proc sort data=tmp1;by id;run; proc sort data=tmp2;by id;run; data tot1; merge tmp1 tmp2; by id; run; Data step merge requires the data be sorted and have the same name for the by variable.

Combining Data from Multiple Tables SQL uses joins to combine tables horizontally. The primary key has different names on the two files This produces results that can be compared to a DATA step merge. proc sql; select tmp1.id,chol,sbp,weight,height from tmp1,tmp3 where tmp1.id=tmp3.id1 ; quit;

Combining Data from Multiple Tables Data set merge takes a bit more code The primary key has different names on the two files This produces results that can be compared to a DATA step merge. proc sort data=tmp1;by id;run; proc sort data=tmp3;by id1;run; data tot1; merge tmp1(in=one) tmp3(in=three rename=(id1=id)); by id; if one and three; run;

Outer Joins Can be performed on only two tables or views at a time. Return all matching rows, plus nonmatching rows from one or both tables Left Full Right