Creating and Updating Tables and Views

Creating Tables

  The Creating Table statement enables you to create tables whitout rows from column definitions or to create tables from a query result. You can also use CREATE TABLE to copy an existing table.

  1. Creating Tables from column definitions:

   You can specify a column's name,type,length,informat,format,and label

proc sql;
    create table sql.newstates(
        
           state char(2),
           date num
                  informat=date9.
                  format=date9.,
           population num
                 )  

proc sql;
       describe table sql.newstates;
Log: 
1 proc sql;
2 describe table sql.newstates;
NOTE: SQL table SQL.NEWSTATES was created like:
create table SQL.NEWSTATES( bufsize=4096 )
(
state char(2),
date num format=DATE9. informat=DATE9.,
population num
);

  2.Creating Tables from a Query Result:

    To create a PROC SQL table from a query result,use a CREATE TABLE statement with the AS keyword,and place it before the SELECT statement.

proc sql outobs=10;
title 'Densities of Countries';
create table sql.densities as
    select Name 'Country' format $15.,
             Population format=comma10.0,
             Area as SquareMiles,
            Population/Area format=6.2 as Density
        from sql.countries;    

  3. Creating Tables like an Existing Table

    To create an empty table that has the same column and attributes as an existing table or view,use the LIKE clause in the CREATE TABLE statement.

proc sql;
    create table sql.newcountries
        like sql.countries;

  4.Copying an Existing Table:

    A quick way to copy a table using PROC SQL is to use the CREATE TABLE statement with a query that returns an entire table.

Create table countries1 as 
    select * from sql.countries;

  5.Using Data Set Options:

Create table countries2 as
    select * from sql.countries(drop=UNDate);

 Inserting Rows into Tables:
  The INSERT statement first adds a new row to an existing table,and then inserts the values that you specify into the row.You specify values by using a SET clause or VALUES clause.You can also insert the rows resulting from a query.

  1.Inserting Rows with the SET Clause:

  With the SET clause, you assign values to columns by name, The columns can appear in any order in the SET clause ,and you must use a semicolon after the last SET clause only

libname sql 'SAS-library';
/* Create the newcountries table. */
proc sql;
    create table sql.newcountries
        like sql.countries;
/* Insert all of the rows from countries into newcountries based */
/* on a population of 130000000. */
proc sql;
    insert into sql.newcountries
        select * from sql.countries
            where population ge 130000000;
/* Insert 2 new rows in the newcountries table. */
/* Print the table. */
proc sql;
    insert into sql.newcountries
        set name='Bangladesh',
        capital='Dhaka',
        population=126391060
        set name='Japan',
        capital='Tokyo',
        population=126352003;

    select name format=$20.,
             capital format=$15.,
            population format=comma15.0
        from sql.newcountries;    

  2.Inserting Rows with the VALUES Clause:

  With the VALUES clause, you assign values to a column by position. It is necessary to specify a value or an appropriate missing value for all columns,and you must use a semicolon after the last Values clause only

libname sql 'SAS-library';
proc sql;
insert into sql.newcountries
    values ('Pakistan', 'Islamabad', 123060000, ., ' ', .)
    values ('Nigeria', 'Lagos', 99062000, ., ' ', .);

    select name format=$20.,
        capital format=$15.,
        population format=comma15.0
        from sql.newcountries;

   3.Inserting Rows with a Query

libname sql 'SAS-library';
proc sql;
    create table sql.newcountries
        like sql.countries;
proc sql;
    title "World's Largest Countries";
        insert into sql.newcountries
        select * from sql.countries
            where population ge 130000000;
    select name format=$20.,
            capital format=$15.,
            population format=comma15.0
    from sql.newcountries;


proc sql;
  create table sql.newcountries
    like sql.countries;
proc sql;
title "World's Largest Countries";
  insert into sql.newcountries (Name,Population)
    select Name,Population from sql.countries
    where population ge 130000000;
select name format=$20., population format=comma15.0
  from sql.newcountries;

