<?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 subquery, wrong variable name, no error in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606443#M76574</link>
    <description>&lt;P&gt;Hey Kevin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're not alone in being stunned by this.&amp;nbsp; I was tipped off to it a few months ago, and started a SAS-L thread that got some good feedback from the 'L:&amp;nbsp;&lt;A href="https://listserv.uga.edu/cgi-bin/wa?A2=SAS-L;11810443.1907b" target="_blank"&gt;https://listserv.uga.edu/cgi-bin/wa?A2=SAS-L;11810443.1907b&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code runs without errors:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  select *
  from sashelp.class
  where name in
    (select name    /*n.b.: sashelp.iris does not have a variable NAME*/
     from sashelp.iris
     where species is not missing
    )
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It turns out the SQL compiler happily turns what the author intended as in inner query into a correlated subquery.&amp;nbsp; In the SAS-L thread&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;kindly pointed out that if you add the FEEDBACK option, the log will show you that the reference to NAME in the inner query is interpreted as class.name rather than iris.name:&lt;/P&gt;
&lt;PRE&gt;11   proc sql feedback;
12     select *
13     from sashelp.class
14     where name in
15       (select name    /*n.b.: sashelp.iris does not have a variable NAME*/
16        from sashelp.iris
17        where species is not missing
18       )
19     ;
NOTE: Statement transforms to:

        select CLASS.Name, CLASS.Sex, CLASS.Age, CLASS.Height, CLASS.Weight
          from SASHELP.CLASS
         where CLASS.Name in
               ( select &lt;STRONG&gt;CLASS.Name&lt;/STRONG&gt;
                   from SASHELP.IRIS
                  where IRIS.Species is not null
               );

20   quit;
&lt;/PRE&gt;
&lt;P&gt;As you showed and Kurt mentioned, the offensive programming to catch this error would be to always use two level names when you have two tables&amp;nbsp; in a query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;21   proc sql ;
22     select *
23     from sashelp.class as a
24     where a.name in
25       (select b.name    /*n.b.: sashelp.iris does not have a variable NAME*/
26        from sashelp.iris as b
27        where species is not missing
28       )
29     ;
ERROR: Column name could not be found in the table/view identified with the correlation name B.
ERROR: Unresolved reference to table/correlation name b.
30   quit;
NOTE: The SAS System stopped processing this step because of errors.
&lt;/PRE&gt;
&lt;P&gt;I haven't been in that habit, but seeing this scared me enough to try to learn this new habit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Nov 2019 13:19:37 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2019-11-22T13:19:37Z</dc:date>
    <item>
      <title>SQL subquery, wrong variable name, no error</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606354#M76569</link>
      <description>&lt;P&gt;I encountered this (stunning) issue today:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;171  proc sql ;
172    create table ds_datadiff as
173    select *
174    from sasdata.ds
175    where recordid in
176             ( select recordid
177               from delta.datadiff
178               where dataset = "DS"
179             )
180    ;
NOTE: Table WORK.DS_DATADIFF created, with 45 rows and 55 columns.

181  quit ;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds


182
183  proc sql ;
184    select b.recordid
185    from delta.datadiff as b
186    where dataset = "DS"
187    ;
ERROR: Column recordid could not be found in the table/view identified with
       the correlation name B.
188  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.01 seconds

189


190  proc sql ;
191    create table ds_datadiff as
192    select *
193    from sasdata.ds
194    where recordid in
195             ( select b.recordid
196               from delta.datadiff as b
197               where dataset = "DS"
198             )
199    ;
ERROR: Column recordid could not be found in the table/view identified with
       the correlation name B.
ERROR: Unresolved reference to table/correlation name b.
ERROR: Expression using IN has components that are of different data types.
NOTE: The IN referred to may have been transformed from an OR to an IN at some
      point during PROC SQL WHERE clause optimization.
