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.

Slides:



Advertisements
Similar presentations
Haas MFE SAS Workshop Lecture 3:
Advertisements

Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Introduction to Structured Query Language (SQL)
1 Creating and Tweaking Data HRP223 – 2010 October 24, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to SQL Session 1 Retrieving Data From a Single Table.
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
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 3: Combining Tables Horizontally using PROC SQL 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
18b. PROC SURVEY Procedures in SAS ®. 1 Prerequisites Recommended modules to complete before viewing this module  1. Introduction to the NLTS2 Training.
SAS Efficiency Techniques and Methods By Kelley Weston Sr. Statistical Programmer Quintiles.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
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 Six Updating Data.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 CONACT UC:  Magnific training   
More SQL: Complex Queries, Triggers, Views, and Schema Modification
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Session 1 Retrieving Data From a Single Table
Retrieving Data Using the SQL SELECT Statement
More SQL: Complex Queries,
Chapter 5: Enhancing Your Output with ODS
MySQL Subquery Source: Dev.MySql.com
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Practical Office 2007 Chapter 10
Chapter 6: Modifying and Combining Data Sets
Database Systems: Design, Implementation, and Management Tenth Edition
Database application MySQL Database and PhpMyAdmin
Chapter 2: Getting Data into SAS
SAS Chapter 10 Exporting Data
Chapter 3: Working With Your Data
SAS Programming I Matthew A. Lanham Doctoral Student
Chapter 18: Modifying SAS Data Sets and Tracking Changes
Structured Query Language (SQL) William Klingelsmith
Chapter 1: Introduction to SAS
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Chapter 7: Macros in SAS Macros provide for more flexible programming in SAS Macros make SAS more “object-oriented”, like R Not a strong suit of text ©
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 2 Views.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Developing a Model-View-Controller Component for Joomla Part 3
A Guide to SQL, Eighth Edition
Chapter 2 Views.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Lab 2 and Merging Data (with SQL)
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
Database Systems: Design, Implementation, and Management Tenth Edition
IST 318 Database Administration
Presentation transcript:

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 discussed in more detail © Fall 2011 John Grego and the University of South Carolina

SPSSX SPSSX is a statistics package popular in the social sciences. It was originally more of a programming language, but now most users are familiar only with the menu-driven features I really like the way in which SPSSX creates output labels and formats © Fall 2011 John Grego and the University of South Carolina

SPSSX SAS interaction with SPSSX is fairly simple and straightforward—it imports SPSSX data sets SAS used to import only portable file formats © Fall 2011 John Grego and the University of South Carolina 3 3

SPSSX Starting with SAS 9.1.3, the Import Wizard can import SPSSX data sets of any type I.e., SPSSX .sav files no longer need to be saved as .por files prior to import The import preserves value coding. © Fall 2011 John Grego and the University of South Carolina 4 4

SPSSX Unfortunately, this coding/labeling is not preserved when the data set is saved as a permanent SAS data set It can either be reconstructed by hand, or the SPSSX data set can be imported each time it is needed, or the format catalog can also be saved © Fall 2011 John Grego and the University of South Carolina 5 5

SPSSX So what’s the point? It’s convenient to import data sets into SAS if (1) we need to take advantage of SAS’s additional functionality or (2) we don’t have a SPSSX license! © Fall 2011 John Grego and the University of South Carolina 6 6

SUDAAN SUDAAN is a package for analyzing complex surveys developed by RTI, but coordinated with SAS Researchers can use SUDAAN even though they do not have an intimate knowledge of survey sampling

SUDAAN Many complex survey databases available for public use include a set of pre-calculated weights, and often some applicable SUDAAN code With the data, the weights, and some knowledge of how the survey was constructed, researchers are ready to go 8

SUDAAN SAS can embed SUDAAN code in a regular SAS program. Syntax for SUDAAN and SAS-callable SUDAAN are so similar that you wouldn’t distinguish them at first glance SAS executes SUDAAN-style PROC steps, but with slight name changes to avoid confusion with existing SAS PROC steps 9

SUDAAN In the following examples, PROC REGRESS in SUDAAN is replaced by PROC SURVEYREG in SAS; NEST is replaced by STRATUM; WEIGHT is unchanged. CLUSTER is an important statement not represented here 10

SUDAAN SUDAAN: proc regress data=one filetype=sas design=wr; nest SSTRATID; weight byqwt; model by2xstd=byses; run; SAS: proc surveyreg data=one; stratum SSTRATID; weight byqwt; model by2xmstd=byses/adjrsq anova clparm deff; run; 11

IML The way in which SAS uses IML (Interactive Matrix Language) is quite different from the above two examples IML allows a form of object-oriented programming in SAS—when I first started grad school, it was one of the very few ways to do matrix math 12

IML IML uses some typical SAS features (semicolons, comments, etc.), but resembles other object-oriented languages such as R or Minitab as well. The basic format PROC IML; .. IML commands..QUIT; is a pattern we will see repeated with PROC SQL 13

SQL in SAS SQL stands for Structured Query Language, a language suited for database management and manipulation SQL can interact with all the standard database packages 14

SQL in SAS We will focus on SQL commands in SAS, though SAS has many other methods for interacting with databases (PROC IMPORT for example) PROC SQL is a SAS procedure that is based on SQL statements We are familiar with one SQL statement already: WHERE 15

SQL in SAS Some of the syntax is similar to the SAS data step, but there are key differences, e.g., CREATE TABLE (rather than DATA) creates a data set PROC SQL is built from extended clauses, rather than a set of discrete statements PROC SQL does not need a RUN; statement to execute. PROC SQL is typically ended with a QUIT; statement 16

SQL in SAS PROC SQL performs many of the same tasks as the DATA step, but PROC SQL has some advantages: Faster execution speed Joining tables with PROC SQL is considered by many to be more convenient than MERGE in a DATA step 17

SQL in SAS PROC SQL performs many of the same tasks as the DATA step, but PROC SQL has some advantages: SQL code can easily access external databases (e.g., Oracle, DB2, Access) In the examples we will study in class, advantages in processing speed will not be obvious 18

SQL in SAS An easy way to do this: PROC SQL; SELECT * FROM tablename; QUIT; One of the simplest tasks in PROC SQL is to select and print a data set that is already created. The * says to select all variables (columns) in the table 19

SQL in SAS By default this code prints the data set to the output window We can also select only a few variables by specifying the variable names (separated by commas) in the SELECT statement 20

SQL in SAS CREATE TABLE newtablename AS SELECT var1, var3, var4 FROM oldtablename; We may wish to create a new data set from part of a previous one. We use the CREATE TABLE..AS statement 21

SQL in SAS Some DATA step keywords work in PROC SQL as well (DROP, KEEP, RENAME) Other tasks using SQL keywords: DISTINCT: selects unique values of variables that have duplicate values ORDER BY: sorts a table by the values of one or more variables 22

SQL in SAS One way to create a data set from scratch is to use CREATE TABLE keywords without AS After the CREATE TABLE line, you specify the names and types of the variables 23

SQL in SAS CREATE TABLE tablename (var1 var1type var2 vaqr2type var 3 var3type); INSERT INTO ..; The raw data is entered into the table with an INSERT INTO statement As you can imagine, this isn’t practical for large data sets! 24

SQL in SAS Subsetting in PROC SQL is typically done with a WHERE statement Various calculations can be done (using AS) to create new variables. Calculations may be done on the whole table, or on groups of observations identified by some grouping variable (Use GROUP BY) 25

SQL in SAS If a calculation involves a variable not in the original data set, but which has been calculated, use keyword CALCULATED with that variable To “subset” based on “calculated” variables, do not use WHERE, but rather use the HAVING keyword 26

SQL in SAS CASE expression WHEN expvalue1 THEN resvalueA THEN resvalueB .. ELSE resvalueZ END AS resultcolumn The PROC SQL equivalent of an IF-THEN statement is a CASE statement 27

Joining Tables in PROC SQL Compared to merging data sets in the DATA step, joining tables in PROC SQL is executed faster In PROC SQL, the key columns (BY variables) do not need to be sorted first “Many to many” merges are possible using PROC SQL 28

Joining Tables in PROC SQL There are four main methods of joining tables using PROC SQL: the inner join, the left join, the right join, and the full join. Other interesting options are also available. 29

Joining Tables in PROC SQL FROM statement specifies source tables and “aliases” for those source tables, and also specifies the method of joining ON statement specifies “key columns” (like BY variables in a DATA step merge) and possibly logical operators SELECT statement contains the table aliases as well as the variables to be selected 30

Joining Tables in PROC SQL Inner join: result lists only observations for which the values of the “key columns” match Left join: result lists all observations in the “left” table (listed first in the FROM statement) and only the matching observations in the “right” table (Similar to use of IN= in a SAS merge) 31

Joining Tables in PROC SQL Right join: result lists all observations (listed second in the FROM statement) and only the matching observations in the “left” table. (Similar to use of IN= in a SAS merge) Full join: a combination of the left and right joins 32

Joining Tables in PROC SQL These “joins” have some undesirable effects—information on important variables can be lost The COALESCE function can recover information from the ON variables 33

Joining Tables in PROC SQL Creating logical indicators (much like IN=) may prove useful too There are many other methods of combining tables in SQL—you can rely on WHERE rather than ON, and there are additional types of “joins” 34

Editing Tables in PROC SQL INSERT INTO is a typical way to add new observations to a table VALUES statement specifies the values to be added (in parentheses, separated by spaces) 35

Editing Tables in PROC SQL SET col1=7, col2=‘charstring’, col3=44; Another way: Use a SET keyword with column names and newly assigned values 36

Editing Tables in PROC SQL To delete observations from a table, use DELETE FROM statement To change the values of one or more columns in a table, use UPDATE statement along with SET statement 37

Editing Tables in PROC SQL ALTER TABLE can be used to change column formats or to delete columns from a table (Typically done with the MODIFY and DROP keywords, respectively) DROP TABLE can also be used to delete an entire table 38

Other Topics in PROC SQL NOPRINT option suppresses printing to the OUTPUT window: PROC SQL NOPRINT; Note: When a CREATE statement is used, NOPRINT is the default 39