<?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 Proc SQL Join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521326#M141439</link>
    <description>&lt;P&gt;I am trying to join the following two data sets:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testA;
input categorical $3. value;
*order = _n_;
datalines;
Dog. 
M  7
F  5
Cat.
M  4
F  2
;
run;

data testA;
set testA;
order=_n_;
run;

data testB;
input categorical $2. value;
datalines;
Dog. 
F  3
Cat.
M  1
F  2
;
run;

proc sql;
create table final as
select a.*,b.* from testA a left join testB b on a.categorical=b.categorical
order by order;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My desire output is the following:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testA;
input categorical $ value value2;
datalines;
Dog . .
M 7 .
F 5 3
Cat . .
M 4 1
F 2 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The problems I've encountered is that 1) the 'categorical' id is not sorted alphabetically and I do not want to change its order 2) Since there are two Ms and Fs I don't know how to join without renaming the M F so that it's unique 3) It could be an inner join since what may be in value may not be in value2&lt;/P&gt;</description>
    <pubDate>Thu, 13 Dec 2018 23:06:40 GMT</pubDate>
    <dc:creator>serena13lee</dc:creator>
    <dc:date>2018-12-13T23:06:40Z</dc:date>
    <item>
      <title>Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521326#M141439</link>
      <description>&lt;P&gt;I am trying to join the following two data sets:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testA;
input categorical $3. value;
*order = _n_;
datalines;
Dog. 
M  7
F  5
Cat.
M  4
F  2
;
run;

data testA;
set testA;
order=_n_;
run;

data testB;
input categorical $2. value;
datalines;
Dog. 
F  3
Cat.
M  1
F  2
;
run;

proc sql;
create table final as
select a.*,b.* from testA a left join testB b on a.categorical=b.categorical
order by order;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My desire output is the following:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testA;
input categorical $ value value2;
datalines;
Dog . .
M 7 .
F 5 3
Cat . .
M 4 1
F 2 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The problems I've encountered is that 1) the 'categorical' id is not sorted alphabetically and I do not want to change its order 2) Since there are two Ms and Fs I don't know how to join without renaming the M F so that it's unique 3) It could be an inner join since what may be in value may not be in value2&lt;/P&gt;</description>
      <pubDate>Thu, 13 Dec 2018 23:06:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521326#M141439</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2018-12-13T23:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521331#M141441</link>
      <description>&lt;P&gt;Must it be using proc sql? Coz any proc sql join would be product of keys in both tables and that means will do many to many based on key value and not on a position. The ideal is a merge.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 00:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521331#M141441</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-14T00:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521332#M141442</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; No it doesn't have to be proc sql! I just thought that would be a good method to start.</description>
      <pubDate>Fri, 14 Dec 2018 00:08:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521332#M141442</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2018-12-14T00:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521333#M141443</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testA;
input categorical $3. value;
order = _n_;
datalines;
Dog . 
M  7
F  5
Cat .
M   4
F   2
;
run;
data testB;
input categorical $3. value;
datalines;
Dog . 
F   3
Cat .
M   1
F   2
;
run;

proc sort data=testa out=_testa;
by categorical;
run;
proc sort data=testb out=_testb;
by categorical;
run;


data temp;
merge _testa _testb(rename=(value=value2));
by categorical;
run;

proc sort data=temp out=want(drop=order);
by order;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Dec 2018 00:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521333#M141443</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-14T00:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521334#M141444</link>
      <description>&lt;P&gt;Also look at modify statement, your datasets appear to be a good case for applying modify statement&amp;nbsp; as master dataset does not have to sorted.&lt;/P&gt;
&lt;P&gt;If my understanding is right, modify statement with key categorical variable indexed look up would be a sound approach.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 00:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521334#M141444</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-12-14T00:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521336#M141445</link>
      <description>&lt;P&gt;Thanks so much for your reply! After running, everything looks except there is a 1 under value2 Dog (M). Can I ask how I can remove that in the case of a blank?&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 00:39:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521336#M141445</guid>
      <dc:creator>serena13lee</dc:creator>
      <dc:date>2018-12-14T00:39:37Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521346#M141451</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testA;
input categorical: $3. value: 8.;
*order = _n_;
datalines;
Dog . 
M  7
F  5
Cat .
M  4
F  2
;
run;


data testA;
set testA;
order=_n_;
run;

proc sort data=testa;
  by categorical order;
run;

data testa;
  set testa;
  by categorical;
  if first.categorical then 
  id=0;
  id+1;
run;

data testB;
input categorical: $3. value: 8.;
datalines;
Dog . 
F 3
Cat .
M  1
F  2
;
run;

data testb;
set testb;
order=_n_;
run;

proc sort data=testb;
  by categorical order;
run;

data testb;
  set testb;
  by categorical;
  if first.categorical then id=0;
  id+1;
run;

proc sql;
create table final as
select 
  coalescec(a.categorical,b.categorical) as categorical,
  a.value as valuea,
  b.value as valueb,
  a.order
  from 
 testA a left join testB b on a.categorical=b.categorical
 and a.id=b.id
order by a.order;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Double-check your desired output.&amp;nbsp; I think there are errors in it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 01:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521346#M141451</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2018-12-14T01:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521361#M141456</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Try restructuring your original datasets by adding a species(dog/cat) and gender (m/f) columns&amp;nbsp; . It will make your life easier .&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 03:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521361#M141456</guid>
      <dc:creator>34reqrwe</dc:creator>
      <dc:date>2018-12-14T03:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521369#M141460</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Modify the way you read your data:  */
data testA;
retain cat1;
input cat2 $3. value;
if missing(value) then cat1 = cat2;
else output;
datalines;
Dog. 
M  7
F  5
Cat.
M  4
F  2
;

data testB;
retain cat1;
input cat2 $3. value;
if missing(value) then cat1 = cat2;
else output;
datalines;
Dog. 
F  3
Cat.
M  1
F  2
;

/* Then you can use:  */
proc sql;
create table final as
select 
    a.*,
    b.value as value2 
from 
    testA a left join 
    testB b on a.cat1=b.cat1 and a.cat2=b.cat2
order by cat1 desc, cat2 desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Dec 2018 04:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Join/m-p/521369#M141460</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-12-14T04:43:54Z</dc:date>
    </item>
  </channel>
</rss>

