简体   繁体   English

在以表别名为前缀的字段上使用“计算”​​功能-SAS SQL

[英]Using 'Calculated' function on field prefixed with table alias - SAS SQL

I am trying to get the following Proc SQL step to work in SAS 9.2, but it is falling over as it doesn't seem to like my using a table alias to define a field that is in both tables during a merge: 我正在尝试使下面的Proc SQL步骤在SAS 9.2中工作,但由于我似乎不喜欢使用表别名来定义合并期间两个表中的字段,因此该操作已经失败了:

proc sql;
create table test1 as 
select 
a.var1,
b.var2 * -1 as var2,
b.var3,
calculated b.var1 * a.var2 as var4
from table1
left join
table2
on a.var5 = b.var5;
quit;

Without the table alias between the 'Calculated' command and the field name, this code would execute without issue. 在“计算的”命令和字段名称之间没有表别名的情况下,此代码将执行而不会出现问题。 Does anyone know the syntax I am looking for to get around this? 有谁知道我要解决的语法?

If I don't use the calculated command the SQL step does not recognise that the calculation used when defining the field 'var1'. 如果我不使用计算的命令,则SQL步骤无法识别定义字段“ var1”时使用的计算。

Thanks 谢谢

CALCULATED tells it to refer to a column that exists in the output (in the select), not in an input dataset, so the table alias doesn't make any sense. CALCULATED告诉它引用输出中(在选择中)而不是输入数据集中存在的列,因此表别名没有任何意义。 CALCULATED var2 is all you need. 您只需要CALCULATED var2

proc sql;
  create table test1 as 
   select 
     a.var1,
     b.var2 * -1 as var2,
     b.var3,
     calculated var2 * a.var1 as var4
   from table1
    left join table2
    on a.id=b.id;
quit;

That would be okay, as long as it didn't decide b.var2 * -1 was just a rename. 只要没有确定b.var2 * -1只是一个重命名, b.var2 * -1可以了。

You are attempting to create a table that has two columns with the same name ( var1 ). 您试图创建一个包含两列具有相同名称( var1 )的表。 I'm not surprised that proc sql is complaining. 我对proc sql抱怨并不感到惊讶。 Try giving them different names: 尝试给他们使用其他名称:

proc sql;
create table test1 as 
    select a.var1 as a_var1,
           b.var1 * -1 as b_var1,
           b.var2,
           calculated b_var1 * a.var1 as var3
    from table1 left join
         table2
         on a.var4 = b.var4;
quit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM