BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
everyone
Fluorite | Level 6

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. 

 

%macro assert(assertion);
if not (&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;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Two problems with your code.

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.

Some examples:

 

%assert(assertion=n = 7)
%assert((n = 7))

The second problem is in your data step that where you are calling the macro.  It will not work when no observations match the WHERE clause.  SAS data steps normally stop when they read past the end of the input.  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.

You also need to spell the NOBS= option correctly.

data cars;
  %assert( (n = 7) )
  set sashelp.cars nobs = n;
  where make = 'Acura';
run;

 

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Two problems with your code.

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.

Some examples:

 

%assert(assertion=n = 7)
%assert((n = 7))

The second problem is in your data step that where you are calling the macro.  It will not work when no observations match the WHERE clause.  SAS data steps normally stop when they read past the end of the input.  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.

You also need to spell the NOBS= option correctly.

data cars;
  %assert( (n = 7) )
  set sashelp.cars nobs = n;
  where make = 'Acura';
run;

 

everyone
Fluorite | Level 6

Thank you @Tom and @Kurt_Bremser for your helpful responses. 

 

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!

%macro assert(assertion);
if not (&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;

 

 

Tom
Super User Tom
Super User

Looks very complex.  No need to re-invent the wheel here.

https://github.com/sasutils/macros/blob/master/nobs.sas

 

Example:

filename nobs url "https://raw.githubusercontent.com/sasutils/macros/master/nobs.sas";
%include nobs;

%macro assert(assertion,description=);
if not (&assertion) then do;
  putlog "ERROR: Assertion (%superq(assertion)) is FALSE.";
%if %length(description) %then %do;
  putlog "ERROR- Description= &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((&nobs=7),description=Number of Acuras is 7)
 %assert((&nobs=6),description=Number of Acuras is 6)
run;

Result:

Tom_0-1665514589692.png

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 633 views
  • 2 likes
  • 3 in conversation