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

标签 sas

我无法使用 %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(&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 似乎可以解决问题。

关于sas - 使用正则表达式删除宏变量括号内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56839376/

相关文章:

mysql - SAS XML 映射 - 具有多个 XML 文件

hadoop - 使用超过15位数字的数值变量将sas数据写入Hive

sas - 如何在 SAS libname 中获取最近创建的数据集的日期

r - 按因素水平和总计汇总

r - 查找日期差异

sas - sysfunc 不工作。

sas - Sql、Compged、Min 和空白

将 SAS 连接到 C 库

string - SAS - 如果长度一定,则替换字符串中的第 n 个单词

sas - SAS 如何在没有 'retain' 语句的情况下保留前一行的值?