<?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: count occurrences excluding few conditions (regular expressions) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-occurrences-excluding-few-conditions-regular-expressions/m-p/679393#M205144</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;maybe this will help you, I added some "special" cases for testing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string $ 50.;
cards4;
Tom has a cat
John has a dog
Anna has a parrot
Frank has a dog
Anna has a cow
John has a tiger
Tom AB has a tiger
Tom A41 has a lion
Tom B12 has a dolphin
Tom 2 has a horse
Tom-32 has a dog
Bob has a parrot and Cedrick has a parrot
Alex has a cat and Alex has a dog and Alex AB13 has a pig
;;;;
run;

data names;
input name $ 12.;
cards;
Tom
John
Frank
Anna
Bob
Cedrick
Alex
;
run;

data _NULL_;
  call symputX("namesNo", nobs);
  stop;
  set names nobs=nobs;
run;


data _null_;
  declare hash H();
  H.defineKey('name');
  H.defineData('name', 'cnt');
  H.defineDone();

  do until(eoN);
    set names end = eoN;
    cnt = 0;
    _RC_ = H.add();
  end;

  do until(eof);
    set have end = eof;

    _RC_ = 1;
    do until(name = "");
      name = scan(string, _RC_, "-", "S");
      if H.find(key:name) = 0 then
        do;
          nextWord = scan(string, _RC_+1, "-", "S");
          if not(prxmatch('/^(AB|A\d|B\d|\d)/', nextWord)) then 
            do;
              cnt + 1;
              H.replace();
            end;
        end;
      _RC_ + 1;
    end;

  end;

  H.output(dataset:"want");
run;

proc print data = want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 Aug 2020 10:22:46 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-08-26T10:22:46Z</dc:date>
    <item>
      <title>count occurrences excluding few conditions (regular expressions)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-occurrences-excluding-few-conditions-regular-expressions/m-p/679380#M205137</link>
      <description>&lt;P&gt;Hey guys,&lt;/P&gt;&lt;P&gt;I have two tables - one with list of names and another one with some strings like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tom has a cat&lt;BR /&gt;John has a dog&lt;BR /&gt;Anna has a parrot&lt;BR /&gt;Frank has a dog&lt;BR /&gt;Anna has a cow&lt;BR /&gt;John has a tiger&lt;BR /&gt;Tom AB has a tiger&lt;BR /&gt;Tom A41 has a lion&lt;BR /&gt;Tom B12 has a dolphin&lt;BR /&gt;Tom 2 has a horse&lt;BR /&gt;Tom-32 has a dog&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And I want to count occurrence of each name (that exists in the table with list of names) in table with strings, however it shouldn't count name "Tom" when there is a string "AB"&lt;BR /&gt;or any number or "Ax" or "Bx" (where "x" is a number) after the name&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the result should be:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Tom 1&lt;BR /&gt;John 2&lt;BR /&gt;Frank 1&lt;BR /&gt;Anna 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know how to count number of names (using for example findw) but I have no idea how to exclude from counting name "Tom" with conditions I've written above.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Any guidance will be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Aug 2020 08:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-occurrences-excluding-few-conditions-regular-expressions/m-p/679380#M205137</guid>
      <dc:creator>2s7</dc:creator>
      <dc:date>2020-08-26T08:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: count occurrences excluding few conditions (regular expressions)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-occurrences-excluding-few-conditions-regular-expressions/m-p/679393#M205144</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;maybe this will help you, I added some "special" cases for testing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input string $ 50.;
cards4;
Tom has a cat
John has a dog
Anna has a parrot
Frank has a dog
Anna has a cow
John has a tiger
Tom AB has a tiger
Tom A41 has a lion
Tom B12 has a dolphin
Tom 2 has a horse
Tom-32 has a dog
Bob has a parrot and Cedrick has a parrot
Alex has a cat and Alex has a dog and Alex AB13 has a pig
;;;;
run;

data names;
input name $ 12.;
cards;
Tom
John
Frank
Anna
Bob
Cedrick
Alex
;
run;

data _NULL_;
  call symputX("namesNo", nobs);
  stop;
  set names nobs=nobs;
run;


data _null_;
  declare hash H();
  H.defineKey('name');
  H.defineData('name', 'cnt');
  H.defineDone();

  do until(eoN);
    set names end = eoN;
    cnt = 0;
    _RC_ = H.add();
  end;

  do until(eof);
    set have end = eof;

    _RC_ = 1;
    do until(name = "");
      name = scan(string, _RC_, "-", "S");
      if H.find(key:name) = 0 then
        do;
          nextWord = scan(string, _RC_+1, "-", "S");
          if not(prxmatch('/^(AB|A\d|B\d|\d)/', nextWord)) then 
            do;
              cnt + 1;
              H.replace();
            end;
        end;
      _RC_ + 1;
    end;

  end;

  H.output(dataset:"want");
run;

proc print data = want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Aug 2020 10:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-occurrences-excluding-few-conditions-regular-expressions/m-p/679393#M205144</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-08-26T10:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: count occurrences excluding few conditions (regular expressions)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-occurrences-excluding-few-conditions-regular-expressions/m-p/682508#M206591</link>
      <description>&lt;P&gt;Here is a sample.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data namelist;
	input name $ 10.;
	datalines;
Tom
John
Frank
Anna
;
run;

data have;
	input strings $ 40.;
	datalines;
Tom has a cat
John has a dog
Anna has a parrot
Frank has a dog
Anna has a cow
John has a tiger
Tom AB has a tiger
Tom A41 has a lion
Tom B12 has a dolphin
Tom 2 has a horse
Tom-32 has a dog
;
run;

data _NULL_;
	length id 8 name $10 strings $40 count 8;
	call missing(name, strings, count);
	declare hash h(suminc:"count", multidata:"y", ordered:"y");
	rc=h.definekey("id");
	rc=h.definedata("id", "strings", "name", "count");
	rc=h.definedone();
	declare hash nl(suminc:"count", multidata:"y", ordered:"y");
	rc=nl.definekey("name");
	rc=nl.definedata("name", "id");
	rc=nl.definedone();

	do until(aaa);
		set namelist end=aaa;
		id+1;
		nl.add(key:name, data:name, data:id);
	end;

	do until(eof);
		set have end=eof;
		count=1;
		name=scan(strings, 1, " ");
		rc=nl.find();

		if rc=0 and prxmatch("s/(Tom)?(A|B)\d+|(AB)-?|( \d)/match/i", strings)=0 then
			do;
				h.ref();
				h.sum(sum:count);
				h.replace();
			end;
	end;

	if eof then
		h.output(dataset:"work.want(drop=strings id)");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Sep 2020 05:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-occurrences-excluding-few-conditions-regular-expressions/m-p/682508#M206591</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-09-09T05:14:28Z</dc:date>
    </item>
  </channel>
</rss>

