使用 SQL 更新 SAS 中的表时无法重新打开错误



我有 2 个表:

  • t1A, B, X, Y, Z
  • t2A, B, N

我想将t1.A设置为值t2.N t1.A = t2.A and t1.B = T2.B的位置。

这听起来很简单,但我不知道解决这个问题;我尝试了类似的东西:

update t1 set A = (select t2.N 
                     from t1,t2 
                    where t1.A = t2.A 
                      and t1.B = t2.B)
......

但这给出了一个错误:

ERROR: You cannot reopen t1.DATA for update access with member-level control because t1.DATA .....

有什么想法吗?

我怀疑你只需要一个相关的子查询:

update t1
    set A = (select t2.N 
             from t2 
             where t1.A = t2.A and t1.B = t2.B
            );
注意

:您应该注意子查询仅返回一行。

除了语法错误之外的旁注:您之所以得到ERROR: You cannot open WORK.T1.DATA for output access with member-level control because WORK.T1.DATA is in use by you in resource environment DATASTEP.,是因为您的输出数据集仍在窗口中打开。在重新运行之前关闭VIEWTABLE:Work.T1

复制t1(例如,我们称之为t1_copy),并在 select 语句中使用该副本。表复制的原因是您无法从要更新的表中进行选择。

%_eg_conditional_dropds(t1_copy); /* drop the copy before using it (if one exists) */
PROC SQL;
    CREATE TABLE t1_copy AS
        SELECT * FROM t1;
   update t1 set A = (select t2.N 
                     from t1_copy,t2 
                    where t1_copy.A = t2.A 
                      and t1_copy.B = t2.B)
QUIT;

最新更新