<?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: Joining multiple tables with different keys using hash object in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745681#M233793</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/109262"&gt;@subhani4&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First make the cities table comma delimited. I believe your objective is to join the datasets to obtain an output as you desired.&lt;BR /&gt;Second it is easy to use proc sql to make the join. Please see the code below .&lt;/P&gt;
&lt;P&gt;Information from zipcode and cities table is sufficient to produce the desired output.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
input Name$ Sex$ Age Height Weight;
infile datalines dlm=' ' dsd;
datalines;
Alfred M 14 69.0 112.5
Alice F 13 56.5 84.0
Barbara F 13 65.3 98.0
Carol F 14 62.8 102.5
Henry M 14 63.5 102.5
;

data zipcodes;
input name $ zipcode;
infile datalines dlm=' ' dsd;
datalines;
Alfred 91940
Alice 75008
Barbara 92330
Carol 94150
Henry 93140
;

data cities;
input zipcode City :$30. Department :$30.;
infile datalines dlm=',' dsd;
datalines;
75008, Paris 8è ,Paris
91940, Les Ulis, Essonne
92330,Sceaux, Hauts-de-Seine
93140, Bondy , Seine-Saint-Denis
94150, Rungis, Val-de-Marne
;
proc sql;&lt;BR /&gt;create table want as
select z.name, z.zipcode, ci.City Format $30. length=30 , Ci.Department Format $30. length=30
from zipcodes z
Left join Cities ci ON z.zipcode=ci.zipcode&lt;BR /&gt;order by Name;
quit;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be as follows&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="want.PNG" style="width: 341px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60012i43C5F36751E43C38/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.PNG" alt="want.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Jun 2021 03:47:41 GMT</pubDate>
    <dc:creator>Sajid01</dc:creator>
    <dc:date>2021-06-04T03:47:41Z</dc:date>
    <item>
      <title>Joining multiple tables with different keys using hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745659#M233785</link>
      <description>&lt;P&gt;i have data as below&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data class;&lt;BR /&gt;input Name$ Sex$ Age Height Weight;&lt;BR /&gt;infile datalines dlm=' ' dsd;&lt;BR /&gt;datalines;&lt;BR /&gt;Alfred M 14 69.0 112.5&lt;BR /&gt;Alice F 13 56.5 84.0&lt;BR /&gt;Barbara F 13 65.3 98.0&lt;BR /&gt;Carol F 14 62.8 102.5&lt;BR /&gt;Henry M 14 63.5 102.5&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data zipcodes;&lt;BR /&gt;input name $ zipcode;&lt;BR /&gt;infile datalines dlm=' ' dsd;&lt;BR /&gt;datalines;&lt;BR /&gt;Alfred 91940&lt;BR /&gt;Alice 75008&lt;BR /&gt;Barbara 92330&lt;BR /&gt;Carol 94150&lt;BR /&gt;Henry 93140&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data cities;&lt;BR /&gt;input zipcode City :$30. Department :$30.;&lt;BR /&gt;infile datalines dlm=' ' dsd;&lt;BR /&gt;datalines;&lt;BR /&gt;75008 Paris 8è Paris&lt;BR /&gt;91940 Les Ulis Essonne&lt;BR /&gt;92330 Sceaux Hauts-de-Seine&lt;BR /&gt;93140 Bondy Seine-Saint-Denis&lt;BR /&gt;94150 Rungis Val-de-Marne&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;i&amp;nbsp; wand the output as&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Name&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;ZipCode&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;City&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Department&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Alfred&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;91940&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Les Ulis&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Essonne&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Alice&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;75008&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Paris 8è&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Paris&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Barbara&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;92330&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Sceaux&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Hauts-de-Seine&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Carol&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;94150&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Rungis&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Val-de-Marne&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Henry&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;93140&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Bondy&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Seine-Saint-Denis&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have coded as below but not able to make it happen:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data need(keep=name zipcode city department);&lt;/P&gt;&lt;P&gt;if 0 then set zipcodes cities;&lt;BR /&gt;if _n_=1 then do;&lt;BR /&gt;dcl hash z(dataset:'zipcodes');&lt;BR /&gt;z.definekey('name','zipcode');&lt;BR /&gt;z.definedata(all:'Y');&lt;BR /&gt;z.definedone();&lt;BR /&gt;dcl hash c(dataset:'cities');&lt;BR /&gt;c.definekey('zipcode');&lt;BR /&gt;c.definedata(all:'Y');&lt;BR /&gt;c.definedone();&lt;BR /&gt;end;&lt;BR /&gt;do until(eof);&lt;BR /&gt;set class end=eof;&lt;BR /&gt;if z.find(key:name,key:zipcode)=0 and c.find(key:zipcode)=0;&lt;BR /&gt;end;&lt;BR /&gt;stop;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 23:08:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745659#M233785</guid>
      <dc:creator>subhani4</dc:creator>
      <dc:date>2021-06-03T23:08:14Z</dc:date>
    </item>
    <item>
      <title>Re: Joining multiple tables with different keys using hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745677#M233791</link>
      <description>What does not able to make it happen mean? If you have errors please include them in the post.</description>
      <pubDate>Fri, 04 Jun 2021 03:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745677#M233791</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-04T03:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: Joining multiple tables with different keys using hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745681#M233793</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/109262"&gt;@subhani4&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First make the cities table comma delimited. I believe your objective is to join the datasets to obtain an output as you desired.&lt;BR /&gt;Second it is easy to use proc sql to make the join. Please see the code below .&lt;/P&gt;
