Hello,
I am working with strings made up of 0, 1, and 2 and for each string I need to:
ID | MONTHSTR |
1 | 0000011220221000022222111111222222222220100022222222222222222222222220 |
2 | 22222222222000000222 |
3 | 2122 |
4 | 2220222022122221 |
5 | 11100000000000000101100000000000000000000 |
6 | 0022222222222222222 |
7 | 0022222222222222222 |
8 | 11100020002000222222222222222222222222222222222222222 |
I greatly appreciate any suggestion. Thanks!
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;
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;
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;
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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 save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.