<?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: merging two data sets by using two &amp;quot;by variables &amp;quot; in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526502#M143394</link>
    <description>&lt;P&gt;Ok, try this and let me know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just a slight adjustment to get all vars from both&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want(drop=_:) as
select a.*,b.*,coalescec(a._cik ,CIK_1) as CIK
from one(rename=(cik=_cik)) a left join two(rename=(cik=__cik)) b
on a._cik=b.__cik or a._cik=CIK_1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 11 Jan 2019 21:18:03 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-01-11T21:18:03Z</dc:date>
    <item>
      <title>merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526115#M143211</link>
      <description>&lt;P&gt;hey all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to merge two data sets that have a common variable called CIK, but in data set 2 I have CIK and CIK_1. However, the CIK in data set 1 could be CIK or CIK_1 which is not given separately like in data set 2. I was wondering if I can first merge&amp;nbsp;them by CIK, and if there is no matching CIK, then compare CIK in data set one to CIK_1 in data set 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;below is the simplified data sets 1 and 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;data set 1 (SORTED BY cik)&lt;/P&gt;&lt;P&gt;Company name&amp;nbsp; &amp;nbsp; &amp;nbsp;CIK&amp;nbsp;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;123456&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;654123&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data set 2 (sorted by CIK)&lt;/P&gt;&lt;P&gt;Company name&amp;nbsp; &amp;nbsp; &amp;nbsp;CIK&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CIK_1&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 456789&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 123456&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;654123&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the final merged data set&amp;nbsp; looks like this:&lt;/P&gt;&lt;P&gt;Company name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CIK&amp;nbsp;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;123456&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 654123&lt;/P&gt;&lt;P&gt;thanks in advance.&lt;/P&gt;&lt;P&gt;Ziba&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jan 2019 17:03:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526115#M143211</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2019-01-10T17:03:08Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526121#M143217</link>
      <description>&lt;P&gt;straight forward sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data one;
input Companyname  $   CIK $;
cards;
A                             123456
B                             654123
;
data two;
input Companyname  $   CIK     $         CIK_1 $;
cards;
A                            456789          123456
B                              .             654123
;

