<?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: PROC SQL JOIN using an aliased colum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582271#M165589</link>
    <description>&lt;P&gt;Did you adjust the JOIN condition?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;You either need to put the formula there as well, or you need to add the key word CALCULATED in front of it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post the code you're using if you're having issues, with the log preferably. If we can't see what you've ran, we're really just guessing at causes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@anonymous_user&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;ERROR: Column department could not be found in the table/view identified with the correlation name A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I try your solution, this is the error I get.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Aug 2019 21:36:51 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-08-19T21:36:51Z</dc:date>
    <item>
      <title>PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582230#M165573</link>
      <description>&lt;P&gt;I have the following query where I try to do a LEFT JOIN on an aliased column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the following errors&lt;/P&gt;&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, ',', AS, FORMAT, FROM, INFORMAT, INTO, LABEL, LEN,&lt;BR /&gt;LENGTH, TRANSCODE.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I join using an aliased column name?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
SELECT a.user_id, substr(a.item_id, 1, 3) as a.department, a.item_id, b.dept_name
FROM sas.db a
LEFT JOIN sas.db_2 b 
ON b.department_id=a.department
WHERE a.invoice_date&amp;gt;='18AUG2019'd;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Aug 2019 19:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582230#M165573</guid>
      <dc:creator>anonymous_user</dc:creator>
      <dc:date>2019-08-19T19:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582233#M165575</link>
      <description>&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
SELECT a.user_id, substr(a.item_id, 1, 3) as department, a.item_id, b.dept_name
FROM sas.db a
LEFT JOIN sas.db_2 b 
ON b.department_id=substr(a.item_id, 1, 3)
WHERE a.invoice_date&amp;gt;='18AUG2019'd;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or even better to have an inline-view&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
SELECT a.*, b.dept_name
FROM (select user_id, substr(item_id, 1, 3) as department, item_id,invoice_date from sas.db) a
LEFT JOIN sas.db_2 b 
ON b.department_id=a.department
WHERE a.invoice_date&amp;gt;='18AUG2019'd;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 10:08:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582233#M165575</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-20T10:08:53Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582247#M165581</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
SELECT a.user_id, substr(a.item_id, 1, 3) as department, a.item_id, b.dept_name
FROM sas.db a
LEFT JOIN sas.db_2 b 
ON b.department_id=a.department
WHERE a.invoice_date&amp;gt;='18AUG2019'd;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Change :&amp;nbsp;&lt;CODE class=" language-sas"&gt;substr(a.item_id, 1, 3) as department&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 20:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582247#M165581</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2019-08-19T20:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582264#M165586</link>
      <description>&lt;P&gt;The issue isn't the JOIN, the issue is that you're trying to name a newly created column with an alias which doesn't make sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;substr(a.item_id, 1, 3) as a.department&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Remove a. from the department and it will create the new variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@anonymous_user&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have the following query where I try to do a LEFT JOIN on an aliased column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get the following errors&lt;/P&gt;
