<?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 Can Anyone Explain this Join Issue? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641770#M191340</link>
    <description>&lt;P&gt;I have a task to use SAS to write some data aggregation in SAS Studio University.&amp;nbsp; I've been using SQL Server and MySQL for over a decade.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However... I'm trying to do a simple self join on a table and I have no idea how to explain the results I'm getting....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ended up going all the way down to a small table to figure out what is going on with 1 column called 'Gender' and 2 rows 'Male' and 'Female':&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select
	m.Gender,
	f.Gender
from DATA.GENDERS m
inner join DATA.GENDERS f 
	on f.Gender = 'Female'
where m.Gender = 'Male';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, the results I get are:&lt;/P&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Gender&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Gender&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried several different ways of putting it together and it seriously doesn't make any sense why I'm getting this join result?&amp;nbsp; It's driving me crazy!&lt;/P&gt;&lt;P&gt;On a side note...&amp;nbsp; it looks like INNER JOINs return null values?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks...&lt;/P&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 21 Apr 2020 22:50:36 GMT</pubDate>
    <dc:creator>dustinsaunders</dc:creator>
    <dc:date>2020-04-21T22:50:36Z</dc:date>
    <item>
      <title>Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641770#M191340</link>
      <description>&lt;P&gt;I have a task to use SAS to write some data aggregation in SAS Studio University.&amp;nbsp; I've been using SQL Server and MySQL for over a decade.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However... I'm trying to do a simple self join on a table and I have no idea how to explain the results I'm getting....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ended up going all the way down to a small table to figure out what is going on with 1 column called 'Gender' and 2 rows 'Male' and 'Female':&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select
	m.Gender,
	f.Gender
from DATA.GENDERS m
inner join DATA.GENDERS f 
	on f.Gender = 'Female'
where m.Gender = 'Male';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, the results I get are:&lt;/P&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default dgrid-selected ui-state-active"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Gender&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Gender&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried several different ways of putting it together and it seriously doesn't make any sense why I'm getting this join result?&amp;nbsp; It's driving me crazy!&lt;/P&gt;&lt;P&gt;On a side note...&amp;nbsp; it looks like INNER JOINs return null values?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks...&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 21 Apr 2020 22:50:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641770#M191340</guid>
      <dc:creator>dustinsaunders</dc:creator>
      <dc:date>2020-04-21T22:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641772#M191341</link>
      <description>&lt;P&gt;For reference, here's the exact same query in SQL Server.&amp;nbsp; Do the joins just work completely different that standard TSQL?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sql_query.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38534i61A583977A12D8C1/image-size/large?v=v2&amp;amp;px=999" role="button" title="sql_query.png" alt="sql_query.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 22:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641772#M191341</guid>
      <dc:creator>dustinsaunders</dc:creator>
      <dc:date>2020-04-21T22:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641774#M191343</link>
      <description>&lt;P&gt;Are you sure those are the values?&amp;nbsp; Do you have a format attached to the variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data genders;
  input gender $10.;
cards;
Male
Female
;

proc sql;
select
   m.Gender as M_Gender
 , f.Gender as F_Gender
from GENDERS m
inner join GENDERS f 
	on f.Gender = 'Female'
where m.Gender='Male'
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;M_Gender    F_Gender
----------------------
Male        Female
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 22:59:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641774#M191343</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-21T22:59:38Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641775#M191344</link>
      <description>&lt;P&gt;What is the actual problem that lead you to create this crazy example?&lt;BR /&gt;What real life situation would you have where you use a join condition that only referenced values from one of the tables?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 23:03:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641775#M191344</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-21T23:03:04Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641779#M191347</link>
      <description>&lt;P&gt;Thanks for the response.&amp;nbsp; The type for the field is CHAR(13).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Self joins aren't uncommon in the normal breadth of SQL work I do, but to elaborate the data actually looks more like this:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Gender&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Question&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Result&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;Male&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;Female&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;66&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the join is Male to Female to compare the differences in values for their responses to Questions A, B, C, etc...&lt;/P&gt;&lt;P&gt;So the join is more like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;f.Gender='Female' and f.Question = m.Question&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but even going back to just the one column I am getting the unexpected result.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 23:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641779#M191347</guid>
      <dc:creator>dustinsaunders</dc:creator>
      <dc:date>2020-04-21T23:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641784#M191349</link>
      <description>&lt;P&gt;Well, I figured out what is going on here, I was getting the issue because it was creating a view and I didn't have an alias for the columns which is invalid.&amp;nbsp; What is interesting is that it proceeds with the view creation but uses only the first instance of the column name rather than throwing a warning.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data genders;
  input gender $10.;
