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

Hi! I have a string like this one 'aaccbdaaaccc', and I need to find the character that occurs the most in consecutive order, and how many times it occurs. I thus need two variables as the output. I do not want to consider the caracter 'a'. The answer should be 'c' and '3'. Do anyone have a solution to this? Help is very much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data _null_;
x='aaccbdaaaccc';
length str max_str  $ 200;
pid=prxparse('/(\w)\1*/');
start=1;
stop=length(x);
max_len=0;
call prxnext(pid,start,stop,x,p,l);
do while(p>0);
 str=substr(x,p,l);
 len=length(str);
 if str not =: 'a' and len gt max_len then do;
  max_str=str;
  max_len=len;
 end;
 call prxnext(pid,start,stop,x,p,l);
end;
putlog max_str= max_len= ;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

You've posted this in Enterprise Guide, are you using the query builder or familiar with SAS data steps?

ak2
Calcite | Level 5 ak2
Calcite | Level 5
I'm writing a data step.
Ksharp
Super User
data _null_;
x='aaccbdaaaccc';
length str max_str  $ 200;
pid=prxparse('/(\w)\1*/');
start=1;
stop=length(x);
max_len=0;
call prxnext(pid,start,stop,x,p,l);
do while(p>0);
 str=substr(x,p,l);
 len=length(str);
 if str not =: 'a' and len gt max_len then do;
  max_str=str;
  max_len=len;
 end;
 call prxnext(pid,start,stop,x,p,l);
end;
putlog max_str= max_len= ;
run;
ak2
Calcite | Level 5 ak2
Calcite | Level 5
This worked fine! Thank you so much for your help!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 4 replies
  • 2139 views
  • 1 like
  • 3 in conversation