Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina."— Presentation transcript:

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

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

3 Creating an Empty Table By Defining Columns By Defining Columns By Copying Column Definitions from another table By Copying Column Definitions from another table 3

4 Stat Lab 2012 4 Client Name Client Name Client Email Client Email Client Degree Client Degree USC USC Client Department Client Department Date Date Gratis Gratis Consultant Consultant

5 Creating an Empty Table By Defining Columns By Defining Columns proc 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

6 Creating an Empty Table By Defining Columns 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 proc sql; describe table work.lab2011; quit; will print the table definition to the LOG 6

7 Creating an Empty Table By copying the format of another 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

8 Stat Lab 2011 NameEmailDegreeUSCDeptDateGratisConsult ant Joe Bridges BridgesJ @dnr.sc. gov ExternalNSCDNR12/03/20 11 NJohn Grego Gina White whiteg@ biol.sc.ed u FacultyYBiology12/05/20 11 YWilma Sims Matthew King kingm3@ email.sc. edu DoctoralYLibrary& Info. Sci. 12/15/20 11 YJohn Grego Wenkuan Zhao zhaow@ email.sc. edu FacultyYMedical School 12/16/20 11 YWilma Sims © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina8

9 Creating an Empty Table By copying the format of another table By copying the format of another table proc sql; create table work.lab2012 like admin.lab2011; quit; proc sql; describe table work.lab2012; quit; Columns can be specified with DROP or KEEP Columns can be specified with DROP or KEEP 9

10 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 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

11 Copying a Table Convenient for moving a table from a permanent location into WORK, or vice versa 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

12 Inserting Rows into a Table Using SET Using SET Using VALUES Using VALUES Using a query Using a query Some of these methods will seem terribly cumbersome, but can be useful to add a handful of new observations 12

13 Inserting Rows into a Table Using SET Using SET proc sql; insert into work.lab2012 set client=Jane Lipschitz, email=lipschitzj@dnr.sc.gov, degree=External, USC=N, Dept=SCDNR, date=25Jan2012d, Gratis=Y, Consultant=John Grego set client=Gerry Bainbridge, email=bainbrid@email.sc.edu, degree=Faculty, USC=Y, Dept=School of Music, date=31Jan2012d, Gratis=Y, Consultant=John Grego; select * from lab2012; quit; 13

14 Inserting Rows into a Table Using VALUES Using VALUES -An entire set of columns can be entered in order -A subset can be entered in a pre-specified order 14

15 Inserting Rows into a Table Using VALUES Using VALUES proc sql; insert into work.lab2012 values(Scott Tyler, tylers@jonesctr.org, External, N, JERC, 17Jan2012d, N, John Grego); select * from work.lab2012; quit; 15

16 Inserting Rows into a Table Using VALUES Using VALUES proc sql; insert into work.lab2012 (Client, Consultant, Degree, USC, Date, Dept, Gratis) values(Erin Merrick, Wilma Sims, Masters, Y, 07Jan2012d, Environment, N); select * from work.lab2012; quit; 16

17 Inserting Rows into a Table Using a query Using a query proc sql; insert into work.lab2012 select * from admin.lab2011 where month(date)=12; select * from work.lab2012; quit; 17

18 Integrity Constraints Restrict data values that can be assigned to columns General Integrity Constraints – – CHECK – – NOT NULL – – UNIQUE – – PRIMARY KEY (NOT NULL and UNIQUE)

19 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 01Jan2012d and 31Dec2012d), Gratis char(1), Consultant char(10) check(Consultant in (John Grego,Wilma Sims)); quit; 19

20 Integrity Constraints Constraints can be viewed with the DESCRIBE TABLE CONSTRAINTS statement: Constraints can be viewed with the DESCRIBE TABLE CONSTRAINTS statement: proc sql; describe table constraints work.lab2012; quit; 20

21 Integrity Constraints proc sql; insert into work.lab2012 values(Erin Merrick,merrickj@email.sc.edu,Masters, Y, Environment,07Jan2011d,Y,Wilma Sim) values(Scott Tyler, tylers@jonesctr.org, External, No, JERC, 17Jan2012d, N, John Grego) values(Erin Merrick, Masters,Y, Environment, 01Feb2012d, N, Wilma Sims) ; quit; 21

22 Integrity Constraints A separate CONSTRAINT statement can also be used to handle integrity constraints A separate CONSTRAINT statement can also be used to handle integrity constraints Names are assigned to the 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

23 Integrity Constraints By default, SAS will not accept any additional rows once it finds an error By default, SAS will not accept any additional rows once it finds an error Options for UNDO_POLICY 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 Table constraints can be viewed using DESCRIBE 23

24 Updating Tables Updating Existing Rows Updating Existing Rows –With a common expression –With a conditional expression (similar to IFELSE construction) 24

25 Updating Tables Use the SET expression to modify values Use the SET expression to modify values The WHERE clause can make the change conditional The WHERE clause can make the change conditional 25

26 Updating Tables-Example proc sql; create table lab2012pay like admin.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;

27 Updating Tables Use the SET expression with a CASE clause for conditional changes to a variable 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 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 CASE can be used elsewhere in a PROC SQL clause 27

28 Updating Tables-Example proc sql; update lab2012 set rate=rate*case when consultant=Director then 1.05 when consultant=Manager then 1.0375 else 1.045 end;

29 Updating Tables The update can be modified for greater efficiency The update can be modified for greater efficiency proc sql; update lab2012 set rate=rate*case consultant when Director then 1.05 when Manager then 1.0375 else 1.045 end; 29

30 Updating Tables DELETE FROM can be used to eliminate rows: DELETE FROM can be used to eliminate rows: proc sql; delete from lab2012 where date lt 01Jan2012d; quit; 30

31 Updating Tables ALTER TABLE can be used to update columns. ALTER TABLE can be used to update columns. Options can be entered separately or simultaneously Options can be entered separately or simultaneously –ADD –MODIFY –DELETE MODIFY cannot change a columns name MODIFY cannot change a columns name 31

32 Updating Tables-Example proc sql; alter table lab2012 add College char(20) label= College or School, time format=hour4.1 modify date format=weekdate31. quit;

33 Updating Tables DROP TABLE can be used to delete a table DROP TABLE can be used to delete a table proc sql; drop table admin.lab2011; quit; 33


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

Similar presentations


Ads by Google