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
2 01 NDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETDRECA
1 02 ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD
1 03 ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD
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
Just loop over the characters 1 by 1, and if char + 1 = RE increment count:
data have; length s $2000; input s $; datalines; NDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETDRECA ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD ; 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;
Just loop over the characters 1 by 1, and if char + 1 = RE increment count:
data have; length s $2000; input s $; datalines; NDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETDRECA ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD ; 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;
@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
2 01 NDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETDRECA
1 02 ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD
1 03 ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD
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;
NDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETDRECA
ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD
ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD
ENDCSPCRCBCSLAFDWIWOFDSP
;;;;
run;
proc print;
run;
Can you use COUNT()? data have; length s $2000; input s $; datalines; NDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETDRECA ENDCSPCRCBCSLAFDWIWOFDSPDERRDETDRECADETD ; 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;
that works too. Thanks Ksharp
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.