- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Show us exactly what you want as output.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Great. Can you please tell me from where I can learn these Hash objects
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Suryakiran
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
genius