<?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: row count in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62015#M17611</link>
    <description>A succinct and efficient way is the use of macro and SCL functions.;&lt;BR /&gt;
&lt;BR /&gt;
%LET DSID = %SYSFUNC(OPEN(TEMP.SAMPLE,IN)); &lt;BR /&gt;
%LET NUM  = %SYSFUNC(ATTRN(&amp;amp;DSID,NLOBS)); &lt;BR /&gt;
%IF &amp;amp;DSID &amp;gt; 0 %THEN %LET RC=%SYSFUNC(CLOSE(&amp;amp;DSID)); &lt;BR /&gt;
&lt;BR /&gt;
%put There are &amp;amp;num observations in the dataset;&lt;BR /&gt;
&lt;BR /&gt;
* The first line opens the data set and the last one closes it. this is needed because you are not using data step or &lt;BR /&gt;
  SCL and so could leave a data set open, causing problems later. The second line is what captures the number of observations &lt;BR /&gt;
  from the header of the data set using the SCL ATTRN function called by %SYSFUNC. &lt;BR /&gt;
&lt;BR /&gt;
* with where clause;&lt;BR /&gt;
%let dsid = %sysfunc(open(temp.sample (where=(x&amp;gt;500000))));&lt;BR /&gt;
%let num  = %sysfunc(attrn(&amp;amp;dsid,nlobs));&lt;BR /&gt;
%if &amp;amp;DSID &amp;gt; 0 %then %let rc = %sysfunc(close(&amp;amp;dsid));</description>
    <pubDate>Thu, 06 Aug 2009 22:44:42 GMT</pubDate>
    <dc:creator>rob_sas</dc:creator>
    <dc:date>2009-08-06T22:44:42Z</dc:date>
    <item>
      <title>row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62010#M17606</link>
      <description>hi,&lt;BR /&gt;
&lt;BR /&gt;
how can i store the obs count of a dataset to a macro variable when i use proc datasets?&lt;BR /&gt;
&lt;BR /&gt;
i know i saw a sample of this code back then but now i couldnt find it anymore &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
thanks a lot!</description>
      <pubDate>Thu, 06 Aug 2009 02:26:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62010#M17606</guid>
      <dc:creator>milts</dc:creator>
      <dc:date>2009-08-06T02:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62011#M17607</link>
      <description>The two processes are inconsistent.  There are SAS CALL functions and DICTIONARY members(s) and the SET stmt NOBS= parameter, used with a CALL SYMPUT for the purpose mentioned.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 06 Aug 2009 02:48:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62011#M17607</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-08-06T02:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62012#M17608</link>
      <description>hi scott,&lt;BR /&gt;
&lt;BR /&gt;
thanks for the info, but in what way are these approaches inconsistent?&lt;BR /&gt;
&lt;BR /&gt;
is there any other way other than the select count into: statement in getting the number of obs of a certain dataset?&lt;BR /&gt;
&lt;BR /&gt;
thanks,&lt;BR /&gt;
milton</description>
      <pubDate>Thu, 06 Aug 2009 03:14:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62012#M17608</guid>
      <dc:creator>milts</dc:creator>
      <dc:date>2009-08-06T03:14:02Z</dc:date>
    </item>
    <item>
      <title>Re: row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62013#M17609</link>
      <description>Yes there is, and much more efficient too.&lt;BR /&gt;
&lt;BR /&gt;
Both DICTIONARY tables method, PROC CONTENTS (you may have confused this with PROC DATASETS), or even a DATA STEP with the NOBS options will retrieve immediately the count of obs (EXCEPT if the table is a VIEW or DBMS table) WITHOUT the need to scan the whole data as the SELECT COUNT INTO: technique will need to do.&lt;BR /&gt;
&lt;BR /&gt;
Check online documentation for DICTIONARY tables and the CONTENTS procedure:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002299818.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002299818.htm&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a000085768.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/a000085768.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;.</description>
      <pubDate>Thu, 06 Aug 2009 07:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62013#M17609</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-08-06T07:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62014#M17610</link>
      <description>Thanks Daniel that's what I'm looking for.&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Milton</description>
      <pubDate>Thu, 06 Aug 2009 07:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62014#M17610</guid>
      <dc:creator>milts</dc:creator>
      <dc:date>2009-08-06T07:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62015#M17611</link>
      <description>A succinct and efficient way is the use of macro and SCL functions.;&lt;BR /&gt;
&lt;BR /&gt;
%LET DSID = %SYSFUNC(OPEN(TEMP.SAMPLE,IN)); &lt;BR /&gt;
%LET NUM  = %SYSFUNC(ATTRN(&amp;amp;DSID,NLOBS)); &lt;BR /&gt;
%IF &amp;amp;DSID &amp;gt; 0 %THEN %LET RC=%SYSFUNC(CLOSE(&amp;amp;DSID)); &lt;BR /&gt;
&lt;BR /&gt;
%put There are &amp;amp;num observations in the dataset;&lt;BR /&gt;
&lt;BR /&gt;
* The first line opens the data set and the last one closes it. this is needed because you are not using data step or &lt;BR /&gt;
  SCL and so could leave a data set open, causing problems later. The second line is what captures the number of observations &lt;BR /&gt;
  from the header of the data set using the SCL ATTRN function called by %SYSFUNC. &lt;BR /&gt;
