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

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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));
--
Paige Miller
Tom
Super User Tom
Super User

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;

 

Moksha
Pyrite | Level 9
Thank you very much Tom. Both the solutions for n_cntwrd worked.
ballardw
Super User

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.

 

 

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 3530 views
  • 1 like
  • 5 in conversation