【问题标题】:SAS Macro, passing value as string to where clauseSAS宏,将值作为字符串传递给where子句
【发布时间】:2014-07-22 15:37:33
【问题描述】:

我下面的 SAS 宏不起作用 --- 这个 sn-p 不返回任何值,因为 where 语句不起作用。有人有想法么?我尝试添加 %str,但也没有用。

%macro refreshments(beverage_type=);

proc sql;
select

*

where drink_type = '&beverage_type.'
;
quit;

%mend

%refreshments(Sprite);

谢谢。

【问题讨论】:

  • 您的 SQL 缺少“from”子句。您需要指定要查询的表的名称。
  • 以后,如果您在使宏工作时遇到困难,请尝试删除宏代码并将其作为常规 SAS 代码运行。一旦工作正常,重新添加宏代码。

标签: sas sas-macro


【解决方案1】:

宏变量不会用单引号解析。您还缺少 FROM 子句,并且宏参数被提供为位置(而不是 name=value 对)。请尝试以下操作:

%macro refreshments(beverage_type=);
  proc sql;
  select * 
    from YOURTABLE
    where drink_type = "&beverage_type";
%mend;

%refreshments(beverage_type=Sprite);

【讨论】:

  • 您还将单引号更改为双引号,这将产生差异。
  • 注意,如果您使用对某些 DBMS 的传递(不允许以这种方式使用双引号,因为它们使用双引号括住列名),这将不起作用。例如,请参阅stackoverflow.com/questions/18323619/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
相关资源
最近更新 更多