Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego,...

33
Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

Transcript of Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego,...

Page 1: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Chapter 5: Creating and

Managing Tables using PROC SQL

1

© Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

Page 2: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Outline

Creating Tables Inserting Rows into Tables Integrity Constraints Updating Tables Altering Columns

2

Page 3: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Creating an Empty Table

By Defining Columns By Copying Column Definitions from

another table

3

Page 4: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Stat Lab 2012

4

Client Name Client Email Client Degree USC Client Department Date Gratis Consultant

Page 5: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Creating an Empty Table

By Defining Columnsproc sql; create table work.lab2012(Client char(12), Email char(19), Degree char(8), USC char(1), Dept char(20), Date num format=mmddyy10. label=‘Date of First Contact’, Gratis char(1), Consultant char(10));

quit;

5

Page 6: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Creating an Empty Table

By Defining Columns- SQL supports other data types, but

PROC SQL simply converts them to either character or numeric

- Width of CHAR column can be varied- FORMAT and LABEL is supported- proc sql; describe table work.lab2011; quit; will print the table definition to the LOG

6

Page 7: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Creating an Empty Table

By copying the format of another table

For the Stat Lab, it would make more sense to copy columns, column labels and formats directly from a 2011 table.

7

Page 8: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Stat Lab 2011Name Email Degree USC Dept Date Gratis Consul

tant

Joe Bridges

[email protected]

External N SCDNR 12/03/2011

N John Grego

Gina White

[email protected]

Faculty Y Biology 12/05/2011

Y Wilma Sims

Matthew King

[email protected]

Doctoral

Y Library& Info. Sci.

12/15/2011

Y John Grego

Wenkuan Zhao

[email protected]

Faculty Y Medical School

12/16/2011

Y Wilma Sims

© Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina 8

Page 9: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Creating an Empty Table

By copying the format of another table

proc sql; create table work.lab2012like admin.lab2011;quit;proc sql; describe table work.lab2012;quit; Columns can be specified with DROP or

KEEP

9

Page 10: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Creating a Table from a query

We have been using the Create Table statement periodically throughout the class to save results from queries as SAS data sets

proc sql; create table sims2011 as select * from admin.lab2011 where consultant=‘Wilma Sims’;

Quit;

10

Page 11: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Copying a Table

Convenient for moving a table from a permanent location into WORK, or vice versa

proc sql;create table work.lab2011 as select * from admin.lab2011;quit;

11

Page 12: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Inserting Rows into a Table

Using SET Using VALUES Using a querySome of these methods will seem

terribly cumbersome, but can be useful to add a handful of new observations

12

Page 13: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Inserting Rows into a Table

Using SETproc sql; insert into work.lab2012set client=‘Jane Lipschitz’,

email=‘[email protected], degree=‘External’, USC=‘N’, Dept=‘SCDNR’, date=‘25Jan2012’d, Gratis=‘Y’, Consultant=‘John Grego’

set client=‘Gerry Bainbridge’, email=‘[email protected], degree=‘Faculty’, USC=‘Y’, Dept=‘School of Music’, date=‘31Jan2012’d, Gratis=‘Y’, Consultant=‘John Grego’;

select * from lab2012; quit; 13

Page 14: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Inserting Rows into a Table

Using VALUES- An entire set of columns can be entered

in order- A subset can be entered in a pre-

specified order

14

Page 15: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Inserting Rows into a Table

Using VALUESproc sql; insert into work.lab2012values(‘Scott Tyler’, ’[email protected]’, ’External’,

’N’, ’JERC’, ’17Jan2012’d, ‘N’, ’John Grego’);select * from work.lab2012; quit;

15

Page 16: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Inserting Rows into a Table

Using VALUESproc sql; insert into work.lab2012(Client, Consultant, Degree, USC, Date, Dept,

Gratis)values(‘Erin Merrick’, ’Wilma Sims’, ’Masters’, ’Y’,

’07Jan2012’d, ’Environment’, ‘N’);select * from work.lab2012; quit;

16

Page 17: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Inserting Rows into a Table