Updating Data Values in a Table:

  You can use the UPDATE statement to modify data values in tables and in the tables views, the update statement unpdates data in existing columns;it does not create new columns.

  1. Updating all rows in a column with the same expression

/* code for all examples in updating section */
libname sql 'SAS-library';
proc sql;
create table sql.newcountries like sql.countries;
    insert into sql.newcountries
    select * from sql.countries
        where population ge 130000000;
proc sql;
    update sql.newcountries
        set population=population*1.05;
title "Updated Population Values";
    select name format=$20.,
            capital format=$15.,
            population format=comma15.0
       from sql.newcountries;

   2.Updating Rows in a Column with Different Expressions

    Use a WHERE expression in the UPDATE statement.

libname sql 'SAS-library';
proc sql;
create table sql.newcountries like sql.countries;
    insert into sql.newcountries
        select * from sql.countries
            where population ge 130000000;
proc sql;
    update sql.newcountries
        set population=population*1.05
            where name like 'B%';
    update sql.newcountries
        set population=population*1.07
            where name in ('China', 'Russia');
title "Selectively Updated Population ";
select name format=$20.,
           capital format=$15.,
        population format=comma15.0
from sql.newcountries;

  3.Updating Rows with case... when..else...end statement

    do not drop else statement

update sql.newcountries
    set population=population*
        case when name like 'B%' then 1.05
                when name in ('China', 'Russia') then 1.07
                else 1
        end;    

Deleting Rows:

  The Delete statement deletes one or more rows in a table.

proc sql;
create table sql.newcountries like sql.countries;
    insert into sql.newcountries
        select * from sql.countries
        where population ge 130000000;
proc sql;
delete
        from sql.newcountries
        where name like 'R%';

Altering Columns:

  The Altering table statement adds, modifies, and deletes columns in existing table.You can use the ALTER TABLE statement with table only.

  1. Adding a Column

    The ADD clause adds a new column to an existing table.You must specify the column name and data type. You can also specify a length(LENGTH=),format(FORMAT=),informat(INFORMAT=),and a label(LABEL=).

proc sql;
    create table sql.newcountries like sql.countries;
        insert into sql.newcountries
            select * from sql.countries
            where population ge 130000000;
proc sql;
    alter table sql.newcountries
        add density num label='Population Density' format=6.2;
title "Population Density Table";
    select name format=$20.,
    capital format=$15.,
    population format=comma15.0,
    density
from sql.newcountries;
quit;
proc sql;
create table sql.newcountries as
select *, population/area as density label='Population Density' format=6.2
     from sql.newcountries;

  2.Modifying a Column:
    You can use the MODIFY clause to change the width,informat,format, and label of a column.To change a column's name, use the RENAME=data set opetion ,You cannot change a column's data type by using the MODIFY clause.

proc sql;
create table sql.newcountries like sqlcountries;
create table sql.newcountries as
select * from sql.countries
where population ge 130000000;
proc sql;
title "World's Largest Countries";
alter table sql.newcountries
modify population format=comma15.;
select name, population from sql.newcountries;
proc sql;
title "World's Largest Countries";
alter table sql.newcountries
modify name char(60) format=$60.;
update sql.newcountries
set name='The United Nations member country is '||name;
select name from sql.newcountries;

 Creating and Using Integrity Constraints in a Table:

  Integrity constraints are rules that you specify to guarantee the accuracy, completeness,or consistency of data in tables.

 Creating and Using PROC SQL Views:

  1.Create Views:

proc sql;
title 'Current Population Information for Continents';
create view sql.newcontinents as
select continent,
    sum(population) as totpop format=comma15. label='Total Population',
    sum(area) as totarea format=comma15. label='Total Area'
from sql.countries
group by continent;
select * from sql.newcontinents;

  2. Describing a View:

 

proc sql;
    describe view sql.newcontinents;
quit;
原文地址:https://www.cnblogs.com/easy-wang/p/6473800.html