<?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: Assert number of observations in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837922#M331322</link>
    <description>&lt;P&gt;Looks very complex.&amp;nbsp; No need to re-invent the wheel here.&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;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename nobs url "https://raw.githubusercontent.com/sasutils/macros/master/nobs.sas";
%include nobs;

%macro assert(assertion,description=);
if not (&amp;amp;assertion) then do;
  putlog "ERROR: Assertion (%superq(assertion)) is FALSE.";
%if %length(description) %then %do;
  putlog "ERROR- Description= &amp;amp;description";
%end;
  abort;
end;
else do;
  putlog "All obs present.";
end;
%mend assert;

%nobs(sashelp.cars(where=(make='Acura')),mvar=nobs)
options mprint;
data _null_;
 %assert((&amp;amp;nobs=7),description=Number of Acuras is 7)
 %assert((&amp;amp;nobs=6),description=Number of Acuras is 6)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1665514589692.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76094i0621478AF5D2E72B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1665514589692.png" alt="Tom_0-1665514589692.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Oct 2022 18:56:43 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-10-11T18:56:43Z</dc:date>
    <item>
      <title>Assert number of observations in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837861#M331308</link>
      <description>&lt;P&gt;Hello, I'm new to sas and am looking for a way to count number of observations (w/o using proc sql) so that I can verify a dataset has the expected amount of observations. I thought I could do something like this with the code below, but I receive the error "The keyword parameter N was not defined with the macro." Any advice would be greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro assert(assertion);
if not (&amp;amp;assertion) then
putlog "Error: Assertion (%superq(assertion)) is FALSE.";
abort;
%mend assert;

data cars;
set sashelp.cars noobs = n;
where make = 'Acura';
%assert(n = 7);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Oct 2022 14:25:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837861#M331308</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2022-10-11T14:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: Assert number of observations in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837867#M331312</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('n',n),
set dataset nobs=n;
stop;
run;

%if &amp;amp;n. = 7 %then %do;
%put your message;
%end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Oct 2022 15:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837867#M331312</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-11T15:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Assert number of observations in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837868#M331313</link>
      <description>&lt;P&gt;A macro call like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%assert(n = 7);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;means that you set the named parameter n to 7, so the macro definition must also have a parameter n.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2022 15:09:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837868#M331313</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-11T15:09:05Z</dc:date>
    </item>
    <item>
      <title>Re: Assert number of observations in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837870#M331314</link>
      <description>&lt;P&gt;Two problems with your code.&lt;/P&gt;
&lt;P&gt;The one you found is that the = is part of the macro call syntax. It is used to pass parameter values by name. So you need to work a little harder to pass in your "assertion" so that the macro will get it.&lt;/P&gt;
&lt;P&gt;Some examples:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%assert(assertion=n = 7)
%assert((n = 7))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The second problem is in your data step that where you are calling the macro.&amp;nbsp; It will not work when no observations match the WHERE clause.&amp;nbsp; SAS data steps normally stop when they read past the end of the input.&amp;nbsp; By placing the call to %ASSERT() after the set the code it generates will never run. You need to test the NOBS= variable BEFORE the SET statement.&lt;/P&gt;
&lt;P&gt;You also need to spell the NOBS= option correctly.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars;
  %assert( (n = 7) )
  set sashelp.cars nobs = n;
  where make = 'Acura';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2022 15:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837870#M331314</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-11T15:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: Assert number of observations in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837891#M331319</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;for your helpful responses.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realized that nobs wasn't yielding a logical count of observations (count of obs where make = 'Acura'), which was what I was after. So I used nlobs to get a locial count and incorporated your responses into my code below and my code now works. Thanks again!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro assert(assertion);
if not (&amp;amp;assertion) then
do;
putlog "Error: Assertion (%superq(assertion)) is FALSE.";
abort;
end;
else
do;
putlog "All obs present.";
end;
%mend assert;

data cars;
set sashelp.cars;
where make = 'Acura';
run;

data _null_;
dsid = open('cars');
obscount = attrn(dsid,'nlobs');
rc = close(dsid);
%assert((obscount=7));
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2022 17:26:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837891#M331319</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2022-10-11T17:26:31Z</dc:date>
    </item>
    <item>
      <title>Re: Assert number of observations in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837922#M331322</link>
      <description>&lt;P&gt;Looks very complex.&amp;nbsp; No need to re-invent the wheel here.&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;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename nobs url "https://raw.githubusercontent.com/sasutils/macros/master/nobs.sas";
%include nobs;

%macro assert(assertion,description=);
if not (&amp;amp;assertion) then do;
  putlog "ERROR: Assertion (%superq(assertion)) is FALSE.";
%if %length(description) %then %do;
  putlog "ERROR- Description= &amp;amp;description";
%end;
  abort;
end;
else do;
  putlog "All obs present.";
end;
%mend assert;

%nobs(sashelp.cars(where=(make='Acura')),mvar=nobs)
options mprint;
data _null_;
 %assert((&amp;amp;nobs=7),description=Number of Acuras is 7)
 %assert((&amp;amp;nobs=6),description=Number of Acuras is 6)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1665514589692.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76094i0621478AF5D2E72B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1665514589692.png" alt="Tom_0-1665514589692.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2022 18:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Assert-number-of-observations-in-dataset/m-p/837922#M331322</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-11T18:56:43Z</dc:date>
    </item>
  </channel>
</rss>