Using a queryproc sql; insert into work.lab2012select * from admin.lab2011 where

month(date)=12;select * from work.lab2012; quit;

17

Page 18: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Integrity Constraints

Restrict data values that can be assigned to columns

General Integrity Constraints– CHECK– NOT NULL– UNIQUE– PRIMARY KEY (NOT NULL and UNIQUE)

Page 19: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Integrity Constraints

proc sql; create table work.lab2012(Client char(12) primary key, Email char(19), Degree char(8), USC char(1) check(USC in (‘N’ ’Y’ ’y’ ’n’), Dept char(20), Date num format=mmddyy10. check(date between ’01Jan2012’d and ’31Dec2012’d), Gratis char(1), Consultant char(10) check(Consultant in (‘John Grego’,’Wilma Sims’));

quit;

19

Page 20: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Integrity Constraints Constraints can be viewed with the DESCRIBE

TABLE CONSTRAINTS statement:

proc sql;describe table constraints work.lab2012;quit;

20

Page 21: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Integrity Constraintsproc sql; insert into work.lab2012values(‘Erin

Merrick’,’[email protected]’ ,’Masters’, ’Y’, ‘Environment’,’07Jan2011’d,’Y’,’Wilma Sim’)

values(‘Scott Tyler’, ’[email protected]’, ’External’, ’No’, ’JERC’, ’17Jan2012’d, ‘N’, ’John Grego’)

values(‘Erin Merrick’, ‘Masters’,’Y’, ‘Environment’, ’01Feb2012’d, ‘N’, ’Wilma Sims’) ;

quit;

21

Page 22: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Integrity Constraints A separate CONSTRAINT statement can also be

used to handle integrity constraints Names are assigned to the constraints

proc sql; create table work.lab2012(Client char(12) primary key, Email char(19), Degree char(8), USC char(1) Dept char(20), Date num format=mmddyy10., Gratis char(1), Consultant char(10), constraint Check_USC check(USC in (‘N’ ’Y’ ’y’ ’n’)) );

quit;22

Page 23: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Integrity Constraints By default, SAS will not accept any additional

rows once it finds an error Options for UNDO_POLICY

– REQUIRED (the record is not added)– NONE (the record is skipped)– OPTIONAL (hybrid that inserts records when possible)

Table constraints can be viewed using DESCRIBE

23

Page 24: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables

Updating Existing Rows– With a common expression– With a conditional expression (similar to

IFELSE construction)

24

Page 25: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables

Use the SET expression to modify values

The WHERE clause can make the change conditional

25

Page 26: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables-Example

proc sql; create table lab2012pay likeadmin.lab2011pay;quit;

proc sql; update lab2012pay set rate=rate*1.05; quit;

proc sql; update lab2012pay set rate=rate*1.0013 where consultant='Director'; quit;

Page 27: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables

Use the SET expression with a CASE clause for conditional changes to a variable

The use of CASE will be very familiar to those who have used IFELSE in either R or Excel

CASE can be used elsewhere in a PROC SQL clause

27

Page 28: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables-Example

proc sql;update lab2012set rate=rate*casewhen consultant=‘Director’ then 1.05when consultant=‘Manager’ then 1.0375else 1.045end;

Page 29: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables

The update can be modified for greater efficiency

proc sql;update lab2012set rate=rate*case consultantwhen ‘Director’ then 1.05when ‘Manager’ then 1.0375else 1.045end;

29

Page 30: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables

DELETE FROM can be used to eliminate rows:

proc sql;delete from lab2012where date lt ’01Jan2012’d;quit;

30

Page 31: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables

ALTER TABLE can be used to update columns.

Options can be entered separately or simultaneously– ADD– MODIFY– DELETE

MODIFY cannot change a column’s name

31

Page 32: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables-Example

proc sql;alter table lab2012add College char(20) label=‘College or School’, time format=hour4.1

modify date format=weekdate31.quit;

Page 33: Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Updating Tables

DROP TABLE can be used to delete a table

proc sql;drop table admin.lab2011;quit;

33