Sas 使用正则表达式删除宏变量括号内的文本

Sas 使用正则表达式删除宏变量括号内的文本,sas,Sas,我无法使用%sysfuncprxchange…删除括号和括号内的文本。。。。参见示例 %macro test(col=); %local result; %let result = %sysfunc(prxchange(s|\([^\)]+\)||i, -1, &col.)); %put &result.; %mend test; %let string = try (to) remove (this); %test(col=%str(&stri

我无法使用%sysfuncprxchange…删除括号和括号内的文本。。。。参见示例

%macro test(col=);
    %local result;
    %let result = %sysfunc(prxchange(s|\([^\)]+\)||i, -1, &col.));
    %put &result.;
%mend test;

%let string = try (to) remove (this);
%test(col=%str(&string.))

ERROR: Expected close parenthesis after macro function invocation not found.
预期输出应为try remove,忽略双空格

编辑-感谢@user667489,最简单的修复方法是

%macro test(col=);
    %local result;
    %let result = %sysfunc(compbl(%sysfunc(prxchange(s|%quote(\%([^\%)]+\%)||i), -1, &col.))));
    %put &result.;
%mend test;

%let string = try (to) remove (this);
%test(col=%str(&string.));

由于%sysfunc必须将宏代码转换为值才能推入您试图调用的函数,因此不确定是否可以修复此问题

为什么不把PRXCHANGE函数调用留在实际的SAS代码中呢

例如,可以让宏生成数据步骤。我建议只传入具有值文本的宏变量的名称和要将结果赋给的宏变量的名称

%macro test(invar,outvar);
%if not %symexist(&outvar) %then %global &outvar;
data _null_;
  call symputx("&outvar",prxchange('s|\([^\)]+\)||i', -1,symget("&invar")));
run;
%mend test;

%let string = try (to) remove (this);
%test(invar=string,outvar=result);
%Put &=result;

我找到了一种方法让它或多或少地发挥作用:

%macro test(col=);
    %local result regex;
    %let regex = %sysfunc(prxparse(%str(s/\%([^\%)]+\%)//)));
    %let result = %sysfunc(prxchange(&regex, -1, &col.));
    %syscall prxfree(regex);  /*Prevent memory leak*/
    %put &result.;
%mend test;

%let string = try (to) remove (this);
%test(col=%str(&string.));
使用%符号屏蔽正则表达式中的括号,以防止它们被解析为SAS代码,并使用单独的prxparse似乎可以做到这一点。

还可以尝试以下方法:

%macro string(string);
   %local new_string;
   %let new_string=%sysfunc(prxchange(s/\s?\(\w+\)//,-1,&string));
   %put &new_string;
%mend;

%string(%str(try (to) remove (this)));

谢谢-我还可以用%quote\%[^\%]+\%\\\\\\;| iNote替换我原来的正则表达式模式,以确保宏处理器匹配开头和结尾,因此问题的根源是正则表达式代码有两个,而且只有一个。因此,您可以将宏引用添加到其中一个。但是引用整个字符串会让人更清楚。请注意,您可能希望在测试用例的结果中包含对COMPBL的调用,以删除try和remove之间剩余的空间。或者你可以想出如何让正则表达式在要删除的字符中包含空格?@Tom-谢谢,你说得对。我只是太懒了