<?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 How many times a word occurs in a string? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445491#M111632</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I count the number of times a string occurs in a bigger string in SAS?&lt;/P&gt;&lt;P&gt;Basically a variable with a lot of text and I need to determine how many times a certain thing occurs in each of the rows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;A variable has a value "This is is a great day" for row 1&lt;BR /&gt;I need to know how many time 'is' occurred.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Similarly for all rows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been using prxmatch to know if it occurs or not. Cant figure out how to calculate number of occurrences.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Mar 2018 14:43:37 GMT</pubDate>
    <dc:creator>silvergrenade</dc:creator>
    <dc:date>2018-03-14T14:43:37Z</dc:date>
    <item>
      <title>How many times a word occurs in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445491#M111632</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I count the number of times a string occurs in a bigger string in SAS?&lt;/P&gt;&lt;P&gt;Basically a variable with a lot of text and I need to determine how many times a certain thing occurs in each of the rows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&amp;nbsp;A variable has a value "This is is a great day" for row 1&lt;BR /&gt;I need to know how many time 'is' occurred.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Similarly for all rows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been using prxmatch to know if it occurs or not. Cant figure out how to calculate number of occurrences.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 14:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445491#M111632</guid>
      <dc:creator>silvergrenade</dc:creator>
      <dc:date>2018-03-14T14:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: How many times a word occurs in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445495#M111634</link>
      <description>&lt;P&gt;How much further are you taking this analysis?&lt;BR /&gt;My suggestion would be to separate each word from a sentence into it's own record and then use PROC FREQ.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example at finding all two word combinations or how to split a file:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Create sample data;
data random_sentences;
	infile cards truncover;
	informat sentence $256.;
	input sentence $256.;
	cards;
This is a random sentence
This is another random sentence
Happy Birthday
My job sucks.
This is a good idea, not.
This is an awesome idea!
How are you today?
Does this make sense?
Have a great day!
;

;
;
;
*Partition into words;
data f1;
	set random_sentences;
	id=_n_;
	nwords=countw(sentence);
	nchar=length(compress(sentence));

	do word_order=1 to nwords;
		word=scan(sentence, word_order);
		output;
	end;
run;

proc sql;
	create table words2 as
		select t1.sentence, lowcase(t1.word) as word1, lowcase(t2.word) as word2
			from f1 as t1
				cross join f1 as t2
			where t1.sentence=t2.sentence 
				and t1.word_order &amp;gt; t2.word_order
			order by t1.sentence, t1.word_order;
quit;

proc freq data=words2 noprint order=freq;
	table word1*word2 /list out=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Mar 2018 14:52:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445495#M111634</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-14T14:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: How many times a word occurs in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445497#M111636</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count = 0;
do i = 1 to countw(yourvar);
  count = count + (scan(yourvar,i) = testvar);
end;
drop i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Applied to your example,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
testvar = 'is';
yourvar = "This is is a great day";
count = 0;
do i = 1 to countw(yourvar);
  count = count + (scan(yourvar,i) = testvar);
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Mar 2018 14:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445497#M111636</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-14T14:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: How many times a word occurs in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445498#M111637</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;Example:&amp;nbsp;A variable has a value "This is is a great day" for row 1&lt;BR /&gt;I need to know how many time 'is' occurred.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Seems to me the answer is 3. Is that right? Can you confirm? Your title indicates you are counting words, but your first sentence indicates you want to know how many times a STRING appears in a bigger string. These are not the same! We need clarification.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code provided by others is looking for entire words that match 'is' and would not count the letters 'is' in the word "This".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:04:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445498#M111637</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-03-14T15:04:27Z</dc:date>
    </item>
    <item>
      <title>Re: How many times a word occurs in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445501#M111638</link>
      <description>&lt;P&gt;Hi Paige,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm sorry for that error.&lt;/P&gt;&lt;P&gt;I didnt see the is in This.&lt;/P&gt;&lt;P&gt;Yes, the count should be 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:15:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445501#M111638</guid>
      <dc:creator>silvergrenade</dc:creator>
      <dc:date>2018-03-14T15:15:05Z</dc:date>
    </item>
    <item>
      <title>Re: How many times a word occurs in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445505#M111640</link>
      <description>&lt;P&gt;So it should be&lt;/P&gt;
&lt;P&gt;"count how many times a string appears in another string"&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:21:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445505#M111640</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-14T15:21:01Z</dc:date>
    </item>
    <item>
      <title>Re: How many times a word occurs in a string?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445511#M111645</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;I figured out a way on achieving my problem. Is there a better way of achieving the same?&lt;/P&gt;&lt;P&gt;OpportunityDescription is my variable while "F2F", "face to face" are my test strings.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data out.open_records_opp_desc;
set out.open_records;

Count_1 = count(OpportunityDescription,'F2F','i');

Count_2 = count(OpportunityDescription,'face to face','i');
Count_3 = count(OpportunityDescription,'in person','i');
Count_4 = count(OpportunityDescription,'meeting','i');
Count_5 = count(OpportunityDescription,'met','i');
Count_6 = count(OpportunityDescription,'discussed','i');
Count_7 = count(OpportunityDescription,'one on one','i');
Count_8 = count(OpportunityDescription,'discussed','i');
Count_9 = count(OpportunityDescription,'contact','i');

count_f2f_occurance = sum(Count_1,Count_2,Count_3,Count_4,Count_5,Count_6,Count_7,Count_8,Count_9);

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 15:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-times-a-word-occurs-in-a-string/m-p/445511#M111645</guid>
      <dc:creator>silvergrenade</dc:creator>
      <dc:date>2018-03-14T15:29:48Z</dc:date>
    </item>
  </channel>
</rss>

