宏变量包含SAS中的另一个子串

时间:2018-02-09 14:23:42

标签: sas char contains

我尝试验证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&变量解析为TRUE,但Coop TDC解析为FALSE。 有没有形式可以做,没有进口订单?

2 个答案:

答案 0 :(得分:1)

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

%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);

答案 1 :(得分:1)

您可以进行正则表达式匹配,下面的逻辑忽略了顺序:

解决方案:

plot(x = results.student1, y = results.student2)

输出:

%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;