&lt;P&gt;ERROR 22-322: Syntax error, expecting one of the following: a quoted string, ',', AS, FORMAT, FROM, INFORMAT, INTO, LABEL, LEN,&lt;BR /&gt;LENGTH, TRANSCODE.&lt;/P&gt;
&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I join using an aliased column name?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
SELECT a.user_id, substr(a.item_id, 1, 3) as a.department, a.item_id, b.dept_name
FROM sas.db a
LEFT JOIN sas.db_2 b 
ON b.department_id=a.department
WHERE a.invoice_date&amp;gt;='18AUG2019'd;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 21:12:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582264#M165586</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-19T21:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582267#M165587</link>
      <description>&lt;P&gt;ERROR: Column department could not be found in the table/view identified with the correlation name A.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 21:28:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582267#M165587</guid>
      <dc:creator>anonymous_user</dc:creator>
      <dc:date>2019-08-19T21:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582268#M165588</link>
      <description>&lt;P&gt;ERROR: Column department could not be found in the table/view identified with the correlation name A.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I try your solution, this is the error I get.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 21:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582268#M165588</guid>
      <dc:creator>anonymous_user</dc:creator>
      <dc:date>2019-08-19T21:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582271#M165589</link>
      <description>&lt;P&gt;Did you adjust the JOIN condition?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;You either need to put the formula there as well, or you need to add the key word CALCULATED in front of it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post the code you're using if you're having issues, with the log preferably. If we can't see what you've ran, we're really just guessing at causes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@anonymous_user&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;ERROR: Column department could not be found in the table/view identified with the correlation name A.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I try your solution, this is the error I get.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 21:36:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582271#M165589</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-19T21:36:51Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582283#M165594</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The thumb rule is ON condition associates with FROM . In other words&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When we say &lt;EM&gt;Hey Proc sql,&lt;/EM&gt; Please select the columns from the tables(this means sql processes the FROM and ON , keeps the copy(resulting FROM-ON execution)&amp;nbsp; like that of a PDV ) from which SELECT chooses the columns to print or write to the OUTPUT table. In many cases offering advantages to do column wise/row wise operations, of course the disadvantage being it is highly I/O dependent/intensive.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore to conclude, the computed column in SELECT clause cannot be used ON join condition whether or not with a CALCULATED Keyword. Of course you can circumvent with an&amp;nbsp; &lt;EM&gt;in line view ,&amp;nbsp;&lt;/EM&gt;albeit that's another pre-pass. The computed aka CALCULATED column can be used in GROUP BY clause but that's so obvious because it anyways makes it self explanatory that it groups the FROM or FROM-ON result. In essence FROM tables/FROM-ON is a unique independent block so to speak, whose result forms the &lt;STRONG&gt;&lt;EM&gt;base&lt;/EM&gt; &lt;/STRONG&gt;for other clauses to act upon.&lt;/P&gt;
&lt;P&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Forgive me if my writing isn't great. I wish I had the lexicon of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp; /&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp; to compile sentences so eloquently and&amp;nbsp; yet quoting references. Well, hopefully in time. I am so jealous. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2019 23:47:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582283#M165594</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-19T23:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582288#M165596</link>
      <description>Thanks, Novinosrin, you are correct!&lt;BR /&gt;&lt;BR /&gt;The calculation should be in the join condition as well.</description>
      <pubDate>Tue, 20 Aug 2019 00:02:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582288#M165596</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-20T00:02:00Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582292#M165598</link>
      <description>&lt;P&gt;Finally this illustration is for my mom(for teaching me SQL) and my recently departed father's soul. To whom I owe everything&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data one;
input var $20.;
cards;
hope floats
hope sinks
hope
;


data two;
input var1 $ var2 &amp;amp; $20.;
cards;
hope  sad and happy
;

/*Expression=variable success*/
proc sql;
create table want as
select a.var,scan(a.var,1) as newvar,var2
from one a left join two b
on scan(a.var,1)=var1;
quit;

/*Calculated newvar-error*/
proc sql;
create table want as
select a.var,scan(a.var,1) as newvar,var2
from one a left join two b
on calculated newvar=var1;
quit;

/*Without Calculated newvar-error*/
proc sql;
create table want as
select a.var,scan(a.var,1) as newvar,var2
from one a left join two b
on  newvar=var1;
quit;

/*In line View success*/
proc sql;
create table want as
select a.*,var2
from (select var,scan(var,1) as newvar from one) a left join two b
on  newvar=var1;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 00:21:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582292#M165598</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-20T00:21:52Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582323#M165613</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;(...)&lt;/P&gt;
&lt;P&gt;or even better to have an inline-view&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
SELECT a.*, b.dept_name
FROM (select user_id, substr(a.item_id, 1, 3) as department, item_id,invoice_date from sas.db) a
...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now that you've pulled my attention to this thread, I would like to point out a minor correction to the accepted solution: Either the inline-view must define the alias &lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt; or variable &lt;FONT face="courier new,courier"&gt;item_id&lt;/FONT&gt; from dataset &lt;FONT face="courier new,courier"&gt;sas.db&lt;/FONT&gt; needs to be referenced without an alias:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;... substr(&lt;STRONG&gt;item_id&lt;/STRONG&gt;, 1, 3) ...&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 07:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582323#M165613</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-08-20T07:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL JOIN using an aliased colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582361#M165634</link>
      <description>&lt;P&gt;Yes Sir, So true. I can't believe I overlooked that &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 10:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-JOIN-using-an-aliased-colum/m-p/582361#M165634</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-20T10:07:58Z</dc:date>
    </item>
  </channel>
</rss>

