我无法使用 %sysfunc(prxchange(...))
删除括号和括号内的文本。查看示例
%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.
预期输出应为尝试删除
(忽略双空格)
编辑 - 感谢@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.));
最佳答案
我找到了一种让它或多或少按原样工作的方法:
%macro test(col=);
%local result regex;
%let regex = %sysfunc(prxparse(%str(s/\%([^\%)]+\%)//)));
%let result = %sysfunc(prxchange(®ex, -1, &col.));
%syscall prxfree(regex); /*Prevent memory leak*/
%put &result.;
%mend test;
%let string = try (to) remove (this);
%test(col=%str(&string.));
使用 %
符号屏蔽正则表达式中的括号以防止它们被解析为 SAS 代码,并使用单独的 prxparse
似乎可以解决问题。
关于sas - 使用正则表达式删除宏变量括号内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56839376/