BookmarkSubscribeRSS Feed
s_manoj
Quartz | Level 8

Hi all,

     I have been trying to find repeated alphabets in a given string.

 

for example, I have a string like "REPEATED". I want to find repeated alphabet in it and its count.

 

can someone help me with its code..

 

Thanks in advance

 

 

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are numerous examples out there, just search on Goolge.  One way would be to output each character as a new observation, then proc freq it. Another would be to loop over each character in the string, and have an array for each letter, and add to that.  The real question here is why?  I don't see any value in counting characters, unless of course you have more than one data item in a variable, which isn't good practice.  Post test data, form of a datastep, and what you want to see out for better answers.

 

ballardw
Super User

Does case matter? For instance in "Aa" do you want a count of 1 for "A" and 1 for "a" or 2 for "A"?

 

You really need to provide some example data and what the output would look like as there are several ways to interpret and provide results.

Reeza
Super User

Show us exactly what you want as output.

novinosrin
Tourmaline | Level 20
data have;
string='REPEATED';
run;
data _null_;
if _n_=1 then do;
 dcl hash H () ;
   h.definekey  ("char") ;
   h.definedata ('char',"count") ;
   h.definedone();
end;
set have;
do _n_=1 to length(string);
char=substr(string,_n_,1);
if h.find() ne 0 then do;count=1;h.add();end;
else do;count=count+1;h.replace();end;
end;
h.output(dataset:'want');
run;
Jay23
Fluorite | Level 6

Great. Can you please tell me from where I can learn these Hash objects

SuryaKiran
Meteorite | Level 14

Why do you need the count of alphabets?  

 

Here is one way

data test (keep=String STR_Count STR_Val);
String="REPEATED";
String_=String;
do until (length(String_)=1);
STR_Count=count(String_,substr(String_,1,1));
STR_Val=substr(String_,1,1);
String_=COMPRESS(String_,substr(String_,1,1));
output;
end;
run;

PROC TRANSPOSE data=test out=want(drop=_name_) prefix=Alphabet_;
by String;
id STR_Val;
var STR_Count;
run;

 

If mixed case then use "i" modifier. 

data test (keep=String STR_Count STR_Val);
String="RePEATED";
String_=String;
do until (length(String_)=1);
STR_Count=count(String_,substr(String_,1,1),'i');
STR_Val=substr(String_,1,1);
String_=COMPRESS(String_,substr(String_,1,1),'i');
output;
end;
run;

PROC TRANSPOSE data=test out=want(drop=_name_) prefix=Alphabet_;
by String;
id STR_Val;
var STR_Count;
run;
Thanks,
Suryakiran
Jay23
Fluorite | Level 6

Hi,

This is so ingenious. Thank you.

Can you please tell me why you have used the compress function in the last step?

I tried running the code without it and it continues to run for indefinite time.

Thanks again

novinosrin
Tourmaline | Level 20

Most simplest:

 

data have;
string='REPEATED';
run;
data temp;
set have;
grp+1;
do _n_=1 to length(string);
char=char(string,_n_);
output;
end;
run;

proc freq data=temp;
by grp;
tables char/out=want(keep=char count);
run;
Jay23
Fluorite | Level 6

genius

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 2570 views
  • 6 likes
  • 7 in conversation