SQL Procedure

Example 1: Creating a Table and Inserting Data into It

Features:

CREATE TABLE statement : column-modifier

INSERT statement : VALUES clause

SELECT clause

FROM clause

Table name: PROCLIB.PAYLIST
This example creates the table PROCLIB.PAYLIST and inserts data into it.

Program

libname proclib 'SAS-library';
proc sql;
   create table proclib.paylist
       (IdNum char(4),
        Gender char(1),
        Jobcode char(3),
        Salary num,
        Birth num informat=date7.
                  format=date7.,
        Hired num informat=date7.
                  format=date7.);
insert into proclib.paylist
    values('1639','F','TA1',42260,'26JUN70'd,'28JAN91'd)
    values('1065','M','ME3',38090,'26JAN54'd,'07JAN92'd)
    values('1400','M','ME1',29769.'05NOV67'd,'16OCT90'd)
values('1561','M',null,36514,'30NOV63'd,'07OCT87'd)
    values('1221','F','FA3',.,'22SEP63'd,'04OCT94'd);
title 'PROCLIB.PAYLIST Table';
select *
   from proclib.paylist;
proc printto; run;

Program Description

Declare the PROCLIB library.The PROCLIB library is used in these examples to store created tables.
libname proclib 'SAS-library';
Create the PROCLIB.PAYLIST table.The CREATE TABLE statement creates PROCLIB.PAYLIST with six empty columns. Each column definition indicates whether the column is character or numeric. The number in parentheses specifies the width of the column. INFORMAT= and FORMAT= assign date informats and formats to the Birth and Hired columns.
proc sql;
   create table proclib.paylist
       (IdNum char(4),
        Gender char(1),
        Jobcode char(3),
        Salary num,
        Birth num informat=date7.
                  format=date7.,
        Hired num informat=date7.
                  format=date7.);
Insert values into the PROCLIB.PAYLIST table.The INSERT statement inserts data values into PROCLIB.PAYLIST according to the position in the VALUES clause. Therefore, in the first VALUES clause, 1639 is inserted into the first column, F into the second column, and so on. Dates in SAS are stored as integers with 0 equal to January 1, 1960. Suffixing the date with a d is one way to use the internal value for dates.
insert into proclib.paylist
    values('1639','F','TA1',42260,'26JUN70'd,'28JAN91'd)
    values('1065','M','ME3',38090,'26JAN54'd,'07JAN92'd)
    values('1400','M','ME1',29769.'05NOV67'd,'16OCT90'd)
Include missing values in the data.The value null represents a missing value for the character column Jobcode. The period represents a missing value for the numeric column Salary.
values('1561','M',null,36514,'30NOV63'd,'07OCT87'd)
    values('1221','F','FA3',.,'22SEP63'd,'04OCT94'd);
Specify the title.
title 'PROCLIB.PAYLIST Table';
Display the entire PROCLIB.PAYLIST table.The SELECT clause selects columns from PROCLIB.PAYLIST. The asterisk (*) selects all columns. The FROM clause specifies PROCLIB.PAYLIST as the table to select from.
select *
   from proclib.paylist;
proc printto; run;

HTML Output

Inserting Data into a Table
PROCLIB.PAYLIST Table