DATA Step, Macro, Functions and more

COUNTW in a macro with incorrect word counts

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

COUNTW in a macro with incorrect word counts

Hi,

 

Does anyone know if "countw" can skip the space between single quoted words?

 

Here is my code:

 

%macro colnames1(colnames1);

     %put "number of names in columns is " %sysfunc(countw(&colnames1));

%mend colnames1;

%colnames1(Contracts age 'young boys'n 'young girls'n);

run;

 

The result is:

 

%macro colnames1(colnames1);

             %put "number of names in columns is " %sysfunc(countw(&colnames1));

         %mend colnames1;

       %colnames1(Contracts age 'young boys'n 'young girls'n);

"number of names in columns is " 6

         run;

 

There should only be 4 words ('contracts', 'age', 'young boys' and 'young girls'). The program is counting the spaces in between the single quotes.

I further tried to following but still didn't get the results I wanted (still gave me 6 words).

 

%put "number of names in columns is " %sysfunc(countw(&colnames2%str("")));

 

 

THANKS!

 


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 11,810

Re: COUNTW in a macro with incorrect word counts

Posted in reply to LuLuVolcano73

Try

%macro colnames1(colnames1);

     %put "number of names in columns is " %sysfunc(countw(&colnames1," " ,Q ));

%mend colnames1;

The Q says to ignore delimiters within quotes,

 

View solution in original post


All Replies
Super User
Posts: 20,731

Re: COUNTW in a macro with incorrect word counts

Posted in reply to LuLuVolcano73

One reason you shouldn't use named literals but oh well. 

An easy fix would be to add a delimiter to the words and count that instead, ie 

 

%colnames1(Contracts | age |  'young boys'n | 'young girls'n);

 

Use COUNTC to count the number of | and then add one to determine the number of 'variables'.

Respected Advisor
Posts: 2,055

Re: COUNTW in a macro with incorrect word counts

[ Edited ]
Posted in reply to LuLuVolcano73

LuLuVolcano73 wrote:

Hi,

 

Does anyone know if "countw" can skip the space between single quoted words?

 

Here is my code:

 

%macro colnames1(colnames1);

     %put "number of names in columns is " %sysfunc(countw(&colnames1));

%mend colnames1;

%colnames1(Contracts age 'young boys'n 'young girls'n);

run;

 

The result is:

 

%macro colnames1(colnames1);

             %put "number of names in columns is " %sysfunc(countw(&colnames1));

         %mend colnames1;

       %colnames1(Contracts age 'young boys'n 'young girls'n);

"number of names in columns is " 6

         run;

 

There should only be 4 words ('contracts', 'age', 'young boys' and 'young girls'). The program is counting the spaces in between the single quotes.

I further tried to following but still didn't get the results I wanted (still gave me 6 words).

 

%put "number of names in columns is " %sysfunc(countw(&colnames2%str("")));

 

 

THANKS!

 


That run; statement doesn't belong ...

 

however, COUNTW is doing the proper thing here, it just sees colnames as a text string, it doesn't understand that you think 'young boys'n is one word. it is simply looking for the space to delimit the words, and so 'young boys'n is two words, the first being 'young and the second being boys'n.

 

If you really want this to be 4 words, you would need some other approach, I don't think COUNTW can get there. Perhaps using regular expressions can turn 'young boys'n into young_boys which then is counted properly by COUNTW. 

 

Or, you can put the anti-delimiters in manually, replacing 'young boys'n with young_boys and 'young girls'n with young_girls. And also in this case COUNTW will work as expected.

--
Paige Miller
New Contributor
Posts: 3

Re: COUNTW in a macro with incorrect word counts

Posted in reply to PaigeMiller

Thanks.

Solution
3 weeks ago
Super User
Posts: 11,810

Re: COUNTW in a macro with incorrect word counts

Posted in reply to LuLuVolcano73

Try

%macro colnames1(colnames1);

     %put "number of names in columns is " %sysfunc(countw(&colnames1," " ,Q ));

%mend colnames1;

The Q says to ignore delimiters within quotes,

 

New Contributor
Posts: 3

Re: COUNTW in a macro with incorrect word counts

Thank you! You are AWESOME!

Respected Advisor
Posts: 2,055

Re: COUNTW in a macro with incorrect word counts

Wow, @ballardw, I did not know that. That is a very easy solution!

--
Paige Miller
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 228 views
  • 7 likes
  • 4 in conversation