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

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
Quartz | Level 8
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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 5 replies
  • 537 views
  • 1 like
  • 5 in conversation