<?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 SQL Order By Statement is generating the error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376445#M90370</link>
    <description>&lt;P&gt;The following is a piece of my program. I would like to count how many times a particular number has borrowed for the last 30 days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%MACRO LOOP(dataset);

%DO I=1 %TO 10; 
proc sql outobs=1;
select count(*) from &amp;amp;dataset
where msisdn = &amp;amp;&amp;amp;msisdn&amp;amp;I and 
abs(datepart(advance_datetime) - 
datepart((select advance_datetime from &amp;amp;dataset where msisdn = &amp;amp;&amp;amp;msisdn&amp;amp;I
order by advance_datetime desc limit 1)) &amp;lt; 31;
quit;
%END;
%MEND LOOP;
%LOOP(credit.bbhistory);&lt;/PRE&gt;&lt;P&gt;When I run it, it generated the following error message(see the bellow snapshot).&lt;/P&gt;&lt;P&gt;Please, help me and tell me what I am doing wrong.&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10361i73476266AE4C8B34/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Order by error.JPG" title="Order by error.JPG" /&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Jul 2017 10:15:45 GMT</pubDate>
    <dc:creator>mathberd</dc:creator>
    <dc:date>2017-07-17T10:15:45Z</dc:date>
    <item>
      <title>SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376445#M90370</link>
      <description>&lt;P&gt;The following is a piece of my program. I would like to count how many times a particular number has borrowed for the last 30 days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%MACRO LOOP(dataset);

%DO I=1 %TO 10; 
proc sql outobs=1;
select count(*) from &amp;amp;dataset
where msisdn = &amp;amp;&amp;amp;msisdn&amp;amp;I and 
abs(datepart(advance_datetime) - 
datepart((select advance_datetime from &amp;amp;dataset where msisdn = &amp;amp;&amp;amp;msisdn&amp;amp;I
order by advance_datetime desc limit 1)) &amp;lt; 31;
quit;
%END;
%MEND LOOP;
%LOOP(credit.bbhistory);&lt;/PRE&gt;&lt;P&gt;When I run it, it generated the following error message(see the bellow snapshot).&lt;/P&gt;&lt;P&gt;Please, help me and tell me what I am doing wrong.&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10361i73476266AE4C8B34/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Order by error.JPG" title="Order by error.JPG" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 10:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376445#M90370</guid>
      <dc:creator>mathberd</dc:creator>
      <dc:date>2017-07-17T10:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376457#M90371</link>
      <description>&lt;P&gt;Could be a few things, but first off order by does not work in sub queries. &amp;nbsp;Also, at no point in that code is: MSISDN declared. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The question is why do it this way in the first place? &amp;nbsp;There is no explanation for the whole do loop for instance. &amp;nbsp;However if I wanted to get a count based on a condition then the steps are simple:&lt;/P&gt;
&lt;P&gt;1) create a category to indicate if the value is within a window&lt;/P&gt;
&lt;P&gt;2) count based on the category&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  if date between low and high then cat="Y";
run;

proc sql;
  create table WANT as
  select  count(*) as RESULT
  from    WANT
  group by CAT;
quit;&lt;/PRE&gt;
&lt;P&gt;That would give you a two way result, count of those having Y and those not. &amp;nbsp;Also, follow the guidance&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 10:23:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376457#M90371</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-17T10:23:03Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376458#M90372</link>
      <description>&lt;P&gt;Run the SQL outside of a macro and macro loop (set the macro variable i manually), and look at the log. The ERROR message will point to the location of the error, and make it easier to debug.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 10:24:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376458#M90372</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-17T10:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376463#M90373</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;, thanks for your input. This is for clarification.&lt;BR /&gt;As you can see the msisdn is a macro variable, it has been declared outside of the loop function. additionally, the reason I wished to get the counts in that way, is to avoid creating intermediary datasets</description>
      <pubDate>Mon, 17 Jul 2017 10:30:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376463#M90373</guid>
      <dc:creator>mathberd</dc:creator>
      <dc:date>2017-07-17T10:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376465#M90374</link>
      <description>&lt;P&gt;After further looking, I count four opening and three closing brackets in your SQL where condition; correct that mismatch.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 10:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376465#M90374</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-17T10:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376468#M90375</link>
      <description>&lt;P&gt;Please ensure you post&amp;nbsp;&lt;STRONG&gt;all&lt;/STRONG&gt; relelvant information in the post. &amp;nbsp;As for not wanting to creating an intermediate dataset - you are creating an intermediate dataset (for each iteration of the do loop) however you just don't see it. &amp;nbsp;The method you are using here will not be more efficient than having one dataset and one count procedure, as yours will always be * 10.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 10:43:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376468#M90375</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-17T10:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376473#M90376</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO LOOP(dataset);
 
%DO I=1 %TO 10; 
proc sql outobs=1;

select count(*) from &amp;amp;dataset
where msisdn = &amp;amp;&amp;amp;msisdn&amp;amp;I and 
abs(datepart(advance_datetime) - 
datepart((select advance_datetime from &amp;amp;dataset 
where msisdn = &amp;amp;&amp;amp;msisdn&amp;amp;I order by advance_datetime desc limit 1))) &amp;lt; 31;
quit;

%END;
%MEND LOOP; 

%LOOP(credit.bbhistory);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is the codes after adding the fourth closing. Like you mentioned, it was the problem. However, it did not get completely resolved.&lt;/P&gt;&lt;P&gt;This is what I got ; error message due to limit statement&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10362i94F3F9A72A5DF37E/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="limit error.JPG" title="limit error.JPG" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 10:50:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376473#M90376</guid>
      <dc:creator>mathberd</dc:creator>
      <dc:date>2017-07-17T10:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376476#M90377</link>
      <description>&lt;P&gt;There is no &lt;FONT face="courier new,courier"&gt;limit&lt;/FONT&gt; statement in SAS.&lt;/P&gt;
&lt;P&gt;Do you have some custom code defined that is sent before every program submit, or is that part of your code?.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 10:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376476#M90377</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-17T10:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376477#M90378</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;, Ok. Let me then try your steps</description>
      <pubDate>Mon, 17 Jul 2017 10:56:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376477#M90378</guid>
      <dc:creator>mathberd</dc:creator>
      <dc:date>2017-07-17T10:56:04Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376481#M90379</link>
      <description>this is part of my code (it's a function which can be called anytime )</description>
      <pubDate>Mon, 17 Jul 2017 11:00:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376481#M90379</guid>
      <dc:creator>mathberd</dc:creator>
      <dc:date>2017-07-17T11:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Order By Statement is generating the error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376484#M90380</link>
      <description>&lt;P&gt;Please supply link to the documentation for "limit". &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no limit statement or function in SAS.&lt;/P&gt;
&lt;P&gt;If you want to limit the number of output observations from proc sql, look into the outobs= option for the proc sql statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;limit n works in MySQL, but it is not standard SQL language.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2017 11:08:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-Order-By-Statement-is-generating-the-error/m-p/376484#M90380</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-17T11:08:35Z</dc:date>
    </item>
  </channel>
</rss>

