## Count presence of two-character string among many characters

Solved
Frequent Contributor
Posts: 123

# Count presence of two-character string among many characters

hi,

I need to count times string "RE" appears in multicharacter string. String is always char.

Example:

Data_Want     Column_have_id     Column_have_string

0                               04                   ENDCSPCRCBCSLAFDWIWOFDSP

In the first string of data in the above example, "RE" is read twice. ("RE" should be captured as is, not "ER"...).

thank you

Accepted Solutions
Solution
‎03-07-2017 08:34 AM
Super User
Posts: 9,599

## Re: Count presence of two-character string among many characters

Just loop over the characters 1 by 1, and if char + 1 = RE increment count:

```data have;
length s \$2000;
input s \$;
datalines;
;
run;

data want (drop=i);
set have;
count=0;
do i=1 to lengthn(s)-1;
if cats(char(s,i),char(s,i+1))="RE" then count=sum(count,1);
end;
run;```

All Replies
Solution
‎03-07-2017 08:34 AM
Super User
Posts: 9,599

## Re: Count presence of two-character string among many characters

Just loop over the characters 1 by 1, and if char + 1 = RE increment count:

```data have;
length s \$2000;
input s \$;
datalines;
;
run;

data want (drop=i);
set have;
count=0;
do i=1 to lengthn(s)-1;
if cats(char(s,i),char(s,i+1))="RE" then count=sum(count,1);
end;
run;```
Frequent Contributor
Posts: 123

## Re: Count presence of two-character string among many characters

this is very helpful, thanks again
Posts: 3,852

## Re: Count presence of two-character string among many characters

brulard wrote:

hi,

I need to count times string "RE" appears in multicharacter string. String is always char.

Example:

Data_Want     Column_have_id     Column_have_string

0                               04                   ENDCSPCRCBCSLAFDWIWOFDSP

In the first string of data in the above example, "RE" is read twice. ("RE" should be captured as is, not "ER"...).

thank you

The FIND function is a good choice for this problem.

``````data re;
input s \$80.;
count=0;
do x=1 by 0 until(f eq 0);
f=find(s,'RE',x);
if f then count+1;
x=f+2;
end;
drop x f;
cards;
ENDCSPCRCBCSLAFDWIWOFDSP
;;;;
run;
proc print;
run;``````

Super User
Posts: 10,784

## Re: Count presence of two-character string among many characters

```Can you use COUNT()?

data have;
length s \$2000;
input s \$;
datalines;
;
run;

data want (drop=i);
set have;
count=0;
do i=1 to lengthn(s)-1;
if cats(char(s,i),char(s,i+1))="RE" then count=sum(count,1);
end;

c=count(s,'RE');

run;
proc print;run;

```
Frequent Contributor
Posts: 123

## Re: Count presence of two-character string among many characters

that works too. Thanks Ksharp

☑ This topic is solved.