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.

 

 

 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register 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.

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