如果您能帮助我尝试计算 SAS 中正则表达式函数的匹配数,我将不胜感激。我需要计算文本字符串中出现金融交易的次数。
我已经能够编写表达式来查找交易,但无法计算它在字符串中出现的次数。为了便于说明,我创建了下表:
DATA Transactions;
infile datalines dlm=' ';
INPUT ID STRING $40.;
DATALINES;
1 One transaction R$250
2 One instance of R$ 250 and one of R$ 200
3 Nothing to see here R$
;
RUN;
我创建了一个变量,可以检查交易是否存在。
DATA Want;
Set Transactions;
QtdMatch = PRXMATCH("/ R\$ ?\d+/",String);
RUN;
但是,我需要的是一个计算操作系统事务数量的变量。因此,在第一个字符串中,变量将读取为 1,第二个字符串为 2,最后一个示例为 0。我的字符串最多可以包含 50 个需要计数的事务。
最佳答案
您可以使用call prxnext
遍历所有匹配项。 This solution is adapted from the SAS documentation on CALL PRXNEXT .
data want;
set transactions;
regex = prxparse("/ R\$ ?\d+/");
stop = length(string);
transactions = 0;
call prxnext(regex, 1, stop, string, position, length);
do while (position > 0);
transactions+1;
call prxnext(regex, 1, stop, string, position, length);
end;
run;
输出:
ID STRING transactions
1 One transaction R$250 1
2 One instance of R$ 250 and one of R$ 250 2
3 Nothing to see here R$ 0
关于regex - 统计 SAS 中正则表达式匹配者的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72621322/