&lt;BR /&gt;
* with where clause;&lt;BR /&gt;
%let dsid = %sysfunc(open(temp.sample (where=(x&amp;gt;500000))));&lt;BR /&gt;
%let num  = %sysfunc(attrn(&amp;amp;dsid,nlobs));&lt;BR /&gt;
%if &amp;amp;DSID &amp;gt; 0 %then %let rc = %sysfunc(close(&amp;amp;dsid));</description>
      <pubDate>Thu, 06 Aug 2009 22:44:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62015#M17611</guid>
      <dc:creator>rob_sas</dc:creator>
      <dc:date>2009-08-06T22:44:42Z</dc:date>
    </item>
    <item>
      <title>Re: row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62016#M17612</link>
      <description>Succint and with a where clause!&lt;BR /&gt;
Introduced with SAS9 (iirc) attrn NLOBSF will provide the NOBS as fast as normal for a normal data set, and instead of missing, 0 or -1 for a dataset subject to a where clause, it will go and make the count! here is a log snippet of my demo, based on the code from rob@sas&lt;BR /&gt;
238  data temp.sample ;&lt;BR /&gt;
239    do x= 1 to 1e7 ;&lt;BR /&gt;
240      ranp = 1+ nobs*ranuni(1) ;&lt;BR /&gt;
241      classn = RANP ;&lt;BR /&gt;
242      set sashelp.class point = ranp nobs= nobs;&lt;BR /&gt;
243      output ;&lt;BR /&gt;
244    end;&lt;BR /&gt;
245    stop;&lt;BR /&gt;
246  run;&lt;BR /&gt;
&lt;BR /&gt;
NOTE: The data set TEMP.SAMPLE has 10000000 observations and 7 variables.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           22.68 seconds&lt;BR /&gt;
      user cpu time       6.40 seconds&lt;BR /&gt;
      system cpu time     2.18 seconds&lt;BR /&gt;
      Memory                            181k&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
247  %let start = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
248  %let dsid = %sysfunc(open(temp.sample ));&lt;BR /&gt;
249  %let num = %sysfunc(attrn(&amp;amp;dsid,nlobsf));&lt;BR /&gt;
250  %let rc = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
251  %let here1 = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
252  %let durn1 = %sysevalf( &amp;amp;here1 - &amp;amp;start ) ;&lt;BR /&gt;
253  %put without where rc=&amp;amp;rc dsid= &amp;amp;dsid   num=&amp;amp;num  durn= &amp;amp;durn1 %now ;&lt;BR /&gt;
without where rc=0 dsid= 1   num=10000000  durn= 0 07AUG2009:12:33:38.681&lt;BR /&gt;
254&lt;BR /&gt;
255  %let star2  = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
256  %let dsid   = %sysfunc(open(temp.sample (where=(x&amp;gt;500000))));&lt;BR /&gt;
257  %let num    = %sysfunc(attrn(&amp;amp;dsid,nlobsf));&lt;BR /&gt;
258  %let rc     = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
259  %let here2  = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
260  %let durn2  = %sysevalf( &amp;amp;here2 - &amp;amp;star2 ) ;&lt;BR /&gt;
261  %put with    where rc=&amp;amp;rc dsid= &amp;amp;dsid   num=&amp;amp;num  durn= &amp;amp;durn2 %now ;&lt;BR /&gt;
with    where rc=0 dsid= 1   num=9500000  durn= 25.1400001049041 07AUG2009:12:34:03.821&lt;BR /&gt;
&lt;BR /&gt;
With a WHERE clause, interestingly, as durn= a smaller number when the where clause returns a small number of rows, and larger when there are more to count ...&lt;BR /&gt;
342  %let star2  = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
343  %let dsid   = %sysfunc(open(temp.sample (where=(x&amp;gt;9500000))));&lt;BR /&gt;
344  %let num    = %sysfunc(attrn(&amp;amp;dsid,nlobsf));&lt;BR /&gt;
345  %let rc     = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
346  %let here2  = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
347  %let durn2  = %sysevalf( &amp;amp;here2 - &amp;amp;star2 ) ;&lt;BR /&gt;
348  %put with    where rc=&amp;amp;rc dsid= &amp;amp;dsid   num=&amp;amp;num  durn= &amp;amp;durn2 %now ;&lt;BR /&gt;
with    where rc=0 dsid= 1   num=500000  durn= 3.375 07AUG2009:15:34:58.682&lt;BR /&gt;
&lt;BR /&gt;
349  %let star2  = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
350  %let dsid   = %sysfunc(open(temp.sample (where=(x&amp;gt;1))));&lt;BR /&gt;
351  %let num    = %sysfunc(attrn(&amp;amp;dsid,nlobsf));&lt;BR /&gt;
352  %let rc     = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
353  %let here2  = %sysfunc( datetime(),best18 ) ;&lt;BR /&gt;
354  %let durn2  = %sysevalf( &amp;amp;here2 - &amp;amp;star2 ) ;&lt;BR /&gt;
355  %put with    where rc=&amp;amp;rc dsid= &amp;amp;dsid   num=&amp;amp;num  durn= &amp;amp;durn2 %now ;&lt;BR /&gt;
with    where rc=0 dsid= 1   num=9999999  durn= 24.2490000724792 07AUG2009:15:35:41.119&lt;BR /&gt;
 &lt;BR /&gt;
 &lt;BR /&gt;
it appears that where-clause handling is more efficient than the counting. ~~~~~~&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
PeterC

Message was edited by: Peter.C</description>
      <pubDate>Fri, 07 Aug 2009 15:58:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62016#M17612</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2009-08-07T15:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: row count</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62017#M17613</link>
      <description>wow! thanks rob and peter. learned something new from your posts.&lt;BR /&gt;
&lt;BR /&gt;
regards,&lt;BR /&gt;
milton</description>
      <pubDate>Tue, 11 Aug 2009 04:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/row-count/m-p/62017#M17613</guid>
      <dc:creator>milts</dc:creator>
      <dc:date>2009-08-11T04:35:44Z</dc:date>
    </item>
  </channel>
</rss>

