BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lxl650
Calcite | Level 5

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

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.

PG
Ksharp
Super User
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;
lxl650
Calcite | Level 5

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?)

ballardw
Super User

@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".

Ksharp
Super User

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1212 views
  • 1 like
  • 4 in conversation