Updating Values in Existing Table Rows

Overview

To modify data values in some or all of the existing rows in a table, you use the UPDATE statement in PROC SQL. In the UPDATE statement, for each column whose rows you want to modify, you specify an expression that indicates how the values should be modified. For example, the following expression indicates that the values for the column Units should be multiplied by 4:
units=units*4
You can use the UPDATE statement in two main ways.
Method of Updating Table
Example
update all (or a subset of) rows in a column with the same expression
proc sql;
   update work.payrollmaster_new
      set salary=salary*1.05
      where jobcode like '__1';
update different rows in a column with different expressions
proc sql;
   update  work.payrollmaster_new 
      set salary=salary*
         case when substr(jobcode,3,1)='1'
                 then 1.05
              when substr(jobcode,3,1)='2'
                 then 1.10
              when substr(jobcode,3,1)='3'
                 then 1.15
              else 1.08
         end;
Note: The UPDATE statement does not insert new rows into the table. To insert rows, you must use the INSERT statement.
Note: You can also use the UPDATE statement to update existing values in a table that underlies a PROC SQL view. For details, see Creating and Managing Views Using PROC SQL.
We consider each of these methods for updating existing rows in a table.

Updating Rows By Using the Same Expression

To update all (or a subset of) rows in a column with the same expression, use an UPDATE statement that contains a SET clause and a possible WHERE clause.
General form, basic UPDATE statement for updating table rows:
UPDATE table-name
SET column-1=expression<, ... column-n=expression>>
<WHERE expression>;
Here is an explanation of the syntax:
table-name
specifies the name of the table in which values are updated.
SET
specifies one or more pairs of column names to be updated, and expressions that indicate how each column is to be updated.
WHERE
is used to specify an expression that subsets the rows to be updated.
CAUTION:
If you want to update only a subset of rows in the table, you must specify a WHERE clause or all rows of the table that are updated.

Example

Suppose a company is considering giving all level-1 employees a 5% raise. Employee salaries are stored in the table Sasuser.Payrollmaster. You do not want to update the original table, so you create a temporary copy of Sasuser.Payrollmaster, called Work.Payrollmaster_New. The following PROC SQL step creates Work.Payrollmaster_New based on a query result and generates an output report of the new table:
proc sql;
   create table work.payrollmaster_new as
      select *
         from sasuser.payrollmaster;
   select *
      from work.payrollmaster_new;
The first 10 rows of Work.Payrollmaster_New, the table in which you update salaries, are shown below.
Work.Payrollmaster_New
Next, you write a PROC SQL step that updates the specified rows. The UPDATE statement contains both of the following:
  • a SET clause that specifies the expression to be used in updating Salary
  • a WHERE clause that specifies a subset of rows (level-1 employees) to be updated.
    proc sql;
       update work.payrollmaster_new
          set salary=salary*1.05
          where jobcode like '__1';
Finally, you can use a SELECT statement to display the updated table as a report. The first 10 rows of Work.Payrollmaster_New, with updates, are shown below.
Work.Payrollmaster_New
The third row lists data for a level-1 employee, and that person's salary has been updated.
If you wanted to increase all of the salaries, you would simply remove the WHERE clause from the UPDATE statement:
proc sql;
   update work.payrollmaster_new
      set salary=salary*1.05;

Updating Rows By Using Different Expressions

Sometimes you want to use different expressions to modify values for different subsets of rows within a column.
For example, instead of only raising the salary of level-1 employees by 5%, you might also want to raise the salaries of level-2 employees by 10%, and so on, using a different percentage increase for each group of employees.
There are two possible ways to use different expressions to update different subsets of rows.
Method of Updating Table
Example
use multiple UPDATE statements subset of rows
A single UPDATE statement can contain only a single WHERE clause, so multiple UPDATE statements are needed to specify expressions for multiple subsets of rows.
proc sql;
   update work.payrollmaster_new
      set salary=salary*1.05
         where substr(jobcode,3,1)='1';
   update work.payrollmaster_new
      set salary=salary*1.10
         where substr(jobcode,3,1)='2';
   update work.payrollmaster_new
      set salary=salary*1.15
         where substr(jobcode,3,1)='3';
