I would appreciate any help in trying to count the number of matches of a regular expression function in SAS. I need to count the number of times a financial transaction is present in a text string.
I have been able to write the expression to find the transactions but have not been able to count how many times it may appear in a string. For illustration, I have created the following table:
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;
And I have created a variable which can check if a transaction is present.
DATA Want;
Set Transactions;
QtdMatch = PRXMATCH("/ R\$ ?\d /",String);
RUN;
However, what I need to have is a variable counting the number os transactions. So, in the first string the variable would read 1, for the 2nd string 2 and for the final example 0. My strings could contain up to 50 transactions which would need to be counted.
CodePudding user response:
You can iterate through all the matches using 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;
Output:
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