&lt;P&gt;Information from zipcode and cities table is sufficient to produce the desired output.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
input Name$ Sex$ Age Height Weight;
infile datalines dlm=' ' dsd;
datalines;
Alfred M 14 69.0 112.5
Alice F 13 56.5 84.0
Barbara F 13 65.3 98.0
Carol F 14 62.8 102.5
Henry M 14 63.5 102.5
;

data zipcodes;
input name $ zipcode;
infile datalines dlm=' ' dsd;
datalines;
Alfred 91940
Alice 75008
Barbara 92330
Carol 94150
Henry 93140
;

data cities;
input zipcode City :$30. Department :$30.;
infile datalines dlm=',' dsd;
datalines;
75008, Paris 8è ,Paris
91940, Les Ulis, Essonne
92330,Sceaux, Hauts-de-Seine
93140, Bondy , Seine-Saint-Denis
94150, Rungis, Val-de-Marne
;
proc sql;&lt;BR /&gt;create table want as
select z.name, z.zipcode, ci.City Format $30. length=30 , Ci.Department Format $30. length=30
from zipcodes z
Left join Cities ci ON z.zipcode=ci.zipcode&lt;BR /&gt;order by Name;
quit;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output will be as follows&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="want.PNG" style="width: 341px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60012i43C5F36751E43C38/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.PNG" alt="want.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 03:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745681#M233793</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-04T03:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: Joining multiple tables with different keys using hash object</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745686#M233797</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do until(eof);
set class end=eof;
if z.find(key:name,key:zipcode)=0 and c.find(key:zipcode)=0;
end;
stop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since there is no OUTPUT statement in your loop, and the STOP statement terminates the data step before the implicit output is done, nothing is written to the dataset.&lt;/P&gt;
&lt;P&gt;Use the "natural loop" of the data step instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need(keep=name zipcode city department);
set class;
if 0 then set zipcodes cities;
if _n_=1 then do;
  length zipcode 8 city department $30; 
  dcl hash z(dataset:'zipcodes');
  z.definekey('name');
  z.definedata('zipcode');
  z.definedone();
  dcl hash c(dataset:'cities');
  c.definekey('zipcode');
  c.definedata('city','department');
  c.definedone();
end;
if z.find()=0;
if c.find()=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I adapted the hash definitions, as dataset class does not have a zipcode variable. Therefore it is also necessary to split the subsetting IFs, to make sure that the second FIND() is executed after the first one that retrieves the zipcode.&lt;/P&gt;
&lt;P&gt;The code is not tested in any way, as I am posting from my tablet.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jun 2021 05:07:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-multiple-tables-with-different-keys-using-hash-object/m-p/745686#M233797</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-04T05:07:01Z</dc:date>
    </item>
  </channel>
</rss>