use a single UPDATE statement that contains a CASE expression
proc sql;
   update work.payrollmaster_new 
      set salary=salary*
         case 
              when substr(jobcode,3,1)='1'
                 then 1.05
              when substr(jobcode,3,1)='2'
                 then 1.10
              when substr(jobcode,3,1)='3'
                 then 1.15
              else 1.08
         end;
The first method, which requires the use of multiple UPDATE statements, is cumbersome because the SET statement and expression must be repeated in each UPDATE statement. In this example, the first method is inefficient because the table Work.Payrollmaster_New must be read three times.
The second method, which uses conditional processing (the CASE expression), is recommended. We now consider the second method.
To update different subsets of rows in a table in different ways, you can incorporate conditional processing by using the CASE expression in the SET clause of an UPDATE statement. The CASE expression selects result values that satisfy specified conditions.
General form, CASE expression:
CASE <case-operand>
WHEN when-condition THEN result-expression
<...WHEN when-condition THEN result-expression>
<ELSE result-expression>
END;
Here is an explanation of the syntax:
CASE
performs conditional processing.
case-operand
is an optional expression that resolves to a table column whose values are compared to all the when-conditions.
WHEN
specifies a when-condition, a shortened expression that assumes case-operand as one of its operands, and that resolves to true or false.
THEN
specifies a result-expression, an expression that resolves to a value.
ELSE
specifies a result-expression, which provides an alternate action if none of the when-conditions is executed.
END
indicates the end of the CASE expression.
CAUTION:
Although the ELSE clause is optional, its use is strongly recommended. If you omit the ELSE clause, each row that is not described in one of the WHEN clauses receives a missing value for the column that you are updating.
Note: You can also use the CASE expression in the INSERT and SELECT statements.

Example

In the following UPDATE statement, the CASE expression contains three WHEN-THEN clauses that specify three different subsets of rows in the table Work.Insure_New:
  • homeowners that are insured by Acme
  • homeowners that are insured by Reliable
  • homeowners that are insured by Homelife.
update work.insure_new
   set pctinsured=pctinsured*
      case 
         when company='ACME' 
              then 1.10
         when company='RELIABLE' 
              then 1.15
         when company='HOMELIFE' 
              then 1.25 
         else 1 
      end;
PROC SQL updates each specified subset of rows differently, according to the corresponding WHEN-THEN (or ELSE) clause.

How PROC SQL Updates Rows Based on a CASE Expression

When you specify a CASE expression, PROC SQL updates each row as follows:
  1. In the CASE expression, PROC SQL finds the WHEN-THEN clause that contains a condition that the row matches.
  2. The CASE expression then returns the result from the matching WHEN-THEN clause to the SET clause. The returned value completes the expression in the SET clause.
  3. The SET clause uses the completed expression to update the value of the specified column in the current row.
The use of the CASE expression is efficient because of how PROC SQL processes the WHEN-THEN clauses. The WHEN-THEN clauses in the CASE expression are evaluated sequentially. When a matching case is found, the THEN expression is evaluated and set, and the remaining WHEN cases are not considered.

How the Case Operand Works

In the next few sections, you learn about the use of the CASE expression in the UPDATE statement, without and with the optional case operand:
CASE <case-operand>

Updating Rows By Using the CASE Expression without a Case Operand

Here is an example of an UPDATE statement that uses the CASE expression for conditional processing. This example shows the form of the CASE expression that does not include the optional case operand.

Example

Suppose a company is considering giving raises to all of its employees, with a different percentage for each employee level:
  • level-1 employees get a 5% raise
  • level-2 employees get a 10% raise
  • level-3 employees get a 15% raise.
