BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9
Data _null_; 
	*i=1; 
	if i = 0 then set &ds. nobs= mycount; 
	Call symput('mycount', mycount); 
	Run; 

The "mycount" macro variable keeps the dataset row count. 

 

How to put these lines into a macro which returns the dataset row count?!

 

%macro ds_rowct(ds);
....

%mend;
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

I would prefer to use this :

 

%macro ds_rowct(ds);
%let dsid=%sysfunc(open(&ds.));
%let nobs=%sysfunc(attrn(&dsid.,nlobs));
%let dsid=%sysfunc(close(&dsid.));

%put &ds. have &nobs. obs.;
%mend; 



%ds_rowct(sashelp.class)

 

1    %macro ds_rowct(ds);
2    %let dsid=%sysfunc(open(&ds.));
3    %let nobs=%sysfunc(attrn(&dsid.,nlobs));
4    %let dsid=%sysfunc(close(&dsid.));
5
6    %put &ds. have &nobs. obs.;
7    %mend;
8
9
10
11   %ds_rowct(sashelp.class)
sashelp.class have 19 obs.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Macros do not normally "return" anything.  The just generate SAS code for SAS to execute.

 

So the simplest way to have it "return" something is to TELL it WHERE to put what you want.

%macro ds_rowct(ds,mvar=mycount);
%if not %symexist(&mvar) %then %global &mvar;
data _null_; 
  call symputx("&mvar",mycount);
  stop;
  set &ds. nobs=mycount; 
run;
%mend; 

Example:

 82         %ds_rowct(sashelp.class)
 
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 83         %put &=mycount;
 MYCOUNT=19

 

PS: You almost never want to use the ancient CALL SYMPUT() method.  The CALL SYMPUTX() method replaced it decades ago.  The only time you would want CALL SYMPUT() is when you need the macro variable's value to have leading or trailing spaces.

hellohere
Pyrite | Level 9

Thanks,

 

If mvar pre-exists, I assume still work.  But anyway, no additional macro variable?!

 

Macro does not return anything. But much more convenient to "Take" that way , say, 

%if %ds_rowct(&dsname.) %then %do;

....

%end;

If not, simply invoke the macro and then evaluate the speific macro variable, BUT

the reality is that too hard to remember all the variables when you are coding with 

dozens of macros!!!

Tom
Super User Tom
Super User

@hellohere wrote:

Thanks,

 

If mvar pre-exists, I assume still work.  But anyway, no additional macro variable?!

 

Macro does not return anything. But much more convenient to "Take" that way , say, 

%if %ds_rowct(&dsname.) %then %do;

....

%end;

If not, simply invoke the macro and then evaluate the speific macro variable, BUT

the reality is that too hard to remember all the variables when you are coding with 

dozens of macros!!!


You can create macros that only emit PART of a statement.  Then you could use the macro as if it was a function, like in your new example.  Note that your original question did not state that was your objective.

 

There are dozens of examples of macros that do that for the example problem of finding the number of observations in a dataset.  See for example this one:

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

 

%if 0<%nobs(&dsname.,mvar=) %then %do;
Ksharp
Super User

I would prefer to use this :

 

%macro ds_rowct(ds);
%let dsid=%sysfunc(open(&ds.));
%let nobs=%sysfunc(attrn(&dsid.,nlobs));
%let dsid=%sysfunc(close(&dsid.));

%put &ds. have &nobs. obs.;
%mend; 



%ds_rowct(sashelp.class)

 

1    %macro ds_rowct(ds);
2    %let dsid=%sysfunc(open(&ds.));
3    %let nobs=%sysfunc(attrn(&dsid.,nlobs));
4    %let dsid=%sysfunc(close(&dsid.));
5
6    %put &ds. have &nobs. obs.;
7    %mend;
8
9
10
11   %ds_rowct(sashelp.class)
sashelp.class have 19 obs.

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 423 views
  • 1 like
  • 3 in conversation