<?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: Filter a dataset by composite key to leave only matched records with hash object in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910296#M359000</link>
    <description>&lt;P&gt;So I see separate issues in this code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First you are not defining the variables that only appear in TABLE1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect you meant to do something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set Table1(keep=MOV_CENTRO_ALTA MOV_CUENTA MOV_NUMER_MOV);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second you are only looking in the hash for a single value.&amp;nbsp; The record where&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MOV_CENTRO_ALTA='DES_CENTRO_ALTA'
MOV_CUENTA='DES_CUENTA' 
MOV_NUMER_MOV='DES_NUMER_MOV'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that is what you wanted to do then just use a WHERE clause when searching TABLE2.&lt;/P&gt;
&lt;P&gt;I suspect that wanted to use the values of the variables from TABLE2 to search the hash. So remove the quotes around the variable names in the FIND() call.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;h.find(key: DES_CENTRO_ALTA ,key: DES_CUENTA ,key: DES_NUMER_MOV)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Personally I find it clearer to treat the return codes as boolean values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if not h.find(....) then ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But since it is confusing that a TRUE value indicates a FAILURE of the FIND() I can see why you might want to test if the return code is zero instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you want to compare the result to a specific value then it will be easier to read (scan) if you code the value first since then I don't have to hunt down the closing ) for the FIND() method call to see if you are testing records where the match worked or where it did not work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0=h.find(....) then ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you seem to be just using this to SUBSET the observations from TABLE2 there is no need to store so much data in the hash.&amp;nbsp; Although I believe it actually does save space to store something in the hash.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you probably will want to DROP the variables created from TABLE1.&amp;nbsp; In fact why not just rename them to match. Then there is no need to define them with an extra SET or to drop them .&amp;nbsp; Plus the coding is easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merged;
  if _n_ = 1 then do;
    declare hash h(dataset: 'Table1(keep=MOV_CENTRO_ALTA MOV_CUENTA MOV_NUMER_MOV
    rename=(MOV_CENTRO_ALTA=DES_CENTRO_ALTA MOV_CUENTA=DES_CUENTA MOV_NUMER_MOV=DES_NUMER_MOV
    )');
    h.defineKey('DES_CENTRO_ALTA', 'DES_CUENTA', 'DES_NUMER_MOV');
    h.DefineData('DES_NUMER_MOV');
    h.defineDone();
  end;
  set Table2;
  if h.find() then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jan 2024 17:18:48 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-01-03T17:18:48Z</dc:date>
    <item>
      <title>Filter a dataset by composite key to leave only matched records with hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910240#M358994</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I want to leave records&amp;nbsp; from&amp;nbsp;Table2 which match composite key from Table1 loaded to hash object but get an error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data merged;
	if 0 then set Table2;
	if _n_ = 1 then do;
		declare hash h(dataset: 'Table1(keep=MOV_CENTRO_ALTA MOV_CUENTA MOV_NUMER_MOV)');
		h.defineKey('MOV_CENTRO_ALTA', 'MOV_CUENTA', 'MOV_NUMER_MOV');
		h.DefineData('MOV_CENTRO_ALTA', 'MOV_CUENTA', 'MOV_NUMER_MOV');
		h.defineDone();
	end;

	set Table2;

	if h.find(key: 'DES_CENTRO_ALTA', key: 'DES_CUENTA', key: 'DES_NUMER_MOV')=0 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;ERROR: Undeclared key symbol MOV_CENTRO_ALTA for hash object at line 34 column 3.&lt;BR /&gt;ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2024 13:43:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910240#M358994</guid>
      <dc:creator>JanKwiatkowski</dc:creator>
      <dc:date>2024-01-03T13:43:04Z</dc:date>
    </item>
    <item>
      <title>Re: Filter a dataset by composite key to leave only matched records with hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910243#M358995</link>
      <description>&lt;P&gt;I think you have a typo.&amp;nbsp; You probably want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;if 0 then set Table&lt;U&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/U&gt;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;That statement is putting all of the columns from Table1 into your PDV during compilation&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2024 13:52:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910243#M358995</guid>
      <dc:creator>Al14</dc:creator>
      <dc:date>2024-01-03T13:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Filter a dataset by composite key to leave only matched records with hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910251#M358998</link>
      <description>&lt;P&gt;The expression following the KEY: keyword must result in a&amp;nbsp;&lt;EM&gt;value&lt;/EM&gt;, not a&amp;nbsp;&lt;EM&gt;variable name&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;Therefore&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if h.find(key: DES_CENTRO_ALTA, key: DES_CUENTA, key: DES_NUMER_MOV)=0&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jan 2024 14:18:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910251#M358998</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-01-03T14:18:59Z</dc:date>
    </item>
    <item>
      <title>Re: Filter a dataset by composite key to leave only matched records with hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910296#M359000</link>
      <description>&lt;P&gt;So I see separate issues in this code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First you are not defining the variables that only appear in TABLE1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suspect you meant to do something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set Table1(keep=MOV_CENTRO_ALTA MOV_CUENTA MOV_NUMER_MOV);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second you are only looking in the hash for a single value.&amp;nbsp; The record where&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;MOV_CENTRO_ALTA='DES_CENTRO_ALTA'
MOV_CUENTA='DES_CUENTA' 
MOV_NUMER_MOV='DES_NUMER_MOV'&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that is what you wanted to do then just use a WHERE clause when searching TABLE2.&lt;/P&gt;
&lt;P&gt;I suspect that wanted to use the values of the variables from TABLE2 to search the hash. So remove the quotes around the variable names in the FIND() call.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;h.find(key: DES_CENTRO_ALTA ,key: DES_CUENTA ,key: DES_NUMER_MOV)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Personally I find it clearer to treat the return codes as boolean values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if not h.find(....) then ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But since it is confusing that a TRUE value indicates a FAILURE of the FIND() I can see why you might want to test if the return code is zero instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you want to compare the result to a specific value then it will be easier to read (scan) if you code the value first since then I don't have to hunt down the closing ) for the FIND() method call to see if you are testing records where the match worked or where it did not work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0=h.find(....) then ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you seem to be just using this to SUBSET the observations from TABLE2 there is no need to store so much data in the hash.&amp;nbsp; Although I believe it actually does save space to store something in the hash.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you probably will want to DROP the variables created from TABLE1.&amp;nbsp; In fact why not just rename them to match. Then there is no need to define them with an extra SET or to drop them .&amp;nbsp; Plus the coding is easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merged;
  if _n_ = 1 then do;
    declare hash h(dataset: 'Table1(keep=MOV_CENTRO_ALTA MOV_CUENTA MOV_NUMER_MOV
    rename=(MOV_CENTRO_ALTA=DES_CENTRO_ALTA MOV_CUENTA=DES_CUENTA MOV_NUMER_MOV=DES_NUMER_MOV
    )');
    h.defineKey('DES_CENTRO_ALTA', 'DES_CUENTA', 'DES_NUMER_MOV');
    h.DefineData('DES_NUMER_MOV');
    h.defineDone();
  end;
  set Table2;
  if h.find() then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2024 17:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-a-dataset-by-composite-key-to-leave-only-matched-records/m-p/910296#M359000</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-01-03T17:18:48Z</dc:date>
    </item>
  </channel>
</rss>