proc sql;
create table want as
select a.companyname,coalescec(a.cik ,CIK_1) as CIK
from one a left join two b
on a.cik=b.cik or a.cik=CIK_1;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Jan 2019 17:17:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526121#M143217</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-10T17:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526474#M143377</link>
      <description>&lt;P&gt;hey&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am sorry I forgot to mention in data set 1 i have more than two variables, and I need to grab them all (I need to grab all the variables in data set 2 as well). So I guess I need to use * to select all the variables?&lt;/P&gt;&lt;P&gt;I&amp;nbsp;don`t&amp;nbsp;know how to use sql and I am not sure why you have a and b in the code? should I create a and b in the first place or I dont have to?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jan 2019 20:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526474#M143377</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2019-01-11T20:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526476#M143379</link>
      <description>&lt;P&gt;Can you please expand your sample a little more with some blah blah, and i will copy/paste here and modify the sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason is to avoid going back and forth&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jan 2019 20:16:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526476#M143379</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-11T20:16:42Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526498#M143391</link>
      <description>&lt;P&gt;In data set 1, I have variables up to 24 variables, and it includes&amp;nbsp;company name and CIK.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in data set 2, I have variables up to 35, and it includes&amp;nbsp;&amp;nbsp;company name( it could be in the same format as data set 1, but I dont want them to merge into one column, so I used Company name_UCLA to differentiate), CIK, CIK_1.&lt;/P&gt;&lt;P&gt;so&amp;nbsp;I think the desired merged data set should have 58 variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should I provide you with all the variables?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jan 2019 21:00:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526498#M143391</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2019-01-11T21:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526502#M143394</link>
      <description>&lt;P&gt;Ok, try this and let me know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just a slight adjustment to get all vars from both&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want(drop=_:) as
select a.*,b.*,coalescec(a._cik ,CIK_1) as CIK
from one(rename=(cik=_cik)) a left join two(rename=(cik=__cik)) b
on a._cik=b.__cik or a._cik=CIK_1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Jan 2019 21:18:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526502#M143394</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-11T21:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526504#M143396</link>
      <description>&lt;P&gt;Just play around, and seriously if you are not a lazy person like me you can ofcourse type select a.var1,a.var2,..a.varn and so on.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jan 2019 21:20:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526504#M143396</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-11T21:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526525#M143409</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want(drop=_:) as
select a.* from final.public_sort, b.* from final.ucla_sort ,coalescec(a._cik ,CIK_1) as CIK
from final.public_sort(rename=(cik=_cik)) a left join final.ucla_sort(rename=(cik=__cik)) b
on a._cik=b.__cik or a._cik=CIK_1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried this code after I changed the names one and two into&amp;nbsp;my sas data sets name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I have a syntax error pointing the star sign&amp;nbsp;next to b:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;433 proc sql;&lt;BR /&gt;434 create table want(drop=_:) as&lt;BR /&gt;435 select a.* from final.public_sort,&amp;nbsp; &amp;nbsp;b.* from final.ucla_sort ,coalescec(a._cik ,CIK_1) as CIK&lt;BR /&gt;-&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;76&lt;BR /&gt;&lt;STRONG&gt;ERROR 22-322: Expecting a name.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I am sure the code is correct, but Im not getting the results. Could you tell where I did it wrong?&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jan 2019 23:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526525#M143409</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2019-01-11T23:12:32Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526526#M143410</link>
      <description>&lt;P&gt;proc sql;&lt;BR /&gt;create table want(drop=_:) as&lt;BR /&gt;select a.* &lt;STRONG&gt;f&lt;STRIKE&gt;rom final.public_sort&lt;/STRIKE&gt;,&lt;/STRONG&gt; b.* &lt;STRONG&gt;&lt;STRIKE&gt;from final.ucla_sort&lt;/STRIKE&gt;&lt;/STRONG&gt; ,coalescec(a._cik ,CIK_1) as CIK&lt;BR /&gt;from final.public_sort(rename=(cik=_cik)) a left join final.ucla_sort(rename=(cik=__cik)) b&lt;BR /&gt;on a._cik=b.__cik or a._cik=CIK_1;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Corrected version below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want(drop=_:) as
select a.* , b.* ,coalescec(a._cik ,CIK_1) as CIK
from final.public_sort(rename=(cik=_cik)) a left join final.ucla_sort(rename=(cik=__cik)) b
on a._cik=b.__cik or a._cik=CIK_1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I took the sample as char values, if cik is a num num value, use &lt;STRONG&gt;coalesce&lt;/STRONG&gt; instead of&amp;nbsp;&lt;STRONG&gt;coalescec&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jan 2019 23:24:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526526#M143410</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-11T23:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526557#M143420</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/247610"&gt;@Ziba&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I believe it would help if you could post SAS datastep code which creates sample data. You don't need to create all variables but just enough so the problem to be solved becomes clear.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jan 2019 01:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526557#M143420</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-01-12T01:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526588#M143439</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I took the sample as char values, if cik is a num num value, use &lt;STRONG&gt;coalesce&lt;/STRONG&gt; instead of&amp;nbsp;&lt;STRONG&gt;coalescec&lt;/STRONG&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just a side note: In PROC SQL you can always use the (older, ANSI SQL) function COALESCE, which works with character arguments as well as with numeric arguments (not with mixed types, though). As an additional advantage over the DATA step function COALESCEC (introduced in SAS 9, together with the &lt;EM&gt;DATA step&lt;/EM&gt; function COALESCE) it assigns a suitable length to its results, not the default length of 200, which is mostly too long -- and when it's too short, it can even truncate the result (in PROC SQL&amp;nbsp;&lt;EM&gt;without notice&lt;/EM&gt;).&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jan 2019 13:42:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/526588#M143439</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-01-12T13:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: merging two data sets by using two "by variables "</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/527496#M143816</link>
      <description>&lt;P&gt;The code worked, I appreciated your help. Thank you very much,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jan 2019 20:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-two-data-sets-by-using-two-quot-by-variables-quot/m-p/527496#M143816</guid>
      <dc:creator>Ziba</dc:creator>
      <dc:date>2019-01-15T20:47:54Z</dc:date>
    </item>
  </channel>
</rss>

