代码之家  ›  专栏  ›  技术社区  ›  Sandra Guerrero

宏变量包含另一个顺序为SAS的子字符串

  •  0
  • Sandra Guerrero  · 技术社区  · 7 年前

    我正在尝试验证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 回复  |  直到 7 年前
        1
  •  1
  •   momo1644    7 年前

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

    解决方案:

    %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.
    
        2
  •  1
  •   DomPazz    7 年前

    如果要检查是否存在任何单词,则需要在字符串中进行分析并针对每个单词运行:

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

    如果需要所有单词,则需要计算它解析为true的次数,并且仅当该次数等于计数时才输出。

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