<?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: Counting the number of specific rows using Hash in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540432#M149089</link>
    <description>&lt;P&gt;First, thank you for provide a complete program, with sample data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are two problems - one having to do with hash object method use, and the other with character variable length:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Character variable length.&amp;nbsp; Because the first assignment of compressed_a='WYETH', that variable was assigned a length of $5, thereby truncating values of compressed_a in other observations.&amp;nbsp; Same with firmname (length=$9) in data set patents.&amp;nbsp; So assign length by means of a LENGTH statement in your sample.&lt;BR /&gt;&lt;BR /&gt;But probably your actual data does not suffer from this issue.&amp;nbsp; In this case, you may have inadvertently added to the problem by making the sample data sets via assignment statements instead of INPUT.&lt;/LI&gt;
&lt;LI&gt;hash object usage.&amp;nbsp; By specifying&amp;nbsp;&amp;nbsp;&amp;nbsp; h.find(key:'compressed_a') you were looking for a data item&lt;STRIKE&gt;time&lt;/STRIKE&gt; with a key VALUE of 'compressed_a'.&amp;nbsp; You should specify&amp;nbsp;&amp;nbsp; h.find(key:compressed_a), which takes the value in variable compressed_a.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data deal;
  length compressed_a  compressed_b $24;
  compressed_a = 'WYETH';              compressed_b = 'FORTDODGEANIMALHEALTH'; year = 1945;output;
  compressed_a = 'ELILILLYANDCOMPANY'; compressed_b = 'HYBRITECHINCORPORATED'; year = 1985;output;
  compressed_a = 'GENENTECH';          compressed_b = 'ABBOTTLABORATORIES';    year = 1990;output;
run;
data patent;
  length firmname $24;
  firmname = 'GENENTECH';          pnyear = 1985; IPC = 'B05C'; output;
  firmname = 'ELILILLYANDCOMPANY'; pnyear = 1983; IPC = 'A01N'; output;
run;

data size_merge5;
  if _n_=1 then do;
	if 0 then set patent;
	dcl hash H (dataset:'patent',multidata:'y') ;
	h.definekey  ("firmname") ;
	h.definedata ("pnyear","ipc") ;
	h.definedone () ;
  end;

  set deal;  

  IPC_A=0;
  do rc=h.find(key:compressed_a) by 0 while(rc=0);		
	if pnyear &amp;lt; year then IPC_A =sum(IPC_A,1);	
	rc=h.find_next();
  end;

  IPC_B=0;
  do rc=h.find(key:compressed_b) by 0 while(rc=0);		
	if pnyear &amp;lt; year then IPC_B =sum(IPC_B,1);	
	rc=h.find_next();
  end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Mar 2019 13:33:58 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2019-03-05T13:33:58Z</dc:date>
    <item>
      <title>Counting the number of specific rows using Hash</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540429#M149086</link>
      <description>&lt;P&gt;Hi, I have two datasets&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data deal;
  compressed_a = 'WYETH'; compressed_b = 'FORTDODGEANIMALHEALTH'; year = 1945;output;
  compressed_a = 'ELILILLYANDCOMPANY'; compressed_b = 'HYBRITECHINCORPORATED'; year = 1985;output;
  compressed_a = 'GENENTECH'; compressed_b = 'ABBOTTLABORATORIES'; year = 1990;output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data patent;
  firmname = 'GENENTECH'; pnyear = 1985; IPC = 'B05C'; output;
  firmname = 'ELILILLYANDCOMPANY'; pnyear = 1983; IPC = 'A01N'; output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;table deal has the M&amp;amp;A data of two firms, including firm names (compressed_a, compressed_b) and the year of the deal was conducted.&lt;/P&gt;&lt;P&gt;table patent has the list of ipc codes of a firm has acquired.&lt;/P&gt;&lt;P&gt;my main question is this: How many number of patents does a company has, at a focal year (deal year).&lt;/P&gt;&lt;P&gt;expected output(ipc_a indicating number of patents, accumulative)&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;compressed_a&lt;/TD&gt;&lt;TD&gt;compressed_d&lt;/TD&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;TD&gt;ipc_a&lt;/TD&gt;&lt;TD&gt;ipc_b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WYETH&lt;/TD&gt;&lt;TD&gt;FORTDODGEANIMALHEALTH&lt;/TD&gt;&lt;TD&gt;1945&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ELILILLYANDCOMPANY&lt;/TD&gt;&lt;TD&gt;HYBRITECHINCORPORATED&lt;/TD&gt;&lt;TD&gt;1985&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;GENENTECH&lt;/TD&gt;&lt;TD&gt;ABBOTTLABORATORIES&lt;/TD&gt;&lt;TD&gt;1990&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thus i have constructed a code as below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data size_merge5;
  if _n_=1 then do;
	if 0 then set patent;
	dcl hash H (dataset:'patent',multidata:'y') ;
	h.definekey  ("firmname") ;
	h.definedata ("pnyear","ipc") ;
	h.definedone () ;
  end;

  set deal;  

  IPC_A=0;
  do rc=h.find(key:'compressed_a') by 0 while(rc=0);		
	if pnyear &amp;lt; year then IPC_A =sum(IPC_A,1);	
	rc=h.find_next();
  end;

  IPC_B=0;
  do rc=h.find(key:'compressed_b') by 0 while(rc=0);		
	if pnyear &amp;lt; year then IPC_B =sum(IPC_B,1);	
	rc=h.find_next();
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but unfortunately this code gives me all zeros for ipc_a and ipc_b.&lt;/P&gt;&lt;P&gt;cannot find what's wrong. please help.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 13:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540429#M149086</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-03-05T13:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of specific rows using Hash</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540430#M149087</link>
      <description>&lt;P&gt;Why not just merge on based on compressed_a=firmname?&amp;nbsp; Thats all you seem to want from that snippet?&amp;nbsp; If it needs to match both, then merge twice (same datastep) and just rename the variable?&lt;/P&gt;
