Hi All,
I am going through the code at:
https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p098-26.pdf
In that, they are calling a macro N_CNTWRD that counts the number of variables in a specified list. It takes two parameters, first one the list and second one to store the count.
Can anyone help me how to write this macro N_CNTWRD?
Thanks,
How old is that paper? Perhaps it predates the addition of the %SYSFUNC() macro function because you can just use it to call COUNTW() to count how many words are in a string.
Since the other code is generating the list of names using SPACE as the delimiter you should tell COUNTW() to use SPACE as the delimiter.
You don't really need to create a macro for something so simple. Just replace:
%n_cntwrd(string=&thesedat, cntvar=VARCNT)
With
%let VARCNT=%sysfunc(countw(&thesedat,%str( )));
If you did want to mimic the N_CNTWRD macro perhaps it might look like this:
%macro n_cntwrd(string,cntvar);
%if not %symexist(&cntvar) %then %global &cntvar;
%let &cntvar=%sysfunc(countw(&string,%str( )));
%mend;
In reality for the example macro n that paper there was no need call that n_cntwrd macro at all.
PROC SQL already counts for you into the macro variable SQLOBS. So to get the count and the list of variables in a dataset into macro variables just use:
proc sql noprint;
%let thesedat=;
select nliteral(NAME)
into :thesedat separated by ' '
from DATES
where libname=%upcase("&lib")
and memname=%upcase("&indata")
;
%let varcnt=&sqlobs;
quit;
Here's a simple way to count number of variables in a list (although I assume the list is a macro variable itself, you really ought to give an example)
%let list=temperature humidity pressure;
%let n_words=%sysfunc(countw(&list));
COUNTW Function documentation
How old is that paper? Perhaps it predates the addition of the %SYSFUNC() macro function because you can just use it to call COUNTW() to count how many words are in a string.
Since the other code is generating the list of names using SPACE as the delimiter you should tell COUNTW() to use SPACE as the delimiter.
You don't really need to create a macro for something so simple. Just replace:
%n_cntwrd(string=&thesedat, cntvar=VARCNT)
With
%let VARCNT=%sysfunc(countw(&thesedat,%str( )));
If you did want to mimic the N_CNTWRD macro perhaps it might look like this:
%macro n_cntwrd(string,cntvar);
%if not %symexist(&cntvar) %then %global &cntvar;
%let &cntvar=%sysfunc(countw(&string,%str( )));
%mend;
In reality for the example macro n that paper there was no need call that n_cntwrd macro at all.
PROC SQL already counts for you into the macro variable SQLOBS. So to get the count and the list of variables in a dataset into macro variables just use:
proc sql noprint;
%let thesedat=;
select nliteral(NAME)
into :thesedat separated by ' '
from DATES
where libname=%upcase("&lib")
and memname=%upcase("&indata")
;
%let varcnt=&sqlobs;
quit;
Don't actually need a macro COUNTW is datastep function. Provide a list of values in a variable or literal string. You can even specify specific delimiters if needed.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.