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=). The following ALTER TABLE statement adds the numeric
data column Density to the NEWCOUNTRIES table:
proc sql;
delete from sql.newcountries;
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;
The new column is added
to NEWCOUNTRIES, but it has no data values. The following UPDATE statement
changes the missing values for Density from missing to the appropriate
population densities for each country:
proc sql;
update sql.newcountries
set density=population/area;
title "Population Density Table";
select name format=$20.,
capital format=$15.,
population format=comma15.0,
density
from sql.newcountries;
Filling in the New Column's Values
You can accomplish the
same update by using an arithmetic expression to create the Population
Density column as you re-create the table:
proc sql;
create table sql.newcountries as
select *, population/area as density
label='Population Density'
format=6.2
from sql.newcountries;
See Calculating Values for another example of creating columns with arithmetic
expressions.