<?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: Proc SQL Count+1 function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Count-1-function/m-p/363340#M86024</link>
    <description>&lt;P&gt;Well, it is possible. &amp;nbsp;What you have to remember is that SQL is different from SAS, SAS treats each obsevation step by by step, and if sorted then you can do this calculation. &amp;nbsp;SQL treats observations as a bunch of observations - order is only given by logical sort, not by logical order in a data table. &amp;nbsp;So you can use the monotonic() function like this:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table SCORE as
  select  SBRID,
             monotonic() as COUNTER
  from    WEEKS.TEST;
quit;&lt;/PRE&gt;
&lt;P&gt;However, if you have the data:&lt;/P&gt;
&lt;P&gt;SBRID&lt;/P&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no logical way of telling SQL that the order is:&lt;/P&gt;
&lt;P&gt;SBRID &amp;nbsp;COUNTER&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than:&lt;/P&gt;
&lt;P&gt;SBRID &amp;nbsp;ORDER&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or any other combination. &amp;nbsp;Is there a reason why you need to do this in SQL, generally speaking for SQL an observation count in itself is not really useful, aggregate functions are of more use (sum, count etc.).&lt;/P&gt;</description>
    <pubDate>Thu, 01 Jun 2017 08:17:57 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-06-01T08:17:57Z</dc:date>
    <item>
      <title>Proc SQL Count+1 function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Count-1-function/m-p/363337#M86022</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below code which is in a data step but i wanted to know if this was possible to do in a Proc SQL statement?&lt;/P&gt;&lt;PRE&gt;DATA SCORE;
SET WEEKS.TEST;
BY SBRID;
COUNTER+1;
IF FIRST.SBRID THEN COUNTER=1;
RUN;&lt;/PRE&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 08:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Count-1-function/m-p/363337#M86022</guid>
      <dc:creator>CamRutherford</dc:creator>
      <dc:date>2017-06-01T08:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Count+1 function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Count-1-function/m-p/363340#M86024</link>
      <description>&lt;P&gt;Well, it is possible. &amp;nbsp;What you have to remember is that SQL is different from SAS, SAS treats each obsevation step by by step, and if sorted then you can do this calculation. &amp;nbsp;SQL treats observations as a bunch of observations - order is only given by logical sort, not by logical order in a data table. &amp;nbsp;So you can use the monotonic() function like this:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table SCORE as
  select  SBRID,
             monotonic() as COUNTER
  from    WEEKS.TEST;
quit;&lt;/PRE&gt;
&lt;P&gt;However, if you have the data:&lt;/P&gt;
&lt;P&gt;SBRID&lt;/P&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;P&gt;001&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no logical way of telling SQL that the order is:&lt;/P&gt;
&lt;P&gt;SBRID &amp;nbsp;COUNTER&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than:&lt;/P&gt;
&lt;P&gt;SBRID &amp;nbsp;ORDER&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;001 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or any other combination. &amp;nbsp;Is there a reason why you need to do this in SQL, generally speaking for SQL an observation count in itself is not really useful, aggregate functions are of more use (sum, count etc.).&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 08:17:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Count-1-function/m-p/363340#M86024</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-06-01T08:17:57Z</dc:date>
    </item>
  </channel>
</rss>