cards;
Male
Female
;

proc sql;
create view GENDERS_JOINED as
select
   m.Gender,
   f.Gender
from GENDERS m
inner join GENDERS f 
	on f.Gender = 'Female'
where m.Gender='Male';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is resulting in the following:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="result.png" style="width: 272px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38535iDAD39EF97D3C23DD/image-size/large?v=v2&amp;amp;px=999" role="button" title="result.png" alt="result.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;However, providing an alias for the fields it doesn't create the issue:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data genders;
  input gender $10.;
cards;
Male
Female
;

proc sql;
create view GENDERS_JOINED as
select
   m.Gender as M_G,
   f.Gender as F_G
from GENDERS m
inner join GENDERS f 
	on f.Gender = 'Female'
where m.Gender='Male';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dustinsaunders_0-1587511902664.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38536iF838445EDBD518D9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dustinsaunders_0-1587511902664.png" alt="dustinsaunders_0-1587511902664.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;If you aren't creating the view with no alias it still works fine as expected:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data genders;
  input gender $10.;
cards;
Male
Female
;

proc sql;

select
   m.Gender,
   f.Gender
from GENDERS m
inner join GENDERS f 
	on f.Gender = 'Female'
where m.Gender='Male';&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="worked.png" style="width: 201px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38537i0D0275B9E8CADE58/image-size/large?v=v2&amp;amp;px=999" role="button" title="worked.png" alt="worked.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 23:40:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641784#M191349</guid>
      <dc:creator>dustinsaunders</dc:creator>
      <dc:date>2020-04-21T23:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641790#M191352</link>
      <description>&lt;P&gt;You cannot have two variables with the same name in a DATASET (or a view which has to mimic a dataset structure).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can, like in both your example and mine, make a REPORT from PROC SQL that is pulling values FROM two variables that USED TO have the same name, since that doesn't make an actual dataset .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you essentially ran:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select gender,gender from have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 00:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641790#M191352</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-22T00:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641835#M191377</link>
      <description>&lt;P&gt;When I run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data genders;
input gender $;
datalines;
Female
Male
;

proc sql;
select
	m.Gender,
	f.Gender
from GENDERS m
inner join GENDERS f 
	on f.Gender = 'Female'
where m.Gender = 'Male';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this:&lt;/P&gt;
&lt;PRE&gt;gender	gender
Male	Female
&lt;/PRE&gt;
&lt;P&gt;so your data has to be different from what you think.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 08:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641835#M191377</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-22T08:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641837#M191379</link>
      <description>&lt;P&gt;Hello Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found the issue in an earlier post-- the problem was with creating the query as view without an alias which is invalid.&amp;nbsp; However, instead of throwing an error SAS will generate the view but use the first instance of the key for all results.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 08:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641837#M191379</guid>
      <dc:creator>dustinsaunders</dc:creator>
      <dc:date>2020-04-22T08:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Can Anyone Explain this Join Issue?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641845#M191385</link>
      <description>&lt;P&gt;To give you even more food for thought, run this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data genders;
input gender $;
datalines;
Female
Male
;

proc sql;
create view GENDERS_JOINED as
select
   m.Gender,
   f.Gender
from GENDERS m
inner join GENDERS f 
	on f.Gender = 'Female'
where m.Gender='Male';
quit;

data test1;
set genders_joined;
run;

proc sql;
create table test2 as
select * from genders_joined;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and look at the two resulting tables.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 08:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Can-Anyone-Explain-this-Join-Issue/m-p/641845#M191385</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-22T08:30:06Z</dc:date>
    </item>
  </channel>
</rss>

