<?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 Number of observation by expression. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-by-expression/m-p/974268#M377860</link>
    <description>&lt;P&gt;I want to get observation number from expression &amp;lt;&lt;STRONG&gt;dsname(where=(...))&lt;/STRONG&gt;&amp;gt; by using macro code. So I could write in any place (inside other procedure or %if statement) &lt;STRONG&gt;%if %sysfunc(nobs(dsname(where=(...)))) &amp;gt; 0 %then %do;&amp;nbsp; BY-statement-code; %end;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;My current solution uses &lt;STRONG&gt;PROC FCMP&lt;/STRONG&gt; and &lt;STRONG&gt;DOSUBL&lt;/STRONG&gt; inside custom function &lt;STRONG&gt;NOBS&lt;/STRONG&gt;. After all I use &lt;STRONG&gt;%sysfunc(NOBS(...))&lt;/STRONG&gt;. Inside &lt;STRONG&gt;NOBS &lt;/STRONG&gt;function I use &lt;STRONG&gt;NOBS ATTRN&lt;/STRONG&gt;&amp;nbsp;to obtain answer if possible. Otherwise, inside &lt;STRONG&gt;DOSUBL&lt;/STRONG&gt; I use &lt;STRONG&gt;PROC SQL&lt;/STRONG&gt; to count observation number. Is there any other universal solution, which more stable than &lt;STRONG&gt;NLOBSF&lt;/STRONG&gt;?&lt;/P&gt;&lt;P&gt;I think I could use &lt;STRONG&gt;FETCH&lt;/STRONG&gt; inside &lt;STRONG&gt;NOBS&lt;/STRONG&gt; function, but I don't know: should I extract &lt;STRONG&gt;WHERE=&lt;/STRONG&gt; option and then apply something like &lt;STRONG&gt;If not where_string then delete;&amp;nbsp;&lt;/STRONG&gt;But there is a problem, that i can't in one function call extract where_string and use it as code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;DOSUBL seems very hard for performance. It's intitializes additional environment/space/memory?&lt;/P&gt;&lt;P&gt;Is there a way to do this elegantly and reliably?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Sep 2025 16:19:58 GMT</pubDate>
    <dc:creator>TimurShangareev</dc:creator>
    <dc:date>2025-09-05T16:19:58Z</dc:date>
    <item>
      <title>Number of observation by expression.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-by-expression/m-p/974268#M377860</link>
      <description>&lt;P&gt;I want to get observation number from expression &amp;lt;&lt;STRONG&gt;dsname(where=(...))&lt;/STRONG&gt;&amp;gt; by using macro code. So I could write in any place (inside other procedure or %if statement) &lt;STRONG&gt;%if %sysfunc(nobs(dsname(where=(...)))) &amp;gt; 0 %then %do;&amp;nbsp; BY-statement-code; %end;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;My current solution uses &lt;STRONG&gt;PROC FCMP&lt;/STRONG&gt; and &lt;STRONG&gt;DOSUBL&lt;/STRONG&gt; inside custom function &lt;STRONG&gt;NOBS&lt;/STRONG&gt;. After all I use &lt;STRONG&gt;%sysfunc(NOBS(...))&lt;/STRONG&gt;. Inside &lt;STRONG&gt;NOBS &lt;/STRONG&gt;function I use &lt;STRONG&gt;NOBS ATTRN&lt;/STRONG&gt;&amp;nbsp;to obtain answer if possible. Otherwise, inside &lt;STRONG&gt;DOSUBL&lt;/STRONG&gt; I use &lt;STRONG&gt;PROC SQL&lt;/STRONG&gt; to count observation number. Is there any other universal solution, which more stable than &lt;STRONG&gt;NLOBSF&lt;/STRONG&gt;?&lt;/P&gt;&lt;P&gt;I think I could use &lt;STRONG&gt;FETCH&lt;/STRONG&gt; inside &lt;STRONG&gt;NOBS&lt;/STRONG&gt; function, but I don't know: should I extract &lt;STRONG&gt;WHERE=&lt;/STRONG&gt; option and then apply something like &lt;STRONG&gt;If not where_string then delete;&amp;nbsp;&lt;/STRONG&gt;But there is a problem, that i can't in one function call extract where_string and use it as code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;DOSUBL seems very hard for performance. It's intitializes additional environment/space/memory?&lt;/P&gt;&lt;P&gt;Is there a way to do this elegantly and reliably?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Sep 2025 16:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-by-expression/m-p/974268#M377860</guid>
      <dc:creator>TimurShangareev</dc:creator>
      <dc:date>2025-09-05T16:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation by expression.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-by-expression/m-p/974291#M377872</link>
      <description>&lt;P&gt;What is your concern with using NLOBSF?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Years ago I was inspired by this paper by Jack Hamilton:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p095-26.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p095-26.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And wrote a obscount macro, the core of which is to use NLOBS if SAS knows the number of records and there is no WHERE options, else use NLOBSF:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    %if %sysfunc(attrn(&amp;amp;idnum, ANOBS)) and %sysfunc(attrn(&amp;amp;idnum, WHSTMT))=0 %then %do;
      %*If sas knows number of obs, and there is no where statement, get nlobs;
      %let nlobs = %sysfunc(attrn(&amp;amp;idnum,nlobs)); %*get number of obs;
    %end;
    %else %if %sysfunc(attrn(&amp;amp;idnum, ANOBS))=0 
              or (1&amp;lt;=%sysfunc(attrn(&amp;amp;idnum, WHSTMT)) and %sysfunc(attrn(&amp;amp;idnum, WHSTMT)) &amp;lt;=3) %then %do;
      %*If sas doesnt know number of obs, or there is a where statement, get nlobsf (this iterates through dataset);
      %let nlobs = %sysfunc(attrn(&amp;amp;idnum,nlobsf)); %*get number of obs;
    %end; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Yes, DOSUBL is slow compared to most other functions, because it's doing a LOT of work (creating and destroying the side session to run execute the code).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Sep 2025 19:25:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-by-expression/m-p/974291#M377872</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-09-05T19:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: Number of observation by expression.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-by-expression/m-p/974298#M377876</link>
      <description>&lt;P&gt;Why not use FETCH ?&amp;nbsp; Been using it for a LOOONG time.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/sasutils/macros/blob/master/nobs.sas" target="_blank"&gt;https://github.com/sasutils/macros/blob/master/nobs.sas&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Sep 2025 21:28:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-observation-by-expression/m-p/974298#M377876</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-09-05T21:28:45Z</dc:date>
    </item>
  </channel>
</rss>

