<?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 Selecting into macro variables with ordering in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-into-macro-variables-with-ordering/m-p/154122#M30236</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you ever received this note in the SAS log when using the SELECT ... INTO ... syntax of PROC SQL?&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example if you want to generate a list of variables from DICTIONARY.COLUMNS (or PROC CONTENTS output dataset) and preserve the data set order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc sql noprint ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; select name&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; into :varlist separated by ' '&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; from dictionary.columns&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; where libname='SASHELP'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; and memname='CLASS'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; order by varnum&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%put nvars=&amp;amp;sqlobs varlist=&amp;amp;varlist;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Generates this output:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; color: #3366ff;"&gt;NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;xxxx&amp;nbsp; %put nvars=&amp;amp;sqlobs varlist=&amp;amp;varlist;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;nvars=5 varlist=Name Sex Age Height Weight&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I used to do was to add the ordering variable (VARNUM) to the select clause and use a dummy macro variable to hold the result.&amp;nbsp; But you can instead re-use the same macro variable if you set the order of the variables in the SELECT clause properly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc sql noprint ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; select &lt;STRONG&gt;varnum&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , name&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; into&amp;nbsp; &lt;STRONG&gt;:varlist&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,:varlist separated by ' '&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; from dictionary.columns&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; where libname='SASHELP'&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; and memname='CLASS'&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; order by varnum&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%put nvars=&amp;amp;sqlobs varlist=&amp;amp;varlist;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now the NOTE no longer appears in the SAS log.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 23 Nov 2013 17:01:43 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2013-11-23T17:01:43Z</dc:date>
    <item>
      <title>Selecting into macro variables with ordering</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-into-macro-variables-with-ordering/m-p/154122#M30236</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you ever received this note in the SAS log when using the SELECT ... INTO ... syntax of PROC SQL?&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example if you want to generate a list of variables from DICTIONARY.COLUMNS (or PROC CONTENTS output dataset) and preserve the data set order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc sql noprint ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; select name&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; into :varlist separated by ' '&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; from dictionary.columns&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; where libname='SASHELP'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; and memname='CLASS'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; order by varnum&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%put nvars=&amp;amp;sqlobs varlist=&amp;amp;varlist;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Generates this output:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; color: #3366ff;"&gt;NOTE: The query as specified involves ordering by an item that doesn't appear in its SELECT clause.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;xxxx&amp;nbsp; %put nvars=&amp;amp;sqlobs varlist=&amp;amp;varlist;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;nvars=5 varlist=Name Sex Age Height Weight&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I used to do was to add the ordering variable (VARNUM) to the select clause and use a dummy macro variable to hold the result.&amp;nbsp; But you can instead re-use the same macro variable if you set the order of the variables in the SELECT clause properly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc sql noprint ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; select &lt;STRONG&gt;varnum&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , name&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; into&amp;nbsp; &lt;STRONG&gt;:varlist&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,:varlist separated by ' '&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; from dictionary.columns&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; where libname='SASHELP'&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; and memname='CLASS'&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; order by varnum&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="text-align: left;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%put nvars=&amp;amp;sqlobs varlist=&amp;amp;varlist;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now the NOTE no longer appears in the SAS log.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 23 Nov 2013 17:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-into-macro-variables-with-ordering/m-p/154122#M30236</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-11-23T17:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting into macro variables with ordering</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-into-macro-variables-with-ordering/m-p/154123#M30237</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for sharing, Tom. I was kinda wondering if no varnum involved, and no 'order by',&amp;nbsp; what kind of order would it be? Randomly?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 24 Nov 2013 16:09:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-into-macro-variables-with-ordering/m-p/154123#M30237</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2013-11-24T16:09:06Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting into macro variables with ordering</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-into-macro-variables-with-ordering/m-p/154124#M30238</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;SAS will normally preserve the order from the source table when querying with SQL, but it is not guaranteed.&lt;/P&gt;&lt;P&gt;If the underlying table is actually coming form a database system (such as Oracle, SQL Server, etc) then it is most likely that there is not a predefined order or even a consistent order for repetitions of the same query.&amp;nbsp; Perhaps as SAS becomes more multi-threaded this will also be true for SAS datasets.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 24 Nov 2013 16:33:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-into-macro-variables-with-ordering/m-p/154124#M30238</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2013-11-24T16:33:38Z</dc:date>
    </item>
  </channel>
</rss>

