Using the INSERT statement within the SQL procedure does not permit correlation with the table being inserted into


Inserting rows into an existing table using the SQL procedure produces errors when a variable being referenced in the SELECT clause does not exist in the table being referenced in the FROM clause. Errors similar to the following occur:

ERROR: INSERT statement does not permit correlation with the table being inserted into.
ERROR: The reference to xx appears to refer to a variable in the table being        inserted into.

For example, the table DS1 contains the variables ID, NAME, ADDRESS, PHONE, and XX. Whereas the table DS2 contains the variables ID, NAME, ADDRESS, and PHONE. Running the following code would produce the errors:

proc sql;

insert into ds1

select id, name, address, phone, xx

from ds2;

quit;

Because the variable XX does not exist in the table DS2, and the INSERT statement does not allow correlation with the table being inserted into, the reference to the variable XX results in errors. The following code would prevent the errors from occurring:

proc sql;

insert into ds1(id, name, address, phone)

select id, name, address, phone

from ds2;

quit;