<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: find number of times a word appears in a string without spaces in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548529#M8548</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length str $18;
input str;
datalines;
121121121121121
121556121112141215
;
run;

data want;
set have;
count=count(str,'121');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 04 Apr 2019 14:59:12 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-04-04T14:59:12Z</dc:date>
    <item>
      <title>find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548447#M8518</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I've found a number of solutions to count words within a string in SAS. However they all do not work for strings without spaces.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this string (it's in character format !) : 121121121121121&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The string can also be like this : 121556121112141215&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like SAS to return 5 in the first case and 4 in the second case. How do I do this ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks !!!&lt;/P&gt;&lt;P&gt;B&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2019 10:21:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548447#M8518</guid>
      <dc:creator>Billybob73</dc:creator>
      <dc:date>2019-04-04T10:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548449#M8519</link>
      <description>&lt;P&gt;If you don't have a delimiter in the string, you have to explain what "word" means in your problem.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2019 10:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548449#M8519</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-04-04T10:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548455#M8520</link>
      <description>&lt;P&gt;Please elaborate why SAS should return 4 and 5 respectively.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2019 11:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548455#M8520</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-04-04T11:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548481#M8527</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172904"&gt;@Billybob73&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am assuming you want a count of 2 in the string values which you haven't mentioned that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; specifically asks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If my assumption is correct--&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Creating sample data HAVE using your 2 string values*/
data have;
length str $20;
str='121121121121121';
output;
str='121556121112141215';
output;
run;
/*This assumes you want the count of 2 from what I see 5 and 4*/
data want;
set have;
want=countc(str,'2');
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Apr 2019 13:06:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548481#M8527</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-04T13:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548513#M8538</link>
      <description>&lt;P&gt;My guess is that your pattern is '121' (PAT).&lt;/P&gt;
&lt;P&gt;There are are several ways to skin the cat,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
length str $18;
input str;
datalines;
121121121121121
121556121112141215
;
run;

data want;
   retain pat '121';
   lengpat = length(pat);
   set have;
   count = 0;
   k = 1;
   do while(1);
      k = find(str,'121',k);
      if k then count + 1;
      if k = 0 then leave;  
      k + lengpat;
   end;
keep str count;
run;   &lt;/PRE&gt;
&lt;P&gt;Another, much shorter way is to use difference of lengths of STR Before and End of compressing the pattern. Length function will return one for a null string. Therefore, lengthN is function is necessary after compression to catch the null string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data need;
   set have;
   retain pat '121';
   lengpat = length(pat);
   lengB = length(str);
   temp = str;
   temp = compress(temp,'121');
   lengE = lengthn(temp);
   count = floor((lengB - lengE)/lengpat);
keep str count;
run;
&lt;/PRE&gt;
&lt;P&gt;Still a better way is have your own function. The advantage is that you can change the pattern as you like. It is coded once, compiled and saved to a Library of your choice.&lt;/P&gt;
&lt;PRE&gt;proc fcmp outlib = work.cmput.lib;
   function patcount(str $, pat $);
   file log;
   patlen = length(pat);
   Blen = length(str);
   str = compress(str, pat);
   Elen = lengthn(str);
   rc = floor((Blen - Elen)/patlen);
   return(rc);
   endsub;
quit;

options cmplib = work.cmput; /* Like any other SAS Function.Found in */
                             /* this Library                         */

data fun;
   set have;
   pat = '123';
   count = patcount(str, pat);
drop pat;
run;
&lt;/PRE&gt;
&lt;P&gt;In case my guess is wrong, still the code will not change and you only need to change the PAT. Hope you will try this out.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2019 14:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548513#M8538</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-04-04T14:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548526#M8545</link>
      <description>&lt;P&gt;I think you read the pattern well:) Nicely done!&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2019 14:55:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548526#M8545</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-04T14:55:35Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548529#M8548</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length str $18;
input str;
datalines;
121121121121121
121556121112141215
;
run;

data want;
set have;
count=count(str,'121');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Apr 2019 14:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548529#M8548</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-04T14:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548539#M8550</link>
      <description>&lt;P&gt;NOvinosrin:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Count() function is a very good choice.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Apr 2019 15:34:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/548539#M8550</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-04-04T15:34:29Z</dc:date>
    </item>
    <item>
      <title>Re: find number of times a word appears in a string without spaces</title>
      <link>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/552401#M9186</link>
      <description>Great stuff!! Thanks very much</description>
      <pubDate>Fri, 19 Apr 2019 07:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/find-number-of-times-a-word-appears-in-a-string-without-spaces/m-p/552401#M9186</guid>
      <dc:creator>Billybob73</dc:creator>
      <dc:date>2019-04-19T07:54:59Z</dc:date>
    </item>
  </channel>
</rss>

