<?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: Trouble formatting merge keys in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440766#M282552</link>
    <description>&lt;P&gt;Check the lengths on your variables to see if they’re the same and use STRIP to remove any leading/trailing spaces.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If if the other variable had a length of $200, perhaps the following may work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Length newID $6.;
NewID = ID;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You’ll have to make the names match but hopefully that gives you the idea.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 28 Feb 2018 04:06:12 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-02-28T04:06:12Z</dc:date>
    <item>
      <title>Trouble formatting merge keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440752#M282550</link>
      <description>&lt;P&gt;I'm using SAS 9.3 and trying to merge two datasets. In dataset1, my merge key variable called "id" is originally formatted as a character&amp;nbsp;$200.&amp;nbsp; In dataset2, the merge key variable also called "id" is formatted as a character&amp;nbsp;$6. The values of id are actually numbers (for example, 001004 or 134875).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried the following code that reformats the id in dataset1 and tries to perform the merge:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset1; set dataset1;
newid=input(id,BEST12.);
newid2=put(newid,z6.);
run;

proc sql;
create table combo as
select a.*, b.var1, b.var2
from dataset2 as a left join dataset1 as b
on a.id = b.newid2;
quit;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The above code appears to work at first, but fails to fully merge all the records in dataset1. The merge works correctly for low values of id, but fails when there are no longer any leading zeros. As a specific example, the merge works for id=099999 but not for id=100000. How can I format my id variable to make sure all records are appropriately merged?&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 03:22:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440752#M282550</guid>
      <dc:creator>Kristen1</dc:creator>
      <dc:date>2018-02-28T03:22:18Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble formatting merge keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440765#M282551</link>
      <description>&lt;P&gt;Check the lengths on your variables to see if they’re the same and use STRIP to remove any leading/trailing spaces.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If if the other variable had a length of $200, perhaps the following may work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Length newID $6.;
NewID = ID;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You’ll have to make the names match but hopefully that gives you the idea.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 04:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440765#M282551</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-28T04:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble formatting merge keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440766#M282552</link>
      <description>&lt;P&gt;Check the lengths on your variables to see if they’re the same and use STRIP to remove any leading/trailing spaces.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If if the other variable had a length of $200, perhaps the following may work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Length newID $6.;
NewID = ID;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You’ll have to make the names match but hopefully that gives you the idea.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 04:06:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440766#M282552</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-28T04:06:12Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble formatting merge keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440767#M282553</link>
      <description>&lt;P&gt;If both ID's are character then you don't need a transformation. Removing leading blanks should be enough:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table combo as
select a.*, b.var1, b.var2
from dataset2 as a left join dataset1 as b
on left(a.id) = left(b.id);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Feb 2018 04:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440767#M282553</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-02-28T04:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Trouble formatting merge keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440863#M282554</link>
      <description>&lt;P&gt;Why not convert the two variables to numeric in the SQL call?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table combo as
  select 
    dataset2.*, 
    dataset1.var1, 
    dataset1.var2
from 
  dataset2 left join dataset1
  on input(dataset2.id,best6.) = input(left(dataset1.id),best12.)
  ;
quit;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And be careful&amp;nbsp;with those SQL aliases. Everybody uses them, but they very often make the code harder to read - in this case dataset1 was mapped to "b" and dataset2 to "a",&amp;nbsp; very confusing, IMO. It is not that hard to use a change command to get rid of the aliases in the final code, and it normally makes everything much easier to read.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Feb 2018 12:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trouble-formatting-merge-keys/m-p/440863#M282554</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-28T12:44:04Z</dc:date>
    </item>
  </channel>
</rss>

