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

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
PaigeMiller
Diamond | Level 26
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

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
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
mh04
Obsidian | Level 7
Thank you! You both make it look so easy...
novinosrin
Tourmaline | Level 20


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;

novinosrin
Tourmaline | Level 20

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;

smantha
Lapis Lazuli | Level 10
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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 2341 views
  • 4 likes
  • 4 in conversation