Creating an Empty Table By Defining Columns

Overview

Sometimes you want to create a new table that is unlike any existing table. In this case, you need to define all of the table’s columns and attributes. To accomplish this, use the CREATE TABLE statement that includes column specifications for the columns that you want to include. This statement creates a table without rows (an empty table).
Note: In addition, integrity constraints can be specified in the CREATE TABLE statement. Integrity constraints are discussed later in this chapter.
General form, basic CREATE TABLE statement with column specifications:
CREATE TABLE table-name
(column-specification-1<,
...column-specification-n>);
Here is an explanation of the syntax:
table-name
specifies the name of the table to be created.
column-specification
specifies a column to be included in the table, and consists of
column-definition <column-constraint-1<, ...column-constraint-n>>
<MESSAGE='message-string'<MSGTYPE=message-type>>
Here is an explanation of the syntax:
column-definition consists of the following:
column-name data-type<(column-width)><column-modifier-1<... column-modifier-n>>
column-name
specifies the name of the column. The column name is stored in the table in the same case that is used in column-name.
data-type
is enclosed in parentheses and specifies one of the following: CHARACTER (or CHAR) | VARCHAR | INTEGER (or INT) | SMALLINT | DECIMAL (or DEC) | NUMERIC (or NUM) | FLOAT | REAL | DOUBLE PRECISION | DATE.
column-width
which is enclosed in parentheses, is an integer that specifies the width of the column. (PROC SQL processes this value only for the CHARACTER and VARCHAR data types.)
column-modifier
is one of the following: INFORMAT= | FORMAT= | LABEL= . More than one column-modifier might be specified.
column-constraint
specifies an integrity constraint.
MESSAGE= and MSGTYPE=
specify an error message that is related to an integrity constraint. (Integrity constraints, the column-constraint, MESSAGE=, and MSGTYPE= are not elaborated here, but are discussed in detail later in this chapter.)
Note: The entire set of column-specifications must be enclosed in parentheses. Multiple column-specifications must be separated by commas. Elements within a column-specification must be separated by spaces.

Example

Suppose you want to create the temporary table Work.Discount, which contains data about discounts that are offered by an airline. There is no existing table that contains the four columns (and column attributes) that you would like to include: Destination, BeginDate, EndDate, and Discount. You use the following PROC SQL step to create the table, based on column definitions that you specify:
 proc sql;
    create table work.discount 
           (Destination char(3),
           BeginDate num Format=date9.,
           EndDate num format=date9.,
           Discount num);
The SAS log confirms that the table has been created.
Table 5.3 SAS Log
NOTE: Table WORK.DISCOUNT created, with 0 rows and 4 columns.
Tip
In this example and all other examples in this chapter, you are instructed to save your data to a temporary table (in the library Work) that is deleted at the end of the SAS session. To save the table permanently in a different library, use the appropriate libref instead of the libref Work in the CREATE TABLE clause.
In the next few sections, you learn more about specifying data types and column modifiers in a column specification.
Note: You learn to insert rows of data in a table later in this chapter.

Specifying Data Types

When you create a table by defining columns, you must specify a data type for each column, following the column name:
column-name data-type <(column-width)> <column-modifier-1<...column-modifier-n>>
For example, the following PROC SQL step (shown also in the previous section) defines four columns: one-character column (Destination) and three numeric columns (BeginDate, EndDate, and Discount):
proc sql;
   create table work.discount 
          (Destination char(3),
          BeginDate num format=date9.,
          EndDate num format=date9.,
          Discount num);
SAS tables use two data types: numeric and character. However, PROC SQL supports additional data types (many, but not all, of the data types that SQL-based databases support). Therefore, in the CREATE TABLE statement, you can specify any of 10 different data types. When the table is created, PROC SQL converts the supported data types that are not SAS data types to either numeric or character format.
Table 5.4 Character Data Types Supported by PROC SQL
Specified Data Type
SAS Data Type
CHARACTER (or CHAR)
CHARACTER
VARCHAR
CHARACTER
Table 5.5 Numeric Data Types Supported by PROC SQL
Specified Data Type
Description
SAS Data Type
NUMERIC (or NUM)
floating-point
NUMERIC
DECIMAL (or DEC)
floating-point
NUMERIC
FLOAT
floating-point
NUMERIC
REAL
floating-point
NUMERIC
DOUBLE PRECISION
floating-point
NUMERIC
INTEGER (or INT)
integer
NUMERIC
SMALLINT
integer
NUMERIC
DATE
date
NUMERIC with a DATE.7 informat and format
The following PROC SQL step is very similar to the previous example. The only difference is that this step specifies three supported data types other than CHAR and NUM: VARCHAR, DATE, and FLOAT.
proc sql;
   create table work.discount2 
          (Destination varchar(3),
          BeginDate date,
          EndDate date,
          Discount float);
PROC SQL converts these data types to either character or numeric, as indicated in the charts above. Therefore, the table Work.Discount2 (created by this PROC SQL step) and Work.Discount (created by the previous PROC SQL step) contains identical columns.
By supporting data types other than SAS data types, PROC SQL can save you time. In many cases, you can copy native code from an implementation of SQL that is external to SAS without having to modify the data types.

Specifying Column Widths

In SAS, the default column width for both character and numeric columns is 8 bytes. However, character and numeric data values are stored differently:
  • Character data is stored one character per byte.
  • Numeric data is stored as floating point numbers in real binary representation, which allows for 16- or 17-digit precision within 8 bytes.
PROC SQL enables you to specify a column width for character columns but not for numeric columns.
Note: PROC SQL allows the WIDTH and NDEC (decimal places) arguments to be included in the column specification for the DECIMAL, NUMERIC, and FLOAT data types. However, PROC SQL ignores this specification and uses the SAS defaults.
In a column specification, the column width follows the data type and is specified as an integer enclosed in parentheses:
column-name data-type <(column-width)> <column-modifier-1<...column-modifier-n>>
In the following PROC SQL step, the first column specification indicates a column width of 3 for the character column Destination:
proc sql;
   create table work.discount 
          (Destination char(3),
          BeginDate num format=date9.,
          EndDate num format=date9.,
          Discount num);
Because the last three columns are numeric, no width is specified and these columns have the default column width of 8 bytes.

Specifying Column Modifiers

In the CREATE TABLE statement, a column specification might include one or more of the following SAS column modifiers: INFORMAT=, FORMAT=, and LABEL=. Column modifiers, if used, are specified at the end of the column specification.
column-name data-type <(column-width)> <...column-modifier-1 <...column-modifier-n>>
Note: A fourth SAS column modifier, LENGTH=, is not allowed in a CREATE TABLE clause. It can be used in a SELECT clause.

Example

The following PROC SQL step creates the table Work.Departments by specifying 4 columns. The column modifiers LABEL= and FORMAT= are used to specify additional column attributes.
proc sql;
   create table work.departments 
          (Dept varchar(20) label='Department',
           Code integer label='Dept Code',
           Manager varchar(20), 
           AuditDate num format=date9.);
The SAS log verifies that the table was created.
Table 5.6 SAS Log
NOTE: Table WORK.DEPARTMENTS created, with 0 rows and 4 columns.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.192.59