🔒 This topic is solved and locked.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 07-30-2020 12:50 PM
(3065 views)
Hello,
I am working with strings made up of 0, 1, and 2 and for each string I need to:
- Count how many groups of three consecutive 2s or more each string contains. For ID #1, it should identify three groups because there are three sequences of 2s containing at least three consecutive 2s; for ID #3 it should identify 0 groups. Groups containing more than three consecutive 2s should be counted only once. My code is counting all groups of three consecutive 2s, including within the same sequence...😑
- Identify how long the longest sequence of consecutive 2s (made up of at least three consecutive 2s) is. For ID #1, it should look at all three sequences and identify that the longest equals 25; for ID #2, the longest sequence would be 11. I do have a clunky way of getting this – basically looking for the longest spell and working backwards – but I’d like to find a more efficient way of doing that.
ID | MONTHSTR |
1 | 0000011220221000022222111111222222222220100022222222222222222222222220 |
2 | 22222222222000000222 |
3 | 2122 |
4 | 2220222022122221 |
5 | 11100000000000000101100000000000000000000 |
6 | 0022222222222222222 |
7 | 0022222222222222222 |
8 | 11100020002000222222222222222222222222222222222222222 |
I greatly appreciate any suggestion. Thanks!
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
set have;
sum=0;
groups_of_3consec_twos=0;
longest_string_of_twos=0;
do i=1 to length(monthstr);
char=substr(monthstr,i,1);
if char^=2 then sum=0;
else sum=sum+1;
if sum=3 then groups_of_3consec_twos = groups_of_3consec_twos+1;
if sum>longest_string_of_twos then longest_string_of_twos=sum;
end;
drop i char sum;
run;
--
Paige Miller
Paige Miller
6 REPLIES 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
set have;
sum=0;
groups_of_3consec_twos=0;
longest_string_of_twos=0;
do i=1 to length(monthstr);
char=substr(monthstr,i,1);
if char^=2 then sum=0;
else sum=sum+1;
if sum=3 then groups_of_3consec_twos = groups_of_3consec_twos+1;
if sum>longest_string_of_twos then longest_string_of_twos=sum;
end;
drop i char sum;
run;
--
Paige Miller
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you! You both make it look so easy...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input ID MONTHSTR : $75.;
cards;
1
0000011220221000022222111111222222222220100022222222222222222222222220
2
22222222222000000222
3
2122
4
2220222022122221
5
11100000000000000101100000000000000000000
6
0022222222222222222
7
0022222222222222222
8
11100020002000222222222222222222222222222222222222222
;
data want;
set have;
want=0;
do _n_=1 to countw(MONTHSTR,'2','k');
want=sum(lengthn(scan(MONTHSTR,_n_,'2','k'))>=3,want);
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Forgot the longest in my previous-
data have;
input ID MONTHSTR : $75.;
cards;
1
0000011220221000022222111111222222222220100022222222222222222222222220
2
22222222222000000222
3
2122
4
2220222022122221
5
11100000000000000101100000000000000000000
6
0022222222222222222
7
0022222222222222222
8
11100020002000222222222222222222222222222222222222222
;
data want;
set have;
want=0;
longest=0;
do _iorc_=1 to countw(MONTHSTR,'2','k');
_n_=lengthn(scan(MONTHSTR,_iorc_,'2','k'));
want=sum(_n_>=3,want);
longest=_n_<>longest;
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data string;
informat string1 string2 string3 $100.;
infile cards dlm=',';
input string1 $ x $;
count=0;
max_len=0;
drop i string2 string3 len x;
if index(string1,'222') > 0 then do;
string2 = translate(tranwrd(string1,'0','|'),'|','13456798 0');
put string2=;
do i = 1 to countc(string2,'|');
string3=scan(string2,i,'|');
put i= string3=;
len=length(compbl(strip(scan(string2,i,'|'))));
max_len=max(max_len,len);
count=(len > 3) + count;
end;
end;
cards;
0000011220221000022222111111222222222220100022222222222222222222222220,x
22222222222000000222,x
2122,x
2220222022122221,x
11100000000000000101100000000000000000000,x
0022222222222222222,x
0022222222222222222,x
11100020002000222222222222222222222222222222222222222,x
;;;
run;