<?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 Hash Merge Using Explicit Key Fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551566#M153255</link>
    <description>&lt;P&gt;I am trying to hash merge using the key: logic to define which field in the base table I want to use to join. The first two keys are named the exact same in the base and join tables, but the third field differs between the two tables. However, I keep getting errors! Here is my base code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_table ;
set work.test;
length Indicator $5;
if 0 then set toodle ;


if _n_=1 then do;

		dcl hash one (dataset:'toodle');
			one.definekey ('Product','Status','Indicator_1');
/*			one.definekey ('Product','Status');*/
			one.definedata('core_1');
			one.definedone();


rc_1 = one.find(key:'Product'n,key:Status,key:'Indicator');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have tested using only the commented out key definition - this works. However, when I add the "Indicator_1" field, I get the following errors:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Argument length greater than length of key variable Indicator_1 at line 47 column 11.&lt;BR /&gt;ERROR: Keys and data must be specified in the same order and have the same types as given in DefineKey/DefineData at line 47 column&lt;BR /&gt;11.&lt;BR /&gt;ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, I set the Indicator field to the length of the Indicator_1 field in the other table using the length statement before the null set statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I remove the quotes around the "Indicator" field in the find statement, I get the following errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Type mismatch for key variable Indicator at line 48 column 11.&lt;BR /&gt;ERROR: Keys and data must be specified in the same order and have the same types as given in DefineKey/DefineData at line 48 column&lt;BR /&gt;11.&lt;BR /&gt;ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Indicator and Indicator_1 fields are both character fields. Why isn't the merge working?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Apr 2019 21:59:35 GMT</pubDate>
    <dc:creator>theponcer</dc:creator>
    <dc:date>2019-04-16T21:59:35Z</dc:date>
    <item>
      <title>Hash Merge Using Explicit Key Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551566#M153255</link>
      <description>&lt;P&gt;I am trying to hash merge using the key: logic to define which field in the base table I want to use to join. The first two keys are named the exact same in the base and join tables, but the third field differs between the two tables. However, I keep getting errors! Here is my base code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_table ;
set work.test;
length Indicator $5;
if 0 then set toodle ;


if _n_=1 then do;

		dcl hash one (dataset:'toodle');
			one.definekey ('Product','Status','Indicator_1');
/*			one.definekey ('Product','Status');*/
			one.definedata('core_1');
			one.definedone();


rc_1 = one.find(key:'Product'n,key:Status,key:'Indicator');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have tested using only the commented out key definition - this works. However, when I add the "Indicator_1" field, I get the following errors:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Argument length greater than length of key variable Indicator_1 at line 47 column 11.&lt;BR /&gt;ERROR: Keys and data must be specified in the same order and have the same types as given in DefineKey/DefineData at line 47 column&lt;BR /&gt;11.&lt;BR /&gt;ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, I set the Indicator field to the length of the Indicator_1 field in the other table using the length statement before the null set statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I remove the quotes around the "Indicator" field in the find statement, I get the following errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Type mismatch for key variable Indicator at line 48 column 11.&lt;BR /&gt;ERROR: Keys and data must be specified in the same order and have the same types as given in DefineKey/DefineData at line 48 column&lt;BR /&gt;11.&lt;BR /&gt;ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Indicator and Indicator_1 fields are both character fields. Why isn't the merge working?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 21:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551566#M153255</guid>
      <dc:creator>theponcer</dc:creator>
      <dc:date>2019-04-16T21:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Merge Using Explicit Key Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551571#M153259</link>
      <description>&lt;P&gt;First off, you are missing END; in the IF THEN DO block&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; _n_&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; do&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

		dcl hash one &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;dataset:&lt;SPAN class="token string"&gt;'toodle'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
			one&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definekey &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'Product'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'Status'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'Indicator_1'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token comment"&gt;/*			one.definekey ('Product','Status');*/&lt;/SPAN&gt;
			one&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definedata&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'core_1'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
			one&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definedone&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;BR /&gt;END;&lt;/PRE&gt;
&lt;P&gt;Is 'Indicator' here&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;rc_1 &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; one&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;find&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key:&lt;SPAN class="token string"&gt;'Product'&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;n&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;key:Status&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;key:&lt;SPAN class="token string"&gt;'Indicator'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;a variable or constant???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If variable, there shouldn't be quotes.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 22:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551571#M153259</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-16T22:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Merge Using Explicit Key Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551729#M153322</link>
      <description>&lt;P&gt;It is a field, but even when I remove the quotes (like I mentioned above), I still get an error that says the types are not the same. Both Indicator fields are Character fields.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 13:57:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551729#M153322</guid>
      <dc:creator>theponcer</dc:creator>
      <dc:date>2019-04-17T13:57:58Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Merge Using Explicit Key Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551733#M153324</link>
      <description>&lt;P&gt;Would it be possible to post a sample of your data(a mock closely resembling the exact) and what your objective?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 14:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/551733#M153324</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-17T14:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Merge Using Explicit Key Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/552026#M153448</link>
      <description>&lt;P&gt;Unlike column definitions in "usual" data step code, hash objects don't create (default) column attributes.&amp;nbsp; You must define the column attributes in the data step prior to defining the hash object.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Often this is done using the construct:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set my_lookup_dataset;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;which sets the attributes but does not read any data.&amp;nbsp; Otherwise you can use length or attrib statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Next, the find() function uses the data values for the key variables that are currently in the PDV to lookup the corresponding keys in the hash object.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have never used a dynamic key variable, i.e.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;one.find(key:'Product'n,key:Status,key:'Indicator');&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In this code, either the 2nd key column would be the value of the Status variable (unlikely, if that's even possible?&amp;nbsp; I'm too lazy to lookup the syntax right now), or else it's a typo, and Status should be 'Status'.&amp;nbsp; I suspect it's a typo, unless the value of Status really is the name of a column (again, if that's possible?)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But there's nothing to keep you from "fiddling" with the values in the PDV at run time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_table ;
   set work.test;
   if 0 then set toodle ;
   length Indicator $5;  
   * if Indicator is not in work.test then what is setting its value? Otherwise it would always be missing ;
   * and if Indicator is in work.test then why the length statement? ;

   if _n_=1 then do;
      dcl hash one (dataset:'toodle');
      one.definekey ('Product','Status','Indicator');
      one.definedata('core_1');
      one.definedone();
   end;

   rc_1 = one.find();

   if (rc_1 ne 0) then do;
      Indicator = Indicator_1;
      rc_1 = one.find();
   end;
   if (rc_1 ne 0) then do;
      Indicator = Indicator_2;
      rc_1 = one.find();
   end;

   /* etc */
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is Indicator_1 in work.test or work.toddle?&amp;nbsp; If in both, do they have the same attributes, i.e. length and type?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if these macros would help, but you can at least look at the use cases in the header to perhaps get some ideas:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/hash_define.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/hash_define.sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/scottbass/SAS/blob/master/Macro/hash_lookup.sas" target="_blank"&gt;https://github.com/scottbass/SAS/blob/master/Macro/hash_lookup.sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH...&lt;/P&gt;</description>
      <pubDate>Thu, 18 Apr 2019 08:26:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Merge-Using-Explicit-Key-Fields/m-p/552026#M153448</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-04-18T08:26:20Z</dc:date>
    </item>
  </channel>
</rss>

