If you want
to update some, but not all, of a column's values, then use a WHERE
expression in the UPDATE statement. You can use multiple UPDATE statements,
each with a different expression. However, each UPDATE statement can
have only one WHERE clause. The following UPDATE statements result
in different population increases for different countries in the NEWCOUNRTRIES
table.
libname sql 'SAS-library';
proc sql;
delete from sql.newcountries;
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 Values";
select name format=$20.,
capital format=$15.,
population format=comma15.0
from sql.newcountries;
Selectively Updating a Column
You can accomplish the
same result with a CASE expression:
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;
If the WHEN clause is true, then the corresponding
THEN clause returns a value that the SET clause then uses to complete
its expression. In this example, when Name starts with the letter
B,
the SET expression becomes
population=population*1.05
.
CAUTION:
Make sure
that you specify the ELSE clause.
If you omit the ELSE
clause, then each row that is not described in one of the WHEN clauses
receives a missing value for the column that you are updating. This
happens because the CASE expression supplies a missing value to the
SET clause, and the Population column is multiplied by a missing value,
which produces a missing value.