&lt;PRE&gt;/* Note both assumes sorted */
data want;
  merge deal
             patent (rename=(firmname=compressed_a ipc=ipc1));
  by compressed_a;
run;
data want;
  merge want
             patent (rename=(firmname=compress_b ipc=ipc2));
  by compressed_b;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Mar 2019 13:19:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540430#M149087</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-05T13:19:23Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of specific rows using Hash</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540432#M149089</link>
      <description>&lt;P&gt;First, thank you for provide a complete program, with sample data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are two problems - one having to do with hash object method use, and the other with character variable length:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Character variable length.&amp;nbsp; Because the first assignment of compressed_a='WYETH', that variable was assigned a length of $5, thereby truncating values of compressed_a in other observations.&amp;nbsp; Same with firmname (length=$9) in data set patents.&amp;nbsp; So assign length by means of a LENGTH statement in your sample.&lt;BR /&gt;&lt;BR /&gt;But probably your actual data does not suffer from this issue.&amp;nbsp; In this case, you may have inadvertently added to the problem by making the sample data sets via assignment statements instead of INPUT.&lt;/LI&gt;
&lt;LI&gt;hash object usage.&amp;nbsp; By specifying&amp;nbsp;&amp;nbsp;&amp;nbsp; h.find(key:'compressed_a') you were looking for a data item&lt;STRIKE&gt;time&lt;/STRIKE&gt; with a key VALUE of 'compressed_a'.&amp;nbsp; You should specify&amp;nbsp;&amp;nbsp; h.find(key:compressed_a), which takes the value in variable compressed_a.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data deal;
  length compressed_a  compressed_b $24;
  compressed_a = 'WYETH';              compressed_b = 'FORTDODGEANIMALHEALTH'; year = 1945;output;
  compressed_a = 'ELILILLYANDCOMPANY'; compressed_b = 'HYBRITECHINCORPORATED'; year = 1985;output;
  compressed_a = 'GENENTECH';          compressed_b = 'ABBOTTLABORATORIES';    year = 1990;output;
run;
data patent;
  length firmname $24;
  firmname = 'GENENTECH';          pnyear = 1985; IPC = 'B05C'; output;
  firmname = 'ELILILLYANDCOMPANY'; pnyear = 1983; IPC = 'A01N'; output;
run;

data size_merge5;
  if _n_=1 then do;
	if 0 then set patent;
	dcl hash H (dataset:'patent',multidata:'y') ;
	h.definekey  ("firmname") ;
	h.definedata ("pnyear","ipc") ;
	h.definedone () ;
  end;

  set deal;  

  IPC_A=0;
  do rc=h.find(key:compressed_a) by 0 while(rc=0);		
	if pnyear &amp;lt; year then IPC_A =sum(IPC_A,1);	
	rc=h.find_next();
  end;

  IPC_B=0;
  do rc=h.find(key:compressed_b) by 0 while(rc=0);		
	if pnyear &amp;lt; year then IPC_B =sum(IPC_B,1);	
	rc=h.find_next();
  end;

run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 13:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540432#M149089</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-03-05T13:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of specific rows using Hash</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540459#M149098</link>
      <description>mkeintz, my savior!&lt;BR /&gt;you're right i wasn't suffering from the issue #1 with my actual data but was suffering because of issue #2!!! just by removing single quotes, it works now. THANKS SO MUCH</description>
      <pubDate>Tue, 05 Mar 2019 14:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540459#M149098</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-03-05T14:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of specific rows using Hash</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540460#M149099</link>
      <description>i am trying to count the accumulative count of IPC codes</description>
      <pubDate>Tue, 05 Mar 2019 14:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-specific-rows-using-Hash/m-p/540460#M149099</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-03-05T14:56:59Z</dc:date>
    </item>
  </channel>
</rss>

