Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego,...
-
Upload
gregory-shepley -
Category
Documents
-
view
216 -
download
1
Transcript of Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego,...
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
Creating an Empty Table
By Defining Columns By Copying Column Definitions from
another table
3
Stat Lab 2012
4
Client Name Client Email Client Degree USC Client Department Date Gratis Consultant
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
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
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
Stat Lab 2011Name Email Degree USC Dept Date Gratis Consul
tant
Joe Bridges
External N SCDNR 12/03/2011
N John Grego
Gina White
Faculty Y Biology 12/05/2011
Y Wilma Sims
Matthew King
Doctoral
Y Library& Info. Sci.
12/15/2011
Y John Grego
Wenkuan Zhao
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
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
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
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
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
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
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
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
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
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
Integrity Constraints
Restrict data values that can be assigned to columns
General Integrity Constraints– CHECK– NOT NULL– UNIQUE– PRIMARY KEY (NOT NULL and UNIQUE)
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
Integrity Constraints Constraints can be viewed with the DESCRIBE
TABLE CONSTRAINTS statement:
proc sql;describe table constraints work.lab2012;quit;
20
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
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
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
Updating Tables
Updating Existing Rows– With a common expression– With a conditional expression (similar to
IFELSE construction)
24
Updating Tables
Use the SET expression to modify values
The WHERE clause can make the change conditional
25
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;
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
Updating Tables-Example
proc sql;update lab2012set rate=rate*casewhen consultant=‘Director’ then 1.05when consultant=‘Manager’ then 1.0375else 1.045end;
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
Updating Tables
DELETE FROM can be used to eliminate rows:
proc sql;delete from lab2012where date lt ’01Jan2012’d;quit;
30
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
Updating Tables-Example
proc sql;alter table lab2012add College char(20) label=‘College or School’, time format=hour4.1
modify date format=weekdate31.quit;
Updating Tables
DROP TABLE can be used to delete a table
proc sql;drop table admin.lab2011;quit;
33