Integrity Constraints Integrity constraints are rules enforced when data is added to a table to guarantee data validity. To preserve the consistency and correctness of your data, specify integrity constraints for the SAS data file. SAS uses integrity constraints to validate data values when you insert or update columns for which you defined integrity constraints.
Integrity Constraints Integrity constraints were added to Base SAS software in SAS 8 follow ANSI standards Cannot be defined for views Can be specified when a table is created Can be added to a table that already contains data Are commonly found in large database management systems (DBMS) with frequently updated tables.
This could be a costly typo! proc sql; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent. ) ; describe table discounts; quit; proc contents data=discounts;run; insert into Discounts values (240500200009,'01Mar2007'd, '31Mar2007'd,.45) values (220200200036,'01Mar2007'd, '31Mar2007'd,.54) values (220200200038,'01Mar2007'd, '31Mar2007'd,.25) proc print data=discounts noobs;run; This could be a costly typo!
Creating Integrity Constraints with PROC SQL General form of PROC SQL using integrity constraints: Integrity constraints are assigned as part of the table definition. PROC SQL; CREATE TABLE table (column-specification,… <constraint-specification,…>);
Creating Integrity Constraints with PROC SQL create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent., constraint ok_discount check (Discount le .5)) ; describe table discounts; quit; proc contents data=discounts;run;
Integrity Constraint Violations Example: Insert three rows of data. proc sql; insert into Discounts values (240500200009,'01Mar2007'd, '31Mar2007'd,.45) values (220200200036,'01Mar2007'd, '31Mar2007'd,.54) values (220200200038,'01Mar2007'd, '31Mar2007'd,.25) ; quit;
What should we do if there is a constraint failure? UNDO_POLICY Option Changing the UNDO_POLICY option in PROC SQL gives you control over how UNDO is performed when integrity constrains are violated.
Controlling the UNDO_POLICY Option UNDO_POLICY=REQUIRED (default) undoes all inserts or updates up to the point of the error. Sometimes the UNDO operation cannot be accomplished reliably. UNDO_POLICY=NONE rejects only rows that violate constraints. Rows that do not violate constraints are inserted. UNDO_POLICY=OPTIONAL operates in a manner similar to REQUIRED when the UNDO operation can be accomplished reliably otherwise, operates similar to NONE.
proc sql undo_policy=none; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent., constraint ok_discount check (Discount le .5)) ; insert into Discounts values (240500200009,'01Mar2007'd, '31Mar2007'd,.45) values (220200200036,'01Mar2007'd, '31Mar2007'd,.54) values (220200200038,'01Mar2007'd, '31Mar2007'd,.25) quit;
proc sql undo_policy=optional; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=percent., constraint ok_discount check (Discount le .5)) ; insert into Discounts values (240500200009,'01Mar2007'd, '31Mar2007'd,.45) values (220200200036,'01Mar2007'd, '31Mar2007'd,.54) values (220200200038,'01Mar2007'd, '31Mar2007'd,.25) quit; proc print data=discounts noobs;run;
Troubleshooting Integrity Constraint Violations When an integrity constraint is violated, the SAS log identifies which VALUES clause contained the error, and names the violated integrity constraint. To correct the problem, you need more information about the violated integrity constraint.
Troubleshooting Integrity Constraint Violations The DESCRIBE statement can display column attributes of a table as well as information about indexes and integrity constraints. DESCRIBE statements produce output in the SAS log. PROC SQL; DESCRIBE TABLE table-name<, …table-name>; DESCRIBE VIEW proc-sql-view <, …proc-sql-view>; DESCRIBE TABLE CONSTRAINTS table-name <, …table-name>;
Troubleshooting Integrity Constraint Violations Statement Results Produced in the SAS Log DESCRIBE VIEW SQL code that would create a view identical to the view being described DESCRIBE TABLE SQL code that would create a table identical to the table being described (including indexes) and a description of the table’s integrity constraints DESCRIBE TABLE CONSTRAINTS A description of the table’s integrity constraints
Troubleshooting Integrity Constraint Violations proc sql; describe table constraints Discounts; quit;
If you specify UNDO_POLICY=NONE when correcting for constraint violations, ensure that you re-submit only the corrected data rows, or you might inadvertently add unwanted duplicates of the original non-rejected rows to the table.
proc sql; create table Discounts (Product_ID num format=z12., Start_Date date, End_Date date, Discount num format=PERCENT., constraint ok_discount check (Discount le .5)) ; insert into Discounts values (240500200009,'01Mar2007'd, '31Mar2007'd,.45) values (220200200036,'01Mar2007'd, '31Mar2007'd,.54) values (220200200038,'01Mar2007'd, '31Mar2007'd,.25); select * from discounts; quit;
/* correct second entry and re-run */ proc sql undo_policy=none; insert into Discounts values (240500200009,'01Mar2007'd, '31Mar2007'd,.45) values (220200200036,'01Mar2007'd, values (220200200038,'01Mar2007'd, '31Mar2007'd,.25); quit; proc sql; select * from discounts;