Chapter 4
Creating and Updating Tables and
Views
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Creating Tables from Column Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Creating Tables from a Query Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Creating Tables like an Existing Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Copying an Existing Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Using Data Set Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Inserting Rows into Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Inserting Rows with the SET Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Inserting Rows with the VALUES Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Inserting Rows with a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Updating Data Values in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Updating All Rows in a Column with the Same Expression . . . . . . . . . . . . . . . . . 120
Updating Rows in a Column with Different Expressions . . . . . . . . . . . . . . . . . . . 121
Handling Update Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Deleting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Altering Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Adding a Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Modifying a Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Deleting a Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Creating an Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Using PROC SQL to Create Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Tips for Creating Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Deleting Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Deleting a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Using SQL Procedure Tables in SAS Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Creating and Using Integrity Constraints in a Table . . . . . . . . . . . . . . . . . . . . . . . 129
Creating and Using PROC SQL Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Overview of Creating and Using PROC SQL Views . . . . . . . . . . . . . . . . . . . . . . . 131
Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Describing a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Updating a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Embedding a LIBNAME in a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Deleting a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Specifying In-Line Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
111

Get SAS 9.4 SQL Procedure User's Guide, Fourth Edition, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.