使用SAS Proc SQL进行条件插入

gdo*_*371 0 sql sas sql-insert

我试图将一个较小的表中的记录添加到一个非常大的表中,如果较小的表中的行的主键值不在较大的表中:

data test;
 Length B C $4;
 infile datalines delimiter=',';
 input a b $ c $;
 datalines;
1000,Test,File
2000,Test,File
3000,Test,File
;

data test2;
 Length B C $4;
 infile datalines delimiter=',';
 input a b $ c $;
 datalines;
1000,Test,File
4000,Test,File
;

proc sql;
insert into test
select * from test2
where a not in (select a from test2);
quit;
Run Code Online (Sandbox Code Playgroud)

然而,这不会将记录插入表中Test.谁能告诉我我做错了什么?最终结果应该是应该将a = 4000的行添加到表中Test.

编辑:

使用where a not in (select a from test)是我最初尝试的,它产生了以下错误:

WARNING: This DELETE/INSERT statement recursively references the target table. A consequence of this is a possible data integrity problem.
ERROR: You cannot reopen WORK.TEST.DATA for update access with member-level control because WORK.TEST.DATA is in use by you in resource
environment SQL.
ERROR: PROC SQL could not undo this statement if an ERROR were to happen as it could not obtain exclusive access to the data set. This
       statement will not execute as the SQL option UNDO_POLICY=REQUIRED is in effect.
224  quit;
Run Code Online (Sandbox Code Playgroud)

谢谢

Tom*_*Tom 6

您可以分两步完成此过程.首先创建要插入的记录表,然后插入它们.

proc sql ;
  create table to_add as 
    select * from test2
    where a not in (select a from test)
  ;
  insert into test select * from to_add ;
quit;
Run Code Online (Sandbox Code Playgroud)

或者您可以只更改UNDO_POLICY选项的设置,SAS将允许您在更新TEST时参考TEST.

proc sql undo_policy=none;
  insert into test
    select * from test2
    where a not in (select a from test)
  ;
quit;
Run Code Online (Sandbox Code Playgroud)