200  quit ;
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.02 seconds
      cpu time            0.01 seconds

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue is that RECORDID is not is DELTA.DATADIFF as illustrated by the second two SQL procedure above.&amp;nbsp; I find this highly unexpected and worrisome.&amp;nbsp; Should the subquery be "protected", meaning that the SELECT clause in it should not pull from other parts of the SQL?&amp;nbsp; I may begin defensive programming like the second procedure to protect against such errors in coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Kevin&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2019 01:31:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606354#M76569</guid>
      <dc:creator>KevinViel</dc:creator>
      <dc:date>2019-11-22T01:31:56Z</dc:date>
    </item>
    <item>
      <title>Re: SQL subquery, wrong variable name, no error</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606365#M76572</link>
      <description>&lt;P&gt;Not super helpful but this is something that's been around for a long time and is the default behaviour.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/Case-when-used-with-a-subquery-strange-result/td-p/210931" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/Case-when-used-with-a-subquery-strange-result/td-p/210931&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2019 03:35:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606365#M76572</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-22T03:35:42Z</dc:date>
    </item>
    <item>
      <title>Re: SQL subquery, wrong variable name, no error</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606402#M76573</link>
      <description>&lt;P&gt;Bottom line: always use two-level column names (at least when more than one table is used) in a SQL select, for clear identification.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2019 07:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606402#M76573</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-22T07:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: SQL subquery, wrong variable name, no error</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606443#M76574</link>
      <description>&lt;P&gt;Hey Kevin,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're not alone in being stunned by this.&amp;nbsp; I was tipped off to it a few months ago, and started a SAS-L thread that got some good feedback from the 'L:&amp;nbsp;&lt;A href="https://listserv.uga.edu/cgi-bin/wa?A2=SAS-L;11810443.1907b" target="_blank"&gt;https://listserv.uga.edu/cgi-bin/wa?A2=SAS-L;11810443.1907b&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code runs without errors:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  select *
  from sashelp.class
  where name in
    (select name    /*n.b.: sashelp.iris does not have a variable NAME*/
     from sashelp.iris
     where species is not missing
    )
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It turns out the SQL compiler happily turns what the author intended as in inner query into a correlated subquery.&amp;nbsp; In the SAS-L thread&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;kindly pointed out that if you add the FEEDBACK option, the log will show you that the reference to NAME in the inner query is interpreted as class.name rather than iris.name:&lt;/P&gt;
&lt;PRE&gt;11   proc sql feedback;
12     select *
13     from sashelp.class
14     where name in
15       (select name    /*n.b.: sashelp.iris does not have a variable NAME*/
16        from sashelp.iris
17        where species is not missing
18       )
19     ;
NOTE: Statement transforms to:

        select CLASS.Name, CLASS.Sex, CLASS.Age, CLASS.Height, CLASS.Weight
          from SASHELP.CLASS
         where CLASS.Name in
               ( select &lt;STRONG&gt;CLASS.Name&lt;/STRONG&gt;
                   from SASHELP.IRIS
                  where IRIS.Species is not null
               );

20   quit;
&lt;/PRE&gt;
&lt;P&gt;As you showed and Kurt mentioned, the offensive programming to catch this error would be to always use two level names when you have two tables&amp;nbsp; in a query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;21   proc sql ;
22     select *
23     from sashelp.class as a
24     where a.name in
25       (select b.name    /*n.b.: sashelp.iris does not have a variable NAME*/
26        from sashelp.iris as b
27        where species is not missing
28       )
29     ;
ERROR: Column name could not be found in the table/view identified with the correlation name B.
ERROR: Unresolved reference to table/correlation name b.
30   quit;
NOTE: The SAS System stopped processing this step because of errors.
&lt;/PRE&gt;
&lt;P&gt;I haven't been in that habit, but seeing this scared me enough to try to learn this new habit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2019 13:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606443#M76574</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2019-11-22T13:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: SQL subquery, wrong variable name, no error</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606512#M76576</link>
      <description>&lt;P&gt;SAS is just being helpful.&amp;nbsp; You asked for &lt;SPAN&gt;RECORDID&amp;nbsp;&lt;/SPAN&gt;and it found a variable &lt;SPAN&gt;RECORDID&amp;nbsp;&lt;/SPAN&gt;and used it.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Nov 2019 16:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-subquery-wrong-variable-name-no-error/m-p/606512#M76576</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-22T16:42:43Z</dc:date>
    </item>
  </channel>
</rss>