First, you create the temporary table Work.Payrollmaster3, which is a copy of Sasuser.Payrollmaster, the table containing the employee salary data. The first 10 rows of Work.Payrollmaster3 are shown below.
Work.Payrollmaster3
Next, you create a PROC SQL step that updates rows by using an UPDATE statement that contains a SET clause and a CASE expression:
proc sql;
   update work.payrollmaster3
      set salary=salary*
         case 
            when substr(jobcode,3,1)='1'
                 then 1.05
            when substr(jobcode,3,1)='2'
                 then 1.10
            when substr(jobcode,3,1)='3'
                 then 1.15
            else 1.08
         end;
In this example, the CASE expression contains three WHEN clauses, one for each subset of rows (level-1, level-2, and level-3 employees), followed by an ELSE clause to handle any rows that do not meet the expected conditions.
The first 10 rows of Work.Payrollmaster3, after the rows have been updated, are shown below.
Work.Payrollmaster3
By comparing the values of Salary in the original and updated versions of Work.Payrollmaster3 (as shown above), you can see how the values changed according to the job level indicated in the JobCode.

Updating Rows By Using the CASE Expression with a Case Operand

If the expression in the SET clause uses an equals (=) comparison operator, you might use the optional case operand in the CASE expression. Consider PROC SQL step that was shown in the preceding example, and see how the CASE expression in the UPDATE statement can be rewritten by using the alternate syntax.

Example

In the following PROC SQL step, which was shown earlier, the CASE expression contains three WHEN-THEN clauses. These clauses contain similar expressions, each of which specifies the same SUBSTR function:
proc sql;
   update work.payrollmaster_new2 
      set salary=salary*
         case 
            when substr(jobcode,3,1)='1'
                   then 1.05
            when substr(jobcode,3,1)='2'
                   then 1.10
            when substr(jobcode,3,1)='3'
                   then 1.15
            else 1.08
         end;
Because the expression in this SET clause uses an equals (=) operator, you can restructure the CASE expression for more efficient processing. In the alternate syntax, the repeated SUBSTR function is removed from each WHEN-THEN clause and is placed after the keyword CASE, as an operand:
proc sql;
   update work.payrollmaster_new2 
      set salary=salary*
         case substr(jobcode,3,1)
            when '1'
                 then 1.05
            when '2'
                 then 1.10
            when '3'
                 then 1.15
            else 1.08
         end;
Using the alternate syntax, the SUBSTR function is evaluated only once, so this PROC SQL step is more efficient than the original version.
Note: You might use the case operand syntax only if the SET clause expression uses the equals (=) comparison operator.

Using the CASE Expression in the SELECT Statement

You can use the CASE expression in three different PROC SQL statements: UPDATE, INSERT, and SELECT. In the SELECT statement, you can use the CASE expression within a new column definition to specify different values for different subsets of rows.

Example

Suppose you want to generate an output report that displays employee names, job codes, and job levels. Your PROC SQL query selects LastName and FirstName from Sasuser.Staffmaster, and JobCode from Sasuser.Payrollmaster. The SELECT statement must define JobLevel as a new column, because it does not exist as a separate column in either table.
You want to assign the values of JobLevel, based on the number at the end of each jobcode. (The number at the end of each JobCode value is expected to be 1, 2, or 3.) To create JobLevel, you can use the case operand form of the CASE expression to specify the three possible conditions (plus an ELSE condition, just in case).
The PROC SQL query is shown below:
proc sql outobs=10;
   select lastname, firstname, jobcode, 
          case substr(jobcode,3,1)
             when '1'
                  then 'junior'
             when '2'
                  then 'intermediate'
             when '3'
                  then 'senior'
             else 'none'
          end as JobLevel
      from sasuser.payrollmaster,
           sasuser.staffmaster
      where staffmaster.empid=
            payrollmaster.empid;
PROC SQL Query Output
The SELECT clause uses the CASE expression to assign a value of junior, intermediate, senior, or none to each row in the new JobLevel column.
..................Content has been hidden....................

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