Dear sas users,
say, I have following codes:
data test;
string='Start ABC ABC ABC End';
new_string = prxchange("s/ABC/someotherstuff/", -1, string);
run;
apparently, there are 3 matches. My question is: is there any function(or option) so that the number of matches can be obtained?
data test;
string='Start ABC ABC ABC End';
s=1;e=length(string);
pid=prxparse("/\bABC\b/i");
call prxnext(pid,s,e,string,p,l);
n=0;
do while(p>0);
n+1;
call prxnext(pid,s,e,string,p,l);
end;
put n=;
run;
I don't think so. But if the replacement string is not the same length as the replaced string, you could calculate the number of replacements by comparing the total length before and after replacement. Otherwise, you can also loop on prxNext to count the matches.
data test;
string='Start ABC ABC ABC End';
s=1;e=length(string);
pid=prxparse("/\bABC\b/i");
call prxnext(pid,s,e,string,p,l);
n=0;
do while(p>0);
n+1;
call prxnext(pid,s,e,string,p,l);
end;
put n=;
run;
Thanks for help. Just wondering why SAS does not have this functionality in hand (or other language such as java/python have this function readily available?)
@lxl650 wrote:
Thanks for help. Just wondering why SAS does not have this functionality in hand (or other language such as java/python have this function readily available?)
Consider how much code would it take to create summary statistics for many (lets say 50 or so) numerical variables such as min, mean, max, standard deviation, skewness, and a variety of quantiles for each combination of 3 or more grouping variable and get summaries not only for combinations 3 at a time but 2 at a time, singly and the data as a whole in Java/python?
4 lines of code in SAS proc means/ summary (5 if you count the Run;). Different language purposes different tools.
And I shudder to think of solving this question in the first version of FORTRAN I learned when the longest character value you could have was 4 characters and the "phrase" would have been stuffed into an array of multiple length 4 elements and likely not considering word breaks for the "stuffing".
If your matched pattern is simple , like your post, count() is right tool.
data test;
string='Start ABC ABC ABC End';
n=count(string,'ABC','i');
put n=;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.