Updating PROC SQL Views

Overview

You can update the data underlying a PROC SQL view using the INSERT, DELETE, and UPDATE statements under the following conditions:
  • You can update only a single table through a view. The table cannot be joined or linked to another table, nor can it contain a subquery.
  • You can update a column using the column's alias, but you cannot update a derived column (a column that is produced by an expression).
  • You can update a view that contains a WHERE clause. The WHERE clause can be specified in the UPDATE clause or in the view. You cannot update a view that contains any other clause such as an ORDER BY or a HAVING clause.
  • You cannot update a summary view (a view that contains a GROUP BY clause).
Updating a view does not change the stored instructions for the view. Only the data in the underlying table(s) is updated.

Example

The following PROC SQL step creates the view Sasuser.Raisev, which includes the columns Salary and MonthlySalary. A subsequent query that references the view shows the columns.
proc sql;
   create view sasuser.raisev as
      select empid, jobcode, 
             salary format=dollar12., 
             salary/12 as MonthlySalary
             format=dollar12.
         from payrollmaster;

proc sql;
   select *
      from sasuser.raisev
      where jobcode in ('PT2','PT3'),
Sasuser.Raisev View
Suppose you want to update the view to show a salary increase for employees whose job code is PT3. You can use an UPDATE statement to change the column Salary and a WHERE clause in the UPDATE clause to identify the rows where the value of JobCode equals PT3. Though MonthlySalary is a derived column and cannot be changed using an UPDATE statement, it is updated because it is derived from Salary.
When the PROC SQL step is submitted, a note appears in the SAS log that indicates how many rows were updated:
proc sql;
   update sasuser.raisev
      set salary=salary * 1.20 
      where jobcode='PT3';
Table 7.3 SAS Log
116   proc sql;
117      update sasuser.raisev
118         set salary=salary * 1.20
119         where jobcode='PT3';
NOTE: 2 rows were updated in SASUSER.RAISEV.
Note: Remember that the rows were updated in the table that underlies the view Sasuser.Raisev.
When you resubmit the query, the updated values for Salary and MonthlySalary appear in the rows where JobCode equals PT3:
proc sql;
   select *
      from sasuser.raisev
      where jobcode in ('PT2','PT3'),
Sasuser.Raisev View with Updated Values
..................Content has been hidden....................

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