<?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: SQL Cartesian and Then Left Join: Correlated Reference Error? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/709854#M218393</link>
    <description>&lt;P&gt;I think it is a matter of order of precedence. Your second query, with the CROSS JOIN statement, first executes the CROSS JOIN and then the LEFT JOIN.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I think the comma has a different precedence, so that PROC SQL first creates the TICKER (I) query, and then tries to evaluate the LEFT JOIN of the DATE (J) query with the whole table (K). So the LEFT JOIN is only against the J query, which has no reference to the I alias.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Jan 2021 08:54:01 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2021-01-07T08:54:01Z</dc:date>
    <item>
      <title>SQL Cartesian and Then Left Join: Correlated Reference Error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/709776#M218343</link>
      <description>&lt;P&gt;I have one unbalanced panel data set and am using SQL to make it balanced by (1) taking the Cartesian and then (2) left joining the original set. The following data set has TICKER and DATE as two indices. I Cartesian joined distinct TICKER and distinct DATE as follows and then tried to left join ADJUST to the Cartesian.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data yahoo;
	input ticker $ @@;
	i=cats("https://query1.finance.yahoo.com/v7/finance/download/",ticker,
		'?period1=-999999999999&amp;amp;period2=999999999999&amp;amp;interval=1d');
	infile j url filevar=i firstobs=2 dsd end=k;
	do until(k);
		input date yymmdd10. +1 open high low close adjust volume;
		output;
	end;
cards;
BA DIS KO
;

proc sql;
	create table i as
	select i.ticker,j.date,adjust
	from (select distinct ticker from yahoo) i,
	(select distinct date from yahoo) j
	left join yahoo k on i.ticker=k.ticker &amp;amp; j.date=k.date
	order by ticker,date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And the code above prints a correlated reference error message as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;14   proc sql;
15       create table i as
16       select i.ticker,j.date,adjust
17       from (select distinct ticker from yahoo) i,
18       (select distinct date from yahoo) j
19       left join yahoo k on i.ticker=k.ticker &amp;amp; j.date=k.date
20       order by ticker,date;
ERROR: Correlated reference to column ticker is not contained
       within a subquery.
21   quit;
NOTE: The SAS System stopped processing this step because of
      errors.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I reviewed &lt;A href="https://support.sas.com/kb/32/646.html" target="_blank" rel="noopener"&gt;this note&lt;/A&gt; but couldn't understand because my ON clause for LEFT JOIN has all the variables already introduced: I.TICKER, K.TICKER, J.DATE, and K.DATE. I wonder whether this can be done in one SQL query.&lt;/P&gt;&lt;P&gt;P.S. CROSS JOIN rather than just comma as follows works.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table i as
	select i.ticker,j.date,adjust
	from (select distinct ticker from yahoo) i
	cross join (select distinct date from yahoo) j
	left join yahoo k on i.ticker=k.ticker &amp;amp; j.date=k.date
	order by ticker,date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I wonder whether a comma and CROSS JOIN differ in other cases.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 03:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/709776#M218343</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2021-01-07T03:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Cartesian and Then Left Join: Correlated Reference Error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/709850#M218389</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt; I wonder whether a comma and CROSS JOIN differ in other cases.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;A comma is similar to an INNER JOIN.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 08:25:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/709850#M218389</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-01-07T08:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Cartesian and Then Left Join: Correlated Reference Error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/709854#M218393</link>
      <description>&lt;P&gt;I think it is a matter of order of precedence. Your second query, with the CROSS JOIN statement, first executes the CROSS JOIN and then the LEFT JOIN.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I think the comma has a different precedence, so that PROC SQL first creates the TICKER (I) query, and then tries to evaluate the LEFT JOIN of the DATE (J) query with the whole table (K). So the LEFT JOIN is only against the J query, which has no reference to the I alias.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 08:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/709854#M218393</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-01-07T08:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Cartesian and Then Left Join: Correlated Reference Error?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/710111#M218532</link>
      <description>&lt;P&gt;I think this explains because the original code also works after adding parentheses before and after the two SELECT clauses as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table i as
	select i.ticker,j.date,adjust
	from ((select distinct ticker from yahoo) i,
	(select distinct date from yahoo) j)
	left join yahoo k on i.ticker=k.ticker &amp;amp; j.date=k.date
	order by ticker,date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It seems PROC SQL tries to process JOIN first and then the comma unless the parentheses. Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 04:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Cartesian-and-Then-Left-Join-Correlated-Reference-Error/m-p/710111#M218532</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2021-01-08T04:24:28Z</dc:date>
    </item>
  </channel>
</rss>

