宏变量以任意顺序包含另一个子字符串 SAS

Macro variable contains another substrings in either order SAS

我正在尝试验证 char 宏变量是否按任一顺序包含另一个子字符串。

%let variable = Coop Fin TDC Real Telco;

options mlogic mprint symbolgen;
%Macro Test/minoperator;
    %if Coop Fin in &variable %then %put i = 1;
    %else %put i = 0;
%mend;
%Test;

Coop Fin in &variable 解析为 TRUE,但 Coop TDC 解析为 FALSE。 没有进口订单,有什么形式可以做到吗?

如果你想检查是否存在任何单词,那么你需要在字符串中解析每个单词 运行:

%let variable = Coop Fin TDC Real Telco;

options mlogic mprint symbolgen;
%Macro Test(input) /minoperator;
    %local j n str;
    %let n=%sysfunc(countw(&input));
    %let i=0;
    %do j=1 %to &n;
        %let str = %scan(&input,&j);
        %if &str in &variable %then %let i = 1;
        %else %put i = 0;
        ;
    %end;
    %put i = &i;
%mend;
%Test(Coop Fin);
%Test(Coop TDC);

如果你想要所有的单词,那么你需要计算它解析为真的次数,并且只有当它等于计数时才输出。

%let variable = Coop Fin TDC Real Telco;

options mlogic mprint symbolgen;
%Macro Test(input) /minoperator;
    %local j n str;
    %let n=%sysfunc(countw(&input));
    %let i=0;
    %do j=1 %to &n;
        %let str = %scan(&input,&j);
        %if &str in &variable %then %let i = %eval(&i+1);
        ;
    %end;
    %if &i=&n %then 
       %put i = &i;
    %else %put i = 0;
    ;
%mend;
%Test(Coop Fin);
%Test(Coop TDC x);

可以做一个正则表达式匹配,下面的逻辑忽略顺序:

解决方案:

%let variable = Coop Fin TDC Real Telco;
options mlogic mprint symbolgen;
%Macro Test/minoperator;
    %if %sysfunc(prxmatch('Coop',"&variable.")) & %sysfunc(prxmatch('TDC',"&variable."))  %then %put i = 1;
    %else %put i = 0;
%mend;
%Test;

输出:

MLOGIC(TEST):  Beginning execution.
SYMBOLGEN:  Macro variable VARIABLE resolves to Coop Fin TDC Real Telco
SYMBOLGEN:  Macro variable VARIABLE resolves to Coop Fin TDC Real Telco
MLOGIC(TEST):  %IF condition %sysfunc(prxmatch('Coop',"&variable.")) & %sysfunc(prxmatch('TDC',"&variable.")) is TRUE
MLOGIC(TEST):  %PUT i = 1
i = 1
MLOGIC(TEST):  Ending execution.