如何在SAS存储过程中处理多个过滤器

问题描述 投票:0回答:1

我正在创建一个SAS存储过程报告,用户可以使用不同的自助餐厅过滤数据。

这就是我正在做的事情

%let ID_WHERE_CLAUSE=; 
%let Source = "group";
%let ActionRequired = "Daily";


%macro SetFilters;

data _null_; 

%if &&ActionRequired ne "A" %then %do;
%let test = "ActionR";
    call symputx('ID_WHERE_CLAUSE',cats(' and ActionRequired = ',' &ActionRequired',''));
%end;


%if &&Source ne "A" %then %do;
%let test = "Source";
    call symputx('ID_WHERE_CLAUSE',cats('and Source = ',' &Source',''));
%end;

run;

%mend; %SetFilters;

%put &ID_WHERE_CLAUSE;

在ID_WHERE_CLAUSE数据应该是and action required = "Daily" and source = "group"但我的代码只是将最后一个过滤器附加到我的ID_WHERE_CLAUSE变量为and source = "group"

我的预期结果是and action required = "Daily" and source = "group"

但实际结果我得到的是and source = "group"

这就是为什么我没有得到我预期的结果。如何在我的ID_WHERE_CLAUSE变量中附加所有条件。

我也尝试了这个但是也没用。

call symputx('ID_WHERE_CLAUSE',' and ActionRequired =  &ActionRequired','');
call symputx('ID_WHERE_CLAUSE', ' and Source =  &Source');

我正在使用它

proc sql noprint;
create table filter_data as
select * from data 
where 1=1 &ID_WHERE_CLAUSE;
quit;

请告诉我如何连接所有where子句或任何更好的方法。

sql sas sas-macro
1个回答
1
投票

call symputx不附加宏变量 - 它会覆盖它们。因此,您需要捕获现有值作为要设置的新值的一部分。例如。

call symputx('ID_WHERE_CLAUSE',cats(symget("ID_WHERE_CLAUSE"),'and Source = ',' &Source',''));

您需要使用symget在您的场景中执行此操作,而不是直接引用宏变量,否则可能会在您的早期数据步骤执行之前解析并设置初始值。

© www.soinside.com 2019 - 2024. All rights reserved.