BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LuLuVolcano73
Calcite | Level 5

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
Reeza
Super User

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

PaigeMiller
Diamond | Level 26

@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
ballardw
Super User

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,

 

LuLuVolcano73
Calcite | Level 5

Thank you! You are AWESOME!

PaigeMiller
Diamond | Level 26

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

--
Paige Miller

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1456 views
  • 7 likes
  • 4 in conversation