<?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: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials in Advanced Programming</title>
    <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961653#M320</link>
    <description>BTW: as for the quote() function that creates the quoted character string to be used in the where in () statement, the reference code is on p319 of SQL1:Essential PDF, part c of question s106s02.</description>
    <pubDate>Wed, 12 Mar 2025 08:53:30 GMT</pubDate>
    <dc:creator>dxiao2017</dc:creator>
    <dc:date>2025-03-12T08:53:30Z</dc:date>
    <item>
      <title>Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961468#M313</link>
      <description>&lt;P&gt;(The question is on PDF p207, Q3, answer is on p241)&lt;/P&gt;
&lt;P&gt;While reading the question, because I did not pay enough attention and ignored the requirement of using sql subquery, I first used two separate sql steps (a &lt;EM&gt;create table&lt;/EM&gt; step and a &lt;EM&gt;sql join&lt;/EM&gt; step) like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table countrycodes as
select distinct countrycode
   from sq.globalmetadata
   where upcase(region)=upcase("Europe &amp;amp; Central Asia") and
         upcase(incomegroup)=upcase("High income");
select * from countrycodes;
quit;
proc sql;
select g.countrycode,
       estyear1/100 as estpct1 format=percent8.2,
       estyear3/100 as estpct3 format=percent8.2,
       estyear1
   from sq.globalfindex as g inner join
        countrycodes as c
   on g.countrycode=c.countrycode
   where indicatorname="Borrowed for health or medical purposes (% age 15+)"
   order by estyear1 desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then I thought using &lt;EM&gt;select into :list;&lt;/EM&gt; could make the codes simpler, but the &lt;EM&gt;&amp;amp;list&lt;/EM&gt; in the &lt;EM&gt;where in;&lt;/EM&gt; statement of the next step needs quotation marks for character strings, and I forgot the syntax of &lt;EM&gt;quote()&lt;/EM&gt; function(I did not even remember whether it was a function or an option that do not has brackets), I tried something like quote, quotes, quotation and did not see the keywords turns into blue color (which often indicates the keyword was spelled correctly, but here the correct one is grey), and then I searched documents for about 10 minutes and found the correct syntax is quote, so I wrote something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select distinct quote(countrycode) 
   into :countrycodelist separated by "," 
   from sq.globalmetadata
   where upcase(region)=upcase("Europe &amp;amp; Central Asia") and
         upcase(incomegroup)=upcase("High income");
quit;
%put &amp;amp;countrycodelist;
proc sql;
select countrycode, indicatorname,
       estyear1/100 as estpct1 format=percent8.2,
       estyear3/100 as estpct3 format=percent8.2,
       estyear1
   from sq.globalfindex
   where indicatorname="Borrowed for health or medical purposes (% age 15+)"
      and countrycode in (&amp;amp;countrycodelist)
   order by estyear1 desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Until I see the correct answer, which uses a subquery in one sql step to solve the question. What I learnt is sql subquery sometimes can make things a lot more easier by saving certain base and macro steps, especially when one does not remember the detailed syntax of the base and macro step. On the other hand, however, when a sql subquery need to be developed to a macro, one may split the sql subquery into separate base steps, as doing so makes set different parameters possible.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select countrycode, indicatorname,
       estyear1/100 as estpct1 format=percent8.2,
       estyear3/100 as estpct3 format=percent8.2,
       estyear1
   from sq.globalfindex
   where indicatorname="Borrowed for health or medical purposes (% age 15+)"
      and countrycode in (select countrycode
                             from sq.globalmetadata
                             where upcase(region)=upcase("Europe &amp;amp; Central Asia") and
                                   upcase(incomegroup)=upcase("High income")
                          )
   order by estyear1 desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Mar 2025 18:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961468#M313</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-10T18:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961481#M314</link>
      <description>&lt;P&gt;You made a good point about breaking up code into smaller chunks for ease of setting parameters and sometimes easier for maintenance. I would like to add that there is a rule for using non-correlated subquery such as this example, in which you can only return values from a SINGLE column from the inner query. That may be limiting for some.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Mar 2025 19:49:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961481#M314</guid>
      <dc:creator>SuCheeTay</dc:creator>
      <dc:date>2025-03-10T19:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961487#M315</link>
      <description>&lt;P&gt;Hi SuCheeTay,&lt;/P&gt;
&lt;P&gt;Thank you for the comments, which elaborates what I wanted to say but was not able to phrase well!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Mar 2025 20:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961487#M315</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-10T20:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961488#M316</link>
      <description>&lt;P&gt;Welcome to the world of SQL subqueries! IMHO these are an underappreciated part of SQL. Once you get into correlated subqueries you will find that they are a very powerful and efficient way of creating result sets from different tables and different levels of data granularity.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Mar 2025 20:58:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961488#M316</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2025-03-10T20:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961503#M317</link>
      <description>&lt;P&gt;Not that it's shorter in this case, but you could also solve this using subqueries in a join like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select a.countrycode, a.indicatorname,
a.estyear/100 as estpct1 format=percent8.2, 
a.estyear3/100 as estpct3 format=percent8.2,
estyear1
from 
	(select * from sq.globalfindex 
		where indicatorname="Borrowed for health or medical purposes (% age 15+)") A
	inner join
	(select countrycode from sq.globalmetadata
		where upcase(region)=upcase("Europe &amp;amp; Central Asia") and
		upcase(incomegroup)=upcase("High income")) B
	on a.countrycode=b.countrycode
order by a.estyear1 desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Mar 2025 01:20:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961503#M317</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2025-03-11T01:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961649#M318</link>
      <description>&lt;P&gt;Hi SASKiwi, thanks a lot for your comments! I will get into more advanced proc sql steps such as correlated subqueries soon I guess. The first time (when I just began to learn base SAS procs) I see proc sql steps (using only the basic techniques including select, where, group by, having, order by) I was thinking they are very powerful techniques to create tables. Learning SAS is fun.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Mar 2025 08:26:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961649#M318</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-12T08:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961650#M319</link>
      <description>&lt;P&gt;Thanks a lot for your comments! So this is another way to solve the question, the logic of which is: using one proc sql step to select columns from a table that has been created through an inner join (of two tables).&lt;/P&gt;</description>
      <pubDate>Wed, 12 Mar 2025 08:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961650#M319</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-12T08:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961653#M320</link>
      <description>BTW: as for the quote() function that creates the quoted character string to be used in the where in () statement, the reference code is on p319 of SQL1:Essential PDF, part c of question s106s02.</description>
      <pubDate>Wed, 12 Mar 2025 08:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961653#M320</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-12T08:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using sql subquery:thoughts and what I learnt from a practice question of SQL1:Essentials</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961679#M322</link>
      <description>&lt;P&gt;This is an example of an in-line view, which is covered in the same lesson as the subqueries in the SAS SQL class.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Mar 2025 14:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Using-sql-subquery-thoughts-and-what-I-learnt-from-a-practice/m-p/961679#M322</guid>
      <dc:creator>SuCheeTay</dc:creator>
      <dc:date>2025-03-12T14:32:20Z</dc:date>
    </item>
  </channel>
